Java |
您所在的位置:网站首页 › Java如何引用类 › Java |
内部类: 将一个类定义在另一个类的里面,对里面那个类就称为内部类(内置类,嵌套类)。 访问特点:• 内部类可以直接访问外部类中的成员,包括私有成员。 • 而外部类要访问内部类中的成员必须要建立内部类的对象。
内部类的位置: 内部类定义在成员位置上• 可以被private static成员修饰符修饰。 • 被static修饰的内部类只能访问外部类中的静态成员。 内部类定义在局部位置上• 也可以直接访问外部类中的成员。 • 同时可以访问所在局部中的局部变量,但必须是被final修饰的。
内部类的访问规则: 1,内部类可以直接访问外部类中的成员,包括私有。 之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用, 格式 外部类名.this 2,外部类要访问内部类,必须建立内部类对象。 (当内部类在外部类的实例域时,内部类可以被private修饰) class Outer { private int x = 3; class Inner//内部类 { int x = 4;//默认转换为final变量 void function() { int x = 6; System.out.println("innner :" + Outer.this.x); //x = 6; this.x = 4; Outer.this.x = 3; } } void method() { Inner in = new Inner(); in.function(); } } class InnerClassDemo { public static void main(String[] args) { //访问内部类成员方法一 Outer out = new Outer(); outhod();//通过在外部内建立方法,在其中建立内部类对象。间接访问内部类中方法。 //访问内部类成员方法二 Outer.Inner in = new Outer().new Inner(); in.function();//通过直接建立内部类的对象访问其方法 } } /* innner :3 innner :3 */
访问格式: 1,当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中。 可以直接建立内部类对象。 格式 外部类名.内部类名 变量名 = 外部类对象.内部类对象; Outer.Inner in = new Outer().new Inner(); 2,当内部类在成员位置上,就可以被成员修饰符所修饰。 比如,private:将内部类在外部类中进行封装。 static:内部类就具备static的特性。 当内部类被static修饰后,只能直接访问外部类中的static成员。出现了访问局限。 在外部其他类中,如何直接访问static内部类的非静态成员呢? new Outer.Inner().function(); 在外部其他类中,如何直接访问static内部类的静态成员呢? Outer.Inner.function(); 注意:当内部类中定义了静态成员,该内部类必须是static的。 当外部类中的静态方法访问内部类时,内部类也必须是static的。 //static不能修饰一般类,但是可以修饰内部类 static class StaticDemo //static不能修饰外部类 { public static void show() { System.out.println("static class..."); } } class InnerClassDemo2 { public static void main(String[] args) { StaticDemo sd = new StaticDemo(); sd.show(); } } /* Exception in thread "main" java.lang.Error: 无法解析的编译问题: 类 StaticDemo 的修饰符不合法;只允许使用“公用”、“抽象”和“终态” */ class Outer { private static int x = 3; static class Inner//静态内部类 { static void function() { System.out.println("innner :"+x); } } static class Inner2//静态类中可以定义静态方法也可以定义非静态方法; {//但是含有静态方法的类一定要定义为静态类,除非是顶层类。 //只能在静态类型和顶层类型中定义静态方法。 void show() { System.out.println("inner2 show"); } } public static void method() { new Inner(); //在外部类内的方法中可以实例化静态内部类 Inner.function();//静态内部类可以类名调用 // Inner2.show(); //不能对非静态方法进行类名访问 new Inner2().show();//非静态方法需要创建对象后访问 } } class InnerClassDemo2 { public static void main(String[] args) { Outer.method(); } } /* innner :3 inner2 show */ class Outer { private static int x = 3; static class Inner//静态内部类 { static void function() { System.out.println("innner :"+x); } } static class Inner2 { void show() { System.out.println("inner2 show"); } } public static void method() { new Inner(); //在外部类内的方法中可以实例化静态内部类 Inner.function();//静态内部类可以类名调用 // Inner2.show(); //不能对非静态方法进行类名访问 new Inner2().show();//非静态方法需要创建对象后访问 } } class InnerClassDemo2 { public static void main(String[] args) { Outer.method();//调用外部类中的方法 Outer.Inner.function(); // Outer.Inner2.show();//不能通过类名调用非静态方法 new Outer.Inner().function(); //警告:应该以静态的方式访问类型Outer.Inner中的静态方法function() } } /* innner :3 inner2 show innner :3 innner :3 */ //静态内部类通过new Outer.Inner();来创建对象 class Outer { private static int x = 3; static class Inner//静态内部类 { static void function() { System.out.println("innner :"+x); } } static class Inner2//静态类与其内方法是否静态无关 { void show() { System.out.println("inner2 show"); } } public static void method() { new Inner(); //在外部类内的方法中可以实例化静态内部类 Inner.function();//静态内部类可以类名调用 // Inner2.show(); //不能对非静态方法进行类名访问 new Inner2().show();//非静态方法需要创建对象后访问 } } class InnerClassDemo2 { public static void main(String[] args) { // Outer.method();//调用外部类中的方法 // Outer.Inner.function(); // Outer.Inner2.show();//不能通过类名调用非静态方法 new Outer.Inner().function(); Outer.Inner in = new Outer.Inner();//实例化静态内部类 in.function(); Outer.Inner2 in2 = new Outer.Inner2();//实例化静态内部类 in2.show(); } } /* innner :3 innner :3 inner2 show */
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |