一、创建普通奶茶类
public class BaseMilkTea extends MilkTea{
@Override
public void make() {
System.out.println("制作一杯普通的奶茶");
}
}
二、小料类
public class Zhenzhu extends MilkTea{
private MilkTea milkTea;
//珍珠要搭配奶茶才可以卖,不能自己独立售卖
public Zhenzhu (MilkTea m){
this.milkTea=m;
}
@Override
public void make() {
milkTea.make();
System.out.println("加珍珠");
}
}
public class Yuyuan extends MilkTea {
private MilkTea m;
public Yuyuan(MilkTea m){
this.m=m;
}
@Override
public void make() {
m.make();
System.out.println("加芋圆");
}
}
三、奶茶类与测试类
设置抽象类和抽象方法
public abstract class MilkTea {
public abstract void make();
}
public class Test {
public static void main(String[] args) {
BaseMilkTea b=new BaseMilkTea();
Zhenzhu z=new Zhenzhu(b);
Yuyuan y=new Yuyuan(z);
y.make();
//1 2 3 4 5
}
}