📄 customer.java
字号:
package simple;
//饺子共性的特点
interface JiaoZi{
//饺子价格
public void jiaoZiValue();
//饺子外形
public void jiaoZiShape();
}
//------------------------------------------------------------------------------------------
//防止输入没的饺子
class DefaultValue extends Exception{
DefaultValue(String s){
super(s);
}
}
//-------------------------------------------------------------------------------------
//牛肉饺子
class BeefJiaoZi implements JiaoZi{
BeefJiaoZi(){
printChar();
jiaoZiValue();
jiaoZiShape();
};
//下面设置了牛肉饺子的特性
public void jiaoZiValue(){
System.out.println("牛肉饺子的价格为1元一斤");
};
public void jiaoZiShape(){
System.out.println("牛肉饺子的馅为牛肉的");
};
//打印出字符串
public void printChar(){
System.out.println("您点的是牛肉饺子");
System.out.println("-----------------------------------------------");
}
}
//------------------------------------------------------------------------------------------
//蔬菜饺子
class VegetableJiaoZi implements JiaoZi{
VegetableJiaoZi(){
jiaoZiValue();
jiaoZiShape();
};
//下面设置了蔬菜饺子的特性
public void jiaoZiValue(){
System.out.println("蔬菜饺子的价格为2元一斤");
};
public void jiaoZiShape(){
System.out.println("蔬菜饺子的馅为白菜");
};
//打印出字符串
public void printChar(){
System.out.println("您点的是蔬菜饺子");
System.out.println("-----------------------------------------------");
}
}
//------------------------------------------------------------------------------------------
//作饺子的饺子店这时便相当于一个做饺子的工厂
class Factory{
//定义了一个判断所要的饺子的口味
public static JiaoZi simpleFactory(String sType) throws DefaultValue{
//当要的是牛肉饺子时就制作牛肉饺子
if (sType.equalsIgnoreCase("beef")){
return new BeefJiaoZi();
}
else {
//当判断的是要蔬菜饺子时就给制作一个蔬菜饺子给顾客
if(sType.equalsIgnoreCase("vegetable")){
return new VegetableJiaoZi();
}
//当选择的是其它类型时就输出请您重输
else throw new DefaultValue("您要的东西本店没有请您重输") ;
}
}
}
//------------------------------------------------------------------------------------------
//下面设置了一个顾客
public class Customer {
public static void main (String arg[]){
try{
//当顾客要的是牛饺子时
Factory.simpleFactory("beef");
//当顾客要蔬菜饺子时
// Factory.simpleFactory("vegetable");
//当顾客要的是不是饺子时
// Factory.simpleFactory("milk");
}catch(DefaultValue e){
System.out.println(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -