标签:rri lis res 导致 运行时 public get tco style
public abstract class Beverage { public String describe = "没有描述信息!"; public String getDescribe() { return describe; } public abstract double getCost(); }
public class EspressoBeverage extends Beverage { public EspressoBeverage(String describe) { this.describe = describe; } @Override public double getCost() { return 1.9; } }
public class MochaBeverage extends Beverage { private Beverage beverage; public MochaBeverage(Beverage beverage) { this.beverage = beverage; } @Override public String getDescribe() { return beverage.getDescribe() + ",摩卡"; } @Override public double getCost() { return beverage.getCost() + 2.0; } }
public class MilkBeverage extends Beverage { private Beverage beverage; public MilkBeverage(Beverage beverage) { this.beverage = beverage; } @Override public String getDescribe() { return beverage.getDescribe() + ",加奶"; } @Override public double getCost() { return beverage.getCost() + 0.3; } }
public class Test { public static void main(String[] args) { Beverage espressoBeverage = new EspressoBeverage("正常咖啡"); Beverage mochaBeverage = new MochaBeverage(espressoBeverage); Beverage milkBeverage = new MilkBeverage(mochaBeverage); System.out.println(mochaBeverage.getDescribe()); System.out.println(mochaBeverage.getCost()); System.out.println(milkBeverage.getDescribe()); System.out.println(milkBeverage.getCost()); } } 执行结果: 正常咖啡,摩卡 3.9 正常咖啡,摩卡,加奶 4.2
标签:rri lis res 导致 运行时 public get tco style
原文地址:https://www.cnblogs.com/use-D/p/9749142.html