标签:tor class xtend 定义 功能 end static out 需要
代码实例
/**
* 基础组件
*/
abstract class Beverage {
private String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public abstract double cost();
}
/**
* 装饰器基类
*/
abstract class Decorator extends Beverage {
@Override
public abstract String getDescription();
}
/**
* 被装饰的类
*/
class Espresso extends Beverage {
public Espresso() {
setDescription("Espresso");
}
@Override
public double cost() {
return 1.99;
}
}
/**
* 装饰器
*/
class Mocha extends Decorator {
private Beverage beverage;
public Mocha(Beverage beverage) {
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.29;
}
@Override
public String getDescription() {
return beverage.getDescription() + ", Mocha";
}
}
class CoffeeTest {
public static void main(String[] args) {
Beverage beverage = new Espresso();
beverage = new Mocha(beverage);
System.out.println(beverage.getDescription() + " " + beverage.cost());
}
}
标签:tor class xtend 定义 功能 end static out 需要
原文地址:https://www.cnblogs.com/bosslv/p/11359525.html