• Java类部类


    内部类(累不累)

    分类

    定义在局部位置:
    1. 局部内部类(有类名)
    2. 匿名内部类(无类名)
    局部内部类
    1. 不能添加访问修饰符,但可以使用final修饰
    2. 只作用于方法体或者代码块中
    3. 地位等同于局部变量
    4. 当局部类属性和外部类属性重名,遵循就近原则,若想访问外部类,可以用外部类名.this.属性名访问
    public class Main {
        public static void main(String[] args) {
            Outer outer = new Outer();
            outer.method1();
        }
    }
    
    class Outer {
        int x = 200;
    
        void method1() {
            int x = 100;
            System.out.println(this);
            System.out.println(Integer.toHexString(hashCode()));
            class Inner {
                class H{
                    H() {
                        new B();
                    }
                    class B{
                        B() {
                            new C();
                        }
                        class C{
                            C() {
                                new D();
                            }
                            class D {
                                D() {
                                    System.out.println(this);
                                }
                            }
                        }
                    }
                }
                void f() {
                    System.out.println("x=" + x);
                    System.out.println("Outer.this.x=" + Outer.this.x);
                    new H();
                }
            }
            new Inner().f();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    貌似局部内部类可以一直套下去

    匿名内部类
    1. 本质是类
    2. 内部类
    3. 该类无类名
    4. 在创建时new出对象
    
    public class Main {
        public static void main(String[] args) {
            // 接口的匿名类
            Ixx obj = new Ixx() {
                @Override
                public void cry() {
                    System.out.println(this);
                    System.out.println("kid crying.....");
                }
            };
            obj.cry();
            System.out.println("obj的运行类型为:" + obj.getClass());
    
            A a = new A() {
                @Override
                void method() {
                    super.x = 100;
                    System.out.println(this.x);
                }
            };
            a.method();
            System.out.println("a的运行类型为:" + a.getClass());
        }
    }
    
    // anonymous 匿名的
    class A {
        int x;
        void method() { }
    }
    
    interface Ixx {
        void cry();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    定义在外部类的成员位置:
    1. 成员内部类(没有static修饰)
    2. 静态内部类(有stati修饰)
    成员内部类

    1.成员内部类定义在外部类属性的位置
    2.可用(public, protected, private)修饰,因为它的地位就像一个属性
    3.只能通过对象访问成员内部类

    
    public class Main {
        public static void main(String[] args) {
            Outer outer = new Outer();
            Outer.Inner inner = outer.new Inner(); // 创建成员内部类对象
            Outer.Inner inner1 = outer.getInner(); // 直接用方法创建 
            System.out.println(inner.x); // 访问内部类的属性
            inner.method(); // 使用内部类的方法
        }
    }
    
    class Outer {
        public int i;
        protected int j;
        int k;
        class Inner {
            int x = 100;
            void method() {
                System.out.println(x);
            }
        }
        Inner getInner() {
            return new Inner();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    静态内部类

    1.类名用static修饰
    2.可以访问外部类的静态成员
    3.可任意加访问修饰符,无限制
    4.只能通过类名访问静态内部类

    public class Main {
        public static void main(String[] args) {
            Outer outer = new Outer();
            Outer.Inner inner = new Outer.Inner(); // 静态类直接外部类.内部类new
        }
    }
    
    class Outer {
        static String str = "hello";
        int x = 100;
        static class Inner {
            static{
                System.out.println(str);
                System.out.println("hhhhh");
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    非同质化权益(NFR):重塑数字资产权益的生态系统
    2022年湖北武汉建筑安全员ABC证怎么考 在哪里可以报名 ?
    C语言中的位域
    如何在商业网络平台当中,建立良好的销售业绩?
    谷歌浏览器网页显示不完整解决方法
    VMware 虚拟化整体方案替代:一举实现架构信创 + 云化转型
    netty系列之:netty中的自动解码器ReplayingDecoder
    心行知合一
    Python基础学习016__UnitTest
    手把手教你GPT4.0画画
  • 原文地址:https://blog.csdn.net/weixin_46211066/article/details/127589116