package polymorphismp2;
public class YinLiao {
public String getYinLiao() {
return null;
}
}
package polymorphismp2;
public class XueBi extends YinLiao {
public String getYinLiao() {// 重写了父类方法
return "雪碧";
}
}
package polymorphismp2;
public class Shui extends YinLiao {
public String getYinLiao() {// 重写了父类方法
return "瓶装水";
}
}
package polymorphismp2;
public class KeLe extends YinLiao {
public String getYinLiao() {// 重写了父类方法
return "可乐";
}
}
package polymorphismp2;
public class ShouHuoJi {
// 1.可乐 2.雪碧 3.瓶装水
//简单工厂模式:按需返回产品
public YinLiao work(int type) {//体现多态,且必须放父类类型
YinLiao y1 = null;
switch (type) {
case 1:
y1 = new KeLe();
break;
case 2:
y1 = new XueBi();
break;
case 3:
y1 = new Shui();
break;
}
return y1;
}
}
package polymorphismp2;
public cl