继承 封装、多态集合的小练习

编写程序实现披萨的制作。需求说明编写程序,接收输入信息,选择需要制作的披萨。可供选择的披萨有:培根披萨和海鲜披萨。
实现思路及关键代码
1)分析培根披萨和海鲜披萨。
2)定义披萨类(作为父类)。
属性: 名称、价格、大小;
方法:展示哦(show);
3)定义培根披萨和海鲜披萨,分别继承披萨类。
4)定义披萨工厂类,根据输入信息生产具体的披萨对象。
运行结果如下图所示
这里写图片描述
这里写图片描述
创建父类 pizza类


/**
*
* @author Dwarf
*创建父类。
*/
public class Pizza {
private String name;
private double price ;
private int size ;
//创建get set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
//创建构造器,以供子类重载,空构造器一定要有
public Pizza() {
super();
}
//创建构造方法,方便子类重载方法
public Pizza(String name, double price, int size) {
super();
this.name = name;
this.price = price;
this.size = size;
}
//创建一个展示的方法
public String show (){
return “我是父类的展示方法,你们可以随意重写我”;
}

}

———-

创建子类培根披萨:
/**
*
* @author Dwarf
*海鲜披萨
*/
public class BaconPizza extends Pizza {
private int gram;//克重

//创建set get方法
public int getGram() {
return gram;
}

public void setGram(int gram) {
this.gram = gram;
}
//创建构造器,空构造器一般都是会写出来的

public BaconPizza() {
super();
}

//创建带三个父类属性和一个子类属性的构造方法,以备后面重载
public BaconPizza(String name, double price, int size, int gram) {
super(name, price, size);
this.gram = gram;
}

//重写父类的show方法
public String show (){
return “名称:”+getName()+”\n价格:”+getPrice()+”\n大小:”+getSize()+”\n培根克数:”+gram;
}

}

———-

创建子类、:海鲜披萨
/**
*
* @author Dwarf
*海鲜披萨
*/
public class SeeFoodPizza extends Pizza {
private String ingredients ;//配料、
//创建set get方法

public String getIngredients() {
return ingredients;
}

public void setIngredients(String ingredients) {
ingredients = ingredients;
}
//创建构造方法

public SeeFoodPizza() {
super();
}

//创建带三个父类属性和一个子类属性的构造方法,以备后面重载
public SeeFoodPizza(String name, double price, int size, String ingredients) {
super(name, price, size);
this.ingredients = ingredients;
}

//重写父类的show方法
public String show (){
return “价格:”+getPrice()+”大小:\n”+getSize()+”配料:\n”+ingredients;
}

}

———-

创建披萨工厂类
/**
*
* @author Dwarf
*工厂类
*/
import java.util.Scanner;//导入Scanner包

public class PizzaFactory {
private int choice;
//创建一个方法
public static Pizza getPizza(int choice){//父类作方法的返回值,返回的是子类
Pizza p = null;
Scanner sc = new Scanner(System.in);//创建扫描器

switch(choice){
case 1:

    System.out.println("请输入培根克数:");
    int gram = sc.nextInt();
    System.out.println("请输入披萨的大小:");
    int size = sc.nextInt();
    System.out.println("请输入披萨价格:");
    double price = sc.nextDouble();
    //调用有参构造方法并赋值
p = new  BaconPizza("培根披萨", price, size, gram);

break;

case 2:
System.out.println(“请输入配料信息”);
String ingredients = sc.next();
System.out.println(“请输入披萨的大小:”);
size = sc.nextInt();
System.out.println(“请输入披萨的 价格:”);
price = sc.nextDouble();
p= new SeeFoodPizza(“海鲜披萨”, price, size, ingredients);

    break;
}
return p;
}

}

———-

创建测试类
package com.bjsxt.com.test3;
/**
*
* @author Dwarf
*测试类
*/
import java.util.Scanner;//导入Scanner 包

public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//创建扫描器
System.out.println(“请选择您要制作的披萨:1.培根披萨 2.海鲜披萨”);
int choice = sc.nextInt();
//类名直接调用类中的方法
Pizza p = PizzaFactory.getPizza(choice);
//打印输出结果
System.out.println(p.show());
}
}
小生不才,初学JAVA,有不足之处,请各位大佬指教。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章