java内部类实例讲解图解

目录(?)[-]

  1. 内部类
    1. 静态内部类
      1. 访问格式
    2. 匿名内部类
    3. 匿名内部类
    4. TEST

      内部类

      java内部类实例讲解图解,内部类:将一个类定义在另一个类的里面,对立面那个类就称为内部类(内置类,嵌套类)。

      内部类 访问特点:

      ·        内部类可以直接访问外部类中的成员,包括私有成员。

      ·        而外部类要访问内部类中的成员必须要建立内部类的对象。

      之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,该引用写法是:外部类名.this

      //外部类名.内部类名 对象名 = new 外部类().new 内部类();

      //对象名.内部类方法();

      l  Code

      [java] view plaincopy

      1. class Outer
      2. {
      3.          private int x = 3;
      4. //内部类
      5. class Innter
      6. {
      7.                  int x = 4;
      8. void function()
      9. {
      10. int x = 5;
      11. System.out.println(“Inner:”+Outer.this.x);
      12. }
      13. }
      14. void method()
      15. {
      16. Innter in = new Innter();
      17.           in.function();
      18. System.out.println(x);
      19. }
      20. }
      21. class InnerClassDemo
      22. {
      23. public static void main(String[] args)
      24. {
      25. //Outer ou = new Outer();
      26. //ou.method();
      27. //外部类名.内部类名 对象名 = new 外部类().new 内部类();
      28. //对象名.内部类方法();
      29. Outer.Innter inn = new Outer().new Innter();
      30. inn.function();
      31. }
      32. }

      静态内部类

      访问格式

      1、当内部类定义在外部类的成员位置上,而且非私有,可以在外部其他类中,可以直接建立内部类对象。

      格式:

      外部类名.内部类名      变量名  = 外部类对象.内部类对象;

      Outer.Innter inn =new Outer().new Innter();

      2、当内部类在成员位置上,就可以被成员修饰符所修饰。

            比如,private :将内部类在外部类中进行封装

              static :内部类就具备了静态特性。

      当内部类被静态修饰后,只能直接访问外部类中的static成员,出现了访问局限。

      注意:当内部类中定义了静态成员,那么该内部类必须是static的。
      当外部类中的静态方法访问内部类时,内部类也必须是静态的。
      内部类定义原则
      类:描述现实中事物。

      当描述事物时,事物的内部还有事物,该事物用内部类来描述。
      因为内部事物在使用外部事物的内容。

      code:

      [java] view plaincopy

      1. class Outer
      2. {
      3.          private static int x = 3;
      4. //内部类
      5. static class Innter
      6. {
      7.                  int x = 4;
      8. static void function()
      9. {
      10. //int x = 5;
      11. //System.out.println(“Inner:”+Outer.this.x);
      12. System.out.println(“Inner:”);
      13. }
      14. }
      15. static class Inner2
      16. {
      17. void show()
      18. {
      19. System.out.println(“Inner2 show”);
      20. }
      21. }
      22. public static void method()
      23. {
      24. new Inner2().show();
      25. Innter.function();
      26. //                Innter in = new Innter();
      27. //                  in.function();
      28. //                System.out.println(x);
      29. }
      30. }
      31. class InnerClassDemo2
      32. {
      33. public static void main(String[] args)
      34. {
      35. Outer.method();
      36. //new Outer.
      37. //访问静态内部类的非静态成员
      38. //new Outer.Innter().function();
      39. //访问静态内部类的静态成员
      40. //Outer.Innter.function();
      41. //Outer ou = new Outer();
      42. //ou.method();
      43. //外部类名.内部类名 对象名 = new 外部类().new 内部类();
      44. //对象名.内部类方法();
      45. //Outer.Innter inn = new Outer().new Innter();
      46. //inn.function();
      47. }
      48. }

      匿名内部类

      1、匿名内部类其实就是内部类的简写格式。——简化书写,覆盖方法。

       2、匿名内部类的前提:

      内部类必须是继承一个类或者实现接口

      3、匿名内部类的格式:new 父类或者接口 () { 定义子类的类容 }

      4、其实匿名内部类就是一个子类对象。(把‘定义类’和‘建立对象’

      封装为一体的表现形式),可以理解为带内容的对象 。

      5、匿名内部类中定义的方法最好不要超过3个。

      匿名内部类

      1、匿名内部类其实就是内部类的简写格式。——简化书写,覆盖方法。

       2、匿名内部类的前提:

      内部类必须是继承一个类或者实现接口

      3、匿名内部类的格式:new 父类或者接口 () { 定义子类的类容 }

      4、其实匿名内部类就是一个子类对象。(把‘定义类’和‘建立对象’

      封装为一体的表现形式),可以理解为带内容的对象 。

      5、匿名内部类中定义的方法最好不要超过3个。

      [java] view plaincopy

      1. {
      2. //newInner().show();
      3. void show()
      4. {
      5. System.out.println(“x=”+x);
      6. }
      7. }.show();

       

      匿名内部类弊端:

      1、不能直接调用自己的方法

      2、匿名内部类里的方法不会超过3个,顶多2个或1个,

      [java] view plaincopy

      1. abstract class AbsDemo
      2. {
      3. abstract void show();
      4. }
      5. class Outer
      6. {
      7. int x = 3;
      8. class Inner extends AbsDemo
      9. {
      10. void show()
      11. {
      12. System.out.pirintl(“show :” + x);
      13. }
      14. }
      15. }
      16. public void function()
      17. {
      18. new AbsDemo()
      19. {
      20. //new Inner().show();
      21. void show()
      22. {
      23. System.out.println(“x=”+x);
      24. }
      25. }.show();
      26. }

      TEST

      [java] view plaincopy

      1. interface Inter
      2. {
      3. void method();//method方法是非静态的,因为他是抽象的。
      4. }
      5. class Test
      6. {
      7. //补全代码,通过匿名内部类
      8. /*
      9. static class Inner implements Inter
      10. {
      11. public void method()
      12. {
      13. System.out.println(“method run”);
      14. }
      15. }
      16. */
      17. static Inter function()//首先,是静态的,参数列表知道——’空参数’,
      18. {
      19. return new Inter() //接口不能new对象
      20. {
      21. public void method()
      22. {
      23. System.out.println(“method run”);
      24. }
      25. };
      26. }
      27. }
      28. class InnerClassTest
      29. {
      30. public static void main(String[] args)
      31. {
      32. Test.function().method();//得先看懂这句话
      33. //Test(类名).XXX可以看出,这个类中肯定有静态成员,成员名叫function(是个函数), 从“()”看出
      34. //【Test.function().】运算完,还能调用method,毫无疑问function方法运算后的结果应该是个对象,
      35. //只有返回来对象,对象才能调用method的方法,method一定要被对象调用。
      36. //什么对象能调用method?—–>必然是Inter。
      37. //Inter不能new对象
      38. //Test.function(): Test类中有一个静态的方法叫做function。
      39. //.method(): function这个方法运算后的结果是一个对象,而且是一个Inter类型的对象,
      40. //因为只有是Inter类型的对象才可以调用method方法
      41. show(new Inter()
      42. {
      43. public void method()
      44. {
      45. System.out.println(“method show run”);
      46. }
      47. });
      48. }
      49. public static void show(Inter in)
      50. {
      51. in.method();
      52. }
      53. }
本文链接地址: java内部类实例讲解图解