接口遥控器
package Hello.lrx.main;
public interface Counter {
void open();
}
玩具遥控器
package Hello.lrx.main;
public class ToyCounter implements Counter{
@Override
public void open() {
System.out.println("按下玩具遥控器");
}
}
构建一个TV类
package Hello.lrx.main;
public abstract class TV {
abstract void display(Counter counter);
}
TV类的子类小米电视
package Hello.lrx.main;
public class XaoMiTV extends TV{
@Override
void display(Counter counter) {
counter.open();
System.out.println("打开小米电视显示屏");
}
}
用户
package Hello.lrx.main;
public class User {
private Counter counter;
private TV tv;
public void setCounter(Counter counter,TV tv) {
this.counter = counter;
this.tv = tv;
}
public void operate(Counter counter,TV tv){
tv.display(counter);
}
}
客户端实现
package Hello.lrx.main;
public class Client {
public static void main(String[] args) {
TV xmtv =new XaoMiTV();
Counter toycounter = new ToyCounter();
User user = new User();
user.setCounter(toycounter,xmtv);
user.operate(toycounter,xmtv);
}
}