需求:坦克大战
创建两种坦克
坦克类型 | 射程 | 速度 |
b70 | 70米 | 时/70公里 |
b50 | 50米 | 时/70公里 |
类图
代码
interface ITank{
void templateMethod();
void shotOperation();
void RunOperation();
}
abstract class Tank implements ITank{
public void templateMethod() {
shotOperation();
RunOperation();
}
}
class B70Tank extends Tank{
public void shotOperation() {
System.out.println("射击70米!");
}
public void RunOperation() {
System.out.println("速度70公里");
}
}
class B50Tank extends Tank{
public void shotOperation() {
System.out.println("射击50米!");
}
public void RunOperation() {
System.out.println("速度50公里");
}
}
public class Client {
public static void main(String[] args) {
System.out.println("hello world !");
B50Tank b5 = new B50Tank();
b5.templateMethod();
}
}
运行结果