内部类

    xiaoxiao2025-06-02  33

     

    内部类: 其实就是定义到了另一个的类的内部。 A类要直接访问B类中的成员时,可以将A类定义到B类中,作为B类的内部类存在。访问规则: 内部类可以直接访问外部类中的成员。

     

    外部类想要访问内部类,只能创建内部类的对象来访问。

     

    class Outer { private int static num = 4; class Inner//内部类。//相当于外部类中的一个成员。它就可以被成员修饰符修饰。public static private { void show(){ System.out.println(num); } } static class Inner2 { static final int count = 5;//在非静态的内部类中只允许定义静态final常量,不允许定义其他其他静态成员。 void show2(){ System.out.println("Inner2:"+num); } static void show3(){ System.out.println("static show3:"+num); } } public void method(){ Inner in = new Inner(); in.show(); } } class InnerClassDemo { public static void main(String args[]){ //Outer out = new Outer(); //out.method(); //================&#
    转载请注明原文地址: https://ju.6miu.com/read-1299507.html
    最新回复(0)