要求实现
代码示例
父类 —— Pizza类:
package Pizza_Pizza;
//父类:披萨类
public class Pizza {
//属性
private String name;//名称
private int size; //大小
private int price; //价格
//方法
//首先:setter、getter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
//再加一个方法:
//展示披萨信息方法
public String showPizza(){
return "披萨的名字是:"+name+"\n披萨的大小是:"+size+"寸\n披萨的价格是:"+price+"元";
}
//构造器
public Pizza() {
}
public Pizza(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
}
子类 —— FruitsPizza类:
package Pizza_Pizza;
public class FruitsPizza extends Pizza{
//子类特有属性
private String burdenings;
//方法
public String getBurdenings() {
return burdenings;
}
public void setBurdenings(String burdenings) {
this.burdenings = burdenings;
}
//对父类中showPizza()方法的重写
@Override
public String showPizza() {
return super.showPizza()+"\n你要加入的水果:"+burdenings;
}
//构造器
public FruitsPizza() {
}
public FruitsPizza(String name, int size, int price, String burdenings) {
super(name, size, price);
this.burdenings = burdenings;
}
}
子类 —— BaconPizza类:
package Pizza_Pizza;
public class BaconPizza extends Pizza {
//子类特有属性
private int weight;
//方法
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
//对父类中showPizza()方法的重写,因为showPizza()方法中少了weight属性。子类FruitsPizza同理
@Override
public String showPizza() {
return super.showPizza()+"\n培根的克数是:"+weight+"克";
}
//构造器
public BaconPizza() {
}
public BaconPizza(String name, int size, int price, int weight) {
super(name, size, price);
this.weight = weight;
}
}
测试类 —— Test类:
package Pizza_Pizza;
import java.util.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();//选择
switch (choice){
case 1:
//case1、case2下的代码块分别用{},是因为都有size、price变量,会报错
{
System.out.println("请录入培根的克数:");
int weight = sc.nextInt();
System.out.println("请录入披萨的大小:");
int size = sc.nextInt();
System.out.println("请录入披萨的价格:");
int price = sc.nextInt();
//录入了以上这些信息后,应该要有对应的输出展示,所以:
//将录入的信息封装为培根披萨的对象:
BaconPizza bp = new BaconPizza("培根披萨",size,price,weight);
System.out.println(bp.showPizza());
}
break;
case 2:
{
System.out.println("请录入你想要加入的水果:");
String burdenings = sc.next(); //水果之间用逗号衔接
System.out.println("请录入披萨的大小:");
int size = sc.nextInt();
System.out.println("请录入披萨的价格:");
int price = sc.nextInt();
//同理
//将录入的信息封装为水果披萨的对象:
FruitsPizza fp = new FruitsPizza("水果披萨",size,price,burdenings);
System.out.println(fp.showPizza());
//fp.showPizza();
}
break;
default:
break;
}
}
}
最终结果,能够实现所要求的的功能。
但是发现,披萨的选择和创建都在主函数中,显得比较拥挤,所以进行了优化。
方法:
通过(简单)工厂模式—— 获取披萨
Pizza类、FruitsPizza类、BaconPizza类不变。
增加了创建披萨的PizzaStore类,将原Test类中披萨的创建放在该类中。
优化代码示例:
PizzaStore类:
package Pizza_Pizza_Optimize;
/*
通过(简单)工厂模式—— 获取披萨
:
把披萨的选择和创建(都在主函数中)分开——显得不那么拥挤
*/
import java.util.Scanner;
public class PizzaStore {
public static Pizza getPizza(int choice){
//设置为static,则后面可直接通过 类名. 进行调用
Scanner sc = new Scanner(System.in);
Pizza p = null;
//return p;
switch (choice){
case 1:
//case1、case2下的代码块分别用{},是因为都有size、price变量,会报错
{
System.out.println("请录入培根的克数:");
int weight = sc.nextInt();
System.out.println("请录入披萨的大小:");
int size = sc.nextInt();
System.out.println("请录入披萨的价格:");
int price = sc.nextInt();
//录入了以上这些信息后,应该要有对应的输出展示,所以:
//将录入的信息封装为培根披萨的对象:
BaconPizza bp = new BaconPizza("培根披萨",size,price,weight);
//System.out.println(bp.showPizza());
p = bp;
}
break;
case 2:
{
System.out.println("请录入你想要加入的水果:");
String burdenings = sc.next(); //水果之间用逗号衔接
System.out.println("请录入披萨的大小:");
int size = sc.nextInt();
System.out.println("请录入披萨的价格:");
int price = sc.nextInt();
//同理
//将录入的信息封装为水果披萨的对象:
FruitsPizza fp = new FruitsPizza("水果披萨",size,price,burdenings);
//System.out.println(fp.showPizza());
p = fp;
}
break;
default:
break;
}
return p;
}
}
Test类:
package Pizza_Pizza_Optimize;
import java.util.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 pizza = PizzaStore.getPizza(choice);
System.out.println(pizza.showPizza());
}
}
可以看出,优化后的Test类比较简洁,同事利于后续代码的扩展。