内部类

1.成员内部类

----先存在外部类对象,然后存在内部类对象   

Face f=new Face();
Face.Nose n=f.new Nose();

----可以在方法内访问外部类的所有属性和方法

2.静态内部类

----可以直接有内部类对象

Face.Ear e=new Face.Ear();

----只可以在方法内访问外部类的static属性和static方法

package mypro01;



public class Outer {
     public static void main(String[] args) {
         
         
         //成员内部类
        //先存在外部类对象,然后存在内部类对象
         
         Face f=new Face();
         Face.Nose n=f.new Nose(); //Face$Nose.class
         System.out.println(n.type); 
         n.breath(); 
         
                
         //静态内部类
         //可以直接有内部类对象
         
         Face.Ear e=new Face.Ear();
         e.listen();
         System.out.println(e.length); 
         
     }
}


class Face{
    int size=10;
    static int age=18;
    
    
    void wash() {
        
        System.out.println("洗脸");
    }
    static void color() {
        
        System.out.println("颜色");
    }
    
    //成员内部类
    //可以在方法内访问外部类的所有属性和方法
    class Nose{
        String type="round";

        void breath() {
            
            System.out.println(size);
            System.out.println(age);
            wash();
            color();
            
            System.out.println("呼吸");
        }
    }
    
    //静态内部类
    //只可以在方法内访问外部类的static属性和static方法
    static class  Ear{
        double length=0.1;
        
        
        void listen() {
            
            System.out.println(age);
            color();
            System.out.println("听声音");
        }
    }
    
}

 

posted on 2020-03-05 17:41  happygril3  阅读(113)  评论(0)    收藏  举报

导航