java 代码
- //*孙鑫教程笔记。接口中run()方法与基类中的run()方法重名。
- //通过内部类实现接口避免重名冲突冲突
- interface Machine {
- void run();
- }
- class Person {
- void run() {
- System.out.println("person run");
- }
- }
- class Robot extends Person {
- class MachineHeat implements Machine {//内部类实现接口
- public void run() {
- System.out.println("heart run");
- }
- }
- Machine getMachine() { //返回内部类引用
- return new MachineHeat();
- }
- }
- public class Test2 {
- public static void main(String[] args) {
- Robot robot = new Robot();
- Machine m = robot.getMachine();
- m.run();//通过内部类引用调用内部类方法run(),即实现的Machine接口中的run()方法
- robot.run();
- }
- }