• 16.成员内部类【20220624】


    1.什么是内部类?

    将一个java类定义到另一个类的类体中,那么定义在类体中的类就是内部类
    我们把包含内部类的java类叫外部类

    1. public class Hello{
    2. public class World{
    3. }
    4. }

    Hello---外部类
    World---内部类
    内部类在编译之后会得到独立的内部类的字节码文件【外部类的类名$内部类的类名.class】

    2.内部类的分类?

    1.成员内部类
    2.方法内部类
    3.匿名内部类
    4.静态内部类【静态嵌套类】 

    3.成员内部类---将内部类定义在另一个类的类体中

    public  class  Hello{
        //成员内部类
        public  class  World{

        }
    }

    1.成员内部类中的元素:实例变量,构造方法,实例方法;不能出现静态元素

    1. package com.wangxing.chengyuan.test1;
    2. public class Hello {
    3. //成员内部类
    4. public class World{
    5. public String worldshili="成员内部类中的实例变量";
    6. //不能出现静态变量
    7. //public static String worldstatic="成员内部类中的静态变量";
    8. public World(){
    9. System.out.println("成员内部类中的构造方法");
    10. }
    11. public void worldShiLiMethod(){
    12. System.out.println("成员内部类中的实例方法");
    13. }
    14. //不能出现静态方法
    15. //public static void worldStaticMethod(){
    16. // System.out.println("成员内部类中的静态方法");
    17. //}
    18. }
    19. }

    2.成员内部类中可以访问成员内部类中的实例变量/实例方法,构造方法
       2.1.对实例变量/实例方法的访问,对象.实例变量/实例方法,this.实例变量/实例方法,对象和this可以省略
       2.2.对构造方法的访问,通过new访问。

     

    1. package com.wangxing.chengyuan.test1;
    2. public class Hello2 {
    3. //成员内部类
    4. public class World2{
    5. public String worldshili="成员内部类中的实例变量";
    6. public World2(){
    7. System.out.println("成员内部类中的构造方法");
    8. }
    9. public World2(String name){
    10. World2 w2=new World2();
    11. System.out.println(w2.worldshili);
    12. System.out.println(this.worldshili);
    13. System.out.println(worldshili);
    14. w2.worldShiLiMethod1();
    15. this.worldShiLiMethod1();
    16. worldShiLiMethod1();
    17. }
    18. public void worldShiLiMethod1(){
    19. System.out.println("成员内部类中的实例方法");
    20. }
    21. public void worldShiLiMethod2(){
    22. System.out.println("成员内部类中的实例方法");
    23. World2 w2=new World2();
    24. System.out.println(w2.worldshili);
    25. System.out.println(this.worldshili);
    26. System.out.println(worldshili);
    27. w2.worldShiLiMethod1();
    28. this.worldShiLiMethod1();
    29. worldShiLiMethod1();
    30. }
    31. }
    32. }

    3.成员内部类中可以访问外部类的实例变量/实例方法,静态变量/静态方法,构造方法
       3.1.对实例变量/实例方法的访问,对象.实例变量/实例方法,外部类名.this.实例变量/实例方法,对象/外部类名.this可以省略。
       3.2.对静态变量/静态方法的访问,对象.静态变量/静态方法,外部类名.this.静态变量/静态方法,外部类类名.静态变量/静态方法,对象/外部类名.this/外部类类名可以省略。        
       3.3.对构造方法访问,通过new访问

    1. package com.wangxing.chengyuan.test1;
    2. public class Hello3 {
    3. public String helloshili="外部类中的实例变量";
    4. public static String hellostatic="外部类中的静态变量";
    5. public Hello3(){
    6. System.out.println("外部类的构造方法");
    7. }
    8. public void helloShiLi(){
    9. System.out.println("外部类的实例方法");
    10. }
    11. public static void helloStatic(){
    12. System.out.println("外部类的静态方法");
    13. }
    14. //成员内部类
    15. public class World3{
    16. public World3(){
    17. Hello3 h3=new Hello3();
    18. System.out.println(h3.helloshili);
    19. System.out.println(Hello3.this.helloshili);
    20. System.out.println(helloshili);
    21. System.out.println(h3.hellostatic);
    22. System.out.println(Hello3.this.hellostatic);
    23. System.out.println(Hello3.hellostatic);
    24. System.out.println(hellostatic);
    25. h3.helloShiLi();
    26. Hello3.this.helloShiLi();
    27. helloShiLi();
    28. h3.helloStatic();
    29. Hello3.this.helloStatic();
    30. Hello3.helloStatic();
    31. helloStatic();
    32. }
    33. public void worldShiLiMethod1(){
    34. Hello3 h3=new Hello3();
    35. System.out.println(h3.helloshili);
    36. System.out.println(Hello3.this.helloshili);
    37. System.out.println(helloshili);
    38. System.out.println(h3.hellostatic);
    39. System.out.println(Hello3.this.hellostatic);
    40. System.out.println(Hello3.hellostatic);
    41. System.out.println(hellostatic);
    42. h3.helloShiLi();
    43. Hello3.this.helloShiLi();
    44. helloShiLi();
    45. h3.helloStatic();
    46. Hello3.this.helloStatic();
    47. Hello3.helloStatic();
    48. helloStatic();
    49. }
    50. }
    51. }

    4.外部类中可以访问成员内部类中的元素
       4.1.外部类中的构造方法/实例方法中可以访问成员内部类中的实例变量/实例方法,构造方法,只能对象访问,不能省略。
       4.2.在外部类中的静态方法中不能访问成员内部类中的元素。

    1. package com.wangxing.chengyuan.test1;
    2. public class Hello4 {
    3. public String helloshili="外部类中的实例变量";
    4. public static String hellostatic="外部类中的静态变量";
    5. public Hello4(){
    6. System.out.println("外部类的构造方法");
    7. World4 w4=new World4();
    8. System.out.println(w4.worldshili);
    9. w4.worldShiLiMethod1();
    10. }
    11. public void helloShiLi(){
    12. System.out.println("外部类的实例方法");
    13. World4 w4=new World4();
    14. System.out.println(w4.worldshili);
    15. w4.worldShiLiMethod1();
    16. }
    17. public static void helloStatic(){
    18. System.out.println("外部类的静态方法");
    19. //World4 w4=new World4();
    20. //System.out.println(w4.worldshili);
    21. //w4.worldShiLiMethod1();
    22. }
    23. //成员内部类
    24. public class World4{
    25. public String worldshili="成员内部类中的实例变量";
    26. public World4(){
    27. System.out.println("成员内部类中的构造方法");
    28. }
    29. public void worldShiLiMethod1(){
    30. System.out.println("成员内部类中的实例方法");
    31. }
    32. }
    33. }

    5.其他类中访问成员内部中的元素
         1.外部类名.内部类名 w1=new 外部类构造方法().new 内部类构造方法();

         2.外部类名 外部类对象=new 外部类构造方法();
            外部类名.内部类名 w1=外部类对象.new 内部类构造方法();

         3.import  包名.外部类名.内部类名;
                3.1 内部类名 w1=new 外部类构造方法().new 内部类构造方法();
                3.2 外部类名 外部类对象=new 外部类构造方法();
                      内部类名 w1=外部类对象.new 内部类构造方法();

    1. package com.wangxing.chengyuan.test1;
    2. public class Hello {
    3. //成员内部类
    4. public class World{
    5. public String worldshili="成员内部类中的实例变量";
    6. public World(){
    7. System.out.println("成员内部类中的构造方法");
    8. }
    9. public void worldShiLiMethod(){
    10. System.out.println("成员内部类中的实例方法");
    11. }
    12. }
    13. }
    14. package com.wangxing.chengyuan.test1;
    15. import com.wangxing.chengyuan.test1.Hello.World;
    16. public class Other {
    17. public Other(){
    18. Hello.World w1=new Hello().new World();
    19. System.out.println(w1.worldshili);
    20. w1.worldShiLiMethod();
    21. }
    22. public void shili(){
    23. Hello hello=new Hello();
    24. Hello.World w1=hello.new World();
    25. System.out.println(w1.worldshili);
    26. w1.worldShiLiMethod();
    27. }
    28. public static void staticmethod(){
    29. Hello hello=new Hello();
    30. World w1=hello.new World();
    31. System.out.println(w1.worldshili);
    32. w1.worldShiLiMethod();
    33. }
    34. }
  • 相关阅读:
    Fortify白盒神器20.1.1下载及安装(非百度网盘)
    Python爬虫怎么挣钱?解析Python爬虫赚钱方式,轻轻松松月入两万,再也不用为钱发愁啦
    线程安全,,Maven基本介绍,220822,,
    微信小程序多端应用 Donut 多端编译
    02_LinuxLED驱动开发
    JVM常用内存参数配置
    基于JSP+Servlet的医疗设备管理系统
    Cocos学习日记2——脚本和属性
    新能源车补能;小鹏、蔚来各有千秋
    vue自动跳转模拟登陆
  • 原文地址:https://blog.csdn.net/guizhaiteng/article/details/125448933