Java之内部类续

静态内部类

1、语法格式

【修饰符】 class 外部类名  【extends 外部类的父类】 【implements 外部类的父接口】{
    【其他修饰符】 static class  静态内部类 【extends 静态内部类的父类】 【implements 静态内部类的父接口】{
        静态内部类的成员列表;
    }
    
    外部类的其他成员列表;
}

2、 使用注意事项

(1)可以包含类的所有成员

(2)权限修饰符:4种;其他修饰符:abstract、final

(3)只能使用外部类的静态成员

(4)在外部类的外面使用静态内部类的要求 :

(1)如果使用的是静态内部类的静态成员
        外部类名.静态内部类名.静态成员
(2)如果使用的是静态内部类的非静态成员
        ①先创建静态内部类的对象
        外部类名.静态内部类名 对象名 = new 外部类名.静态内部类名(【实参列表】);
        ②通过对象调用非静态成员
        对象名.xxx

3、示例代码

class Out{
    private static int i = 10;
    static class In{
        public void method(){
            System.out.println(i);//可以
        }
        public static void test(){
            System.out.println(i);//可以
        }
    }
    
    public void outMethod(){
        In in = new In();
        in.method();
    }
    public static void outTest(){
        In in = new In();
        in.method();
    }
}
class Test{
    public static void main(String[] args){
        Out.In.test();
        
        Out.In in = new Out.In();
        in.method();
    }
}

非静态内部类

1、语法格式

扫描二维码关注公众号,回复: 6575393 查看本文章

【修饰符】 class 外部类名  【extends 外部类的父类】 【implements 外部类的父接口】{
    【修饰符】 class  非静态内部类 【extends 非静态内部类的父类】 【implements 非静态内部类的父接口】{
        非静态内部类的成员列表;
    }
    
    外部类的其他成员列表;
}

//使用非静态内部类的非静态成员
//(1)创建外部类的对象
外部类名  对象名1 = new  外部类名(实参列表);

//(2)通过外部类的对象去创建或获取非静态内部类的对象
//创建
外部类名.非静态内部类名  对象名2 = 对象名1.new 非静态内部类名(实参列表);

//获取
外部类名.非静态内部类名  对象名2 = 对象名1.get非静态内部类对象的方法(实参列表);

//(3)通过非静态内部类调用它的非静态成员
对象名2.xxx

2、示例代码

class Out{
    private static int i = 10;
    private int j = 20;
    class In{
        public void method(){
            System.out.println(i);
            System.out.println(j);
        }
    }
    
    public void outMethod(){
        In in = new In();
        in.method();
    }
    public static void outTest(){
    }
    
    public Inner getInner(){
        return new Inner();
    }
}
class Test{
    public static void main(String[] args){
        Out out = new Out();
        
        Out.In in1 = out.new In();        
        in1.method();
        
        Out.In in2 = out.getIn();    
        in2.method();
    }
}

局部内部类

1、语法格式

【修饰符】 class 外部类名  【extends 外部类的父类】 【implements 外部类的父接口】{
    【修饰符】 返回值类型  方法名(【形参列表】){
        【修饰符】 class  局部内部类 【extends 局部内部类的父类】 【implements 局部内部类的父接口】{
            局部内部类的成员列表;
        }
    }    
    外部类的其他成员列表;
}

2、示例代码

class Out{
    private static int i = 101;
    private int j = 220;

    
    public void outMethod(){
        class In{
            public void method(){
                //...
                System.out.println(i);
                System.out.println(j);
            }
           }
        In in = new In();
        in.method();
    }
    public static void outTest(){
        final int k = 30;
       class In{
            public void method(){
                System.out.println(i);
                System.out.println(k);
            }
           }
        In in = new In();
        in.method();
    }
}

猜你喜欢

转载自blog.csdn.net/Brevity6/article/details/91126293