`
windy2coast
  • 浏览: 54212 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java笔记二

    博客分类:
  • Java
阅读更多
java 代码
  1. //*孙鑫教程笔记。接口中run()方法与基类中的run()方法重名。    
  2. //通过内部类实现接口避免重名冲突冲突   
  3. interface Machine {   
  4.  void run();   
  5. }   
  6. class Person {   
  7.  void run() {   
  8.   System.out.println("person run");   
  9.  }   
  10. }   
  11. class Robot extends Person {   
  12.  class MachineHeat implements Machine {//内部类实现接口   
  13.   public void run() {   
  14.    System.out.println("heart run");   
  15.   }   
  16.  }   
  17.  Machine getMachine() {          //返回内部类引用   
  18.   return new MachineHeat();   
  19.  }   
  20.     
  21. }   
  22. public class Test2 {   
  23.  public static void main(String[] args) {   
  24.   Robot robot  = new Robot();   
  25.   Machine m = robot.getMachine();   
  26.   m.run();//通过内部类引用调用内部类方法run(),即实现的Machine接口中的run()方法   
  27.   robot.run();   
  28.  }   
  29. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics