内部类

image

Application

package com.oop;


import com.oop.demo10.Outer;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //通过外部类来实例化内部类~
        Outer.Inner inner = outer.new Inner();
        inner.in();
        inner.getID();
    }
}

Outer

package com.oop.demo10;

public class Outer {

    private int id = 999;
    public void out(){
        System.out.println("这位是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这位是内部类的方法");
        }
        public void getID(){
            System.out.println(id);
        }

        //局部内部类
        public void method(){
            class Innere{
                public void in(){

                }
            }
        }


    }
}

Test

package com.oop.demo10;

public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不用将实例存到变量中~
        new Apple();
        new Apple().eat();

        new UserService(){
            @Override
            public void hello() {

            }
        };
    }
}
class Apple{
    public void eat(){
        System.out.println("1");
    }

}

interface UserService{
    void hello();
}
posted @ 2026-03-12 22:09  果子同志  阅读(9)  评论(0)    收藏  举报