UML
手机品牌抽象类
public class HandsetBrand {
protected String name;
protected HandsetSoft soft;
public HandsetBrand(String name) {
this.name = name;
}
public void setSoft(HandsetSoft soft) {
this.soft = soft;
}
public void run() {
System.out.print(name+"--->");
soft.run();
}
}
两个具体的手机品牌
public class HandsetBrandA extends HandsetBrand {
public HandsetBrandA(String name) {
super(name);
}
@Override
public void run() {
super.run();
}
}
public class HandsetBrandB extends HandsetBrand {
public HandsetBrandB(String name) {
super(name);
}
@Override
public void run() {
super.run();
}
}
手机软件抽象类
public abstract class HandsetSoft {
public abstract void run();
}
手机通讯录
public class HandsetSoftAddressList extends HandsetSoft {
@Override
public void run() {
System.out.println("运行手机通讯录");
}
}
手机游戏
public class HandsetSoftGame extends HandsetSoft {
@Override
public void run() {
System.out.println("运行手机游戏");
}
}
测试类
public class Main {
public static void main(String[] args) {
HandsetBrand a = new HandsetBrandA("小米MIX3");
a.setSoft(new HandsetSoftAddressList());
a.run();
a.setSoft(new HandsetSoftGame());
a.run();
HandsetBrand b = new HandsetBrandB("华为P20");
b.setSoft(new HandsetSoftAddressList());
b.run();
b.setSoft(new HandsetSoftGame());
b.run();
}
}
运行结果