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. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值