内部类
-
内部类就是一个类的内部再定义一个类,比如,A类中定义一个B类,那么B类相对于A类来说就称为内部类,而A类相对于B类来说就是外部类
-
-
成员内部类
-
静态内部类
-
局部内部类
-
匿名内部类
package oop.Dome10; public class Test { public static void main(String[] args) { //没有名字初始化类,不用将实例保存早变量中 new Apple().eat(); new UserService(){ @Override public void hello() { } }; } } class Apple{ public void eat(){ System.out.println("1"); } } interface UserService{ void hello(); }
package oop.Dome10; public class App { public static void main(String[] args) { Outer outer = new Outer(); //通过这个外部类来实例化内部类 Outer.Inner inner = outer.new Inner(); inner.getID(); } }
-