java匿名内部类-细节

 1 package face_09;
 2 
 3 public class InnerClassDemo50 {
 4     static class Inner{
 5         
 6     }
 7     public static void main(String[] args) {
 8         new Inner();//对象所属类必须是静态
 9     }
10     public void method() {
11         new Inner();
12     }
13     public static void show(Inter in) {
14         in.show1();
15         in.show2();
16     }
17 }
View Code
 1 package face_09;
 2 class Outer{
 3     void method() {
 4         Object obj = new Object() {
 5             public  void show() {
 6                 System.out.println("show run");
 7             }
 8         };
 9         obj.show();//因为匿名内部类这个子类对象被向上转型为了Object类型。
10                    //这样就不能再使用子类特有的方法了。
11                    
12     }
13 }
14 public class InnerClassDemo6 {
15     public static void main(String[] args) {
16         new Outer().method();
17     }
18     
19 }
View Code

 

posted @ 2021-10-06 22:45  doremi429  阅读(16)  评论(0)    收藏  举报