目录(?)[-]
- 内部类
- 静态内部类
- 访问格式
- 匿名内部类
- 匿名内部类
- TEST
内部类
java内部类实例讲解图解,内部类:将一个类定义在另一个类的里面,对立面那个类就称为内部类(内置类,嵌套类)。
内部类 访问特点:
· 内部类可以直接访问外部类中的成员,包括私有成员。
· 而外部类要访问内部类中的成员必须要建立内部类的对象。
之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,该引用写法是:外部类名.this
//外部类名.内部类名 对象名 = new 外部类().new 内部类();
//对象名.内部类方法();
l Code
[java] view plaincopy- class Outer
- {
- private int x = 3;
- //内部类
- class Innter
- {
- int x = 4;
- void function()
- {
- int x = 5;
- System.out.println(“Inner:”+Outer.this.x);
- }
- }
- void method()
- {
- Innter in = new Innter();
- in.function();
- System.out.println(x);
- }
- }
- class InnerClassDemo
- {
- public static void main(String[] args)
- {
- //Outer ou = new Outer();
- //ou.method();
- //外部类名.内部类名 对象名 = new 外部类().new 内部类();
- //对象名.内部类方法();
- Outer.Innter inn = new Outer().new Innter();
- inn.function();
- }
- }
静态内部类
访问格式
1、当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中,可以直接建立内部类对象。
格式:
外部类名.内部类名 变量名 = 外部类对象.内部类对象;
Outer.Innter inn =new Outer().new Innter();
2、当内部类在成员位置上,就可以被成员修饰符所修饰。
比如,private :将内部类在外部类中进行封装
static :内部类就具备了静态特性。
当内部类被静态修饰后,只能直接访问外部类中的static成员,出现了访问局限。
注意:当内部类中定义了静态成员,那么该内部类必须是static的。
当外部类中的静态方法访问内部类时,内部类也必须是静态的。
内部类定义原则
类:描述现实中事物。当描述事物时,事物的内部还有事物,该事物用内部类来描述。
因为内部事物在使用外部事物的内容。code:
[java] view plaincopy- class Outer
- {
- private static int x = 3;
- //内部类
- static class Innter
- {
- int x = 4;
- static void function()
- {
- //int x = 5;
- //System.out.println(“Inner:”+Outer.this.x);
- System.out.println(“Inner:”);
- }
- }
- static class Inner2
- {
- void show()
- {
- System.out.println(“Inner2 show”);
- }
- }
- public static void method()
- {
- new Inner2().show();
- Innter.function();
- // Innter in = new Innter();
- // in.function();
- // System.out.println(x);
- }
- }
- class InnerClassDemo2
- {
- public static void main(String[] args)
- {
- Outer.method();
- //new Outer.
- //访问静态内部类的非静态成员
- //new Outer.Innter().function();
- //访问静态内部类的静态成员
- //Outer.Innter.function();
- //Outer ou = new Outer();
- //ou.method();
- //外部类名.内部类名 对象名 = new 外部类().new 内部类();
- //对象名.内部类方法();
- //Outer.Innter inn = new Outer().new Innter();
- //inn.function();
- }
- }
匿名内部类
1、匿名内部类其实就是内部类的简写格式。——简化书写,覆盖方法。
2、匿名内部类的前提:
内部类必须是继承一个类或者实现接口
3、匿名内部类的格式:new 父类或者接口 () { 定义子类的类容 }
4、其实匿名内部类就是一个子类对象。(把‘定义类’和‘建立对象’
封装为一体的表现形式),可以理解为带内容的对象 。
5、匿名内部类中定义的方法最好不要超过3个。
匿名内部类
1、匿名内部类其实就是内部类的简写格式。——简化书写,覆盖方法。
2、匿名内部类的前提:
内部类必须是继承一个类或者实现接口
3、匿名内部类的格式:new 父类或者接口 () { 定义子类的类容 }
4、其实匿名内部类就是一个子类对象。(把‘定义类’和‘建立对象’
封装为一体的表现形式),可以理解为带内容的对象 。
5、匿名内部类中定义的方法最好不要超过3个。
[java] view plaincopy- {
- //newInner().show();
- void show()
- {
- System.out.println(“x=”+x);
- }
- }.show();
匿名内部类弊端:
1、不能直接调用自己的方法
2、匿名内部类里的方法不会超过3个,顶多2个或1个,
[java] view plaincopy- abstract class AbsDemo
- {
- abstract void show();
- }
- class Outer
- {
- int x = 3;
- class Inner extends AbsDemo
- {
- void show()
- {
- System.out.pirintl(“show :” + x);
- }
- }
- }
- public void function()
- {
- new AbsDemo()
- {
- //new Inner().show();
- void show()
- {
- System.out.println(“x=”+x);
- }
- }.show();
- }
TEST
[java] view plaincopy- interface Inter
- {
- void method();//method方法是非静态的,因为他是抽象的。
- }
- class Test
- {
- //补全代码,通过匿名内部类
- /*
- static class Inner implements Inter
- {
- public void method()
- {
- System.out.println(“method run”);
- }
- }
- */
- static Inter function()//首先,是静态的,参数列表知道——’空参数’,
- {
- return new Inter() //接口不能new对象
- {
- public void method()
- {
- System.out.println(“method run”);
- }
- };
- }
- }
- class InnerClassTest
- {
- public static void main(String[] args)
- {
- Test.function().method();//得先看懂这句话
- //Test(类名).XXX可以看出,这个类中肯定有静态成员,成员名叫function(是个函数), 从“()”看出
- //【Test.function().】运算完,还能调用method,毫无疑问function方法运算后的结果应该是个对象,
- //只有返回来对象,对象才能调用method的方法,method一定要被对象调用。
- //什么对象能调用method?—–>必然是Inter。
- //Inter不能new对象
- //Test.function(): Test类中有一个静态的方法叫做function。
- //.method(): function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象,
- //因为只有是Inter类型的对象才可以调用method方法
- show(new Inter()
- {
- public void method()
- {
- System.out.println(“method show run”);
- }
- });
- }
- public static void show(Inter in)
- {
- in.method();
- }
- }
- 静态内部类