内部类

public class Outer {
   private int id =10;
   public void out(){
       System.out.println("这是外部类的方法");
  }
   public class Inner{
       public void in() {
           System.out.println("这是内部类的方法");
      }
       //获得外部类的私有属性    
       public void getID(){
           System.out.println(id);
      }
  }
}
============================================
public class Application {
   public static void main(String[] args) {
       Outer outer = new Outer();
       //通过外部类来实例化这个内部类
       Outer.Inner inner = outer.new Inner();
       inner.getID();
  }    
}
============================================
public class Outer {
   //局部内部类
   public void method(){
       class Inner{};
  }
}
=============================================
public class Test {
   public static void main(String[] args) {
       //没有名字初始化类,不用将实例保存在变量中
       new Apple().eat();
       //匿名内部类
       new UserSvice(){
           @Override
           public void hell() {

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

interface UserSvice{
   void hell();
}
 
posted @ 2024-01-23 10:50  fightownself  阅读(1)  评论(0编辑  收藏  举报