项目需求
项目结构分析
最终代码
匹萨父类:
/**
* @Auther: themyth
* 父类:匹萨类
*/
public class Pizza {
//属性
private String name;//名称
private int size;//大小
private int price;//价格
//方法
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;
}
}
培根匹萨:
/**
* @Auther: themyth
*/
public class BaconPizza extends Pizza {
//属性:
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
//构造器:
public BaconPizza() {
}
public BaconPizza(String name, int size, int price, int weight) {
super(name, size, price);
this.weight = weight;
}
//重写父类showPizza方法:
@Override
public String showPizza() {
return super.showPizza()+"\n培根的克数是:"+weight+"克";
}
}
水果匹萨:
/**
* @Auther: themyth
*/
public class FruitsPizza extends Pizza{
//属性:
private String burdening;
public String getBurdening() {
return burdening;
}
public void setBurdening(String burdening) {
this.burdening = burdening;
}
//构造器:
public FruitsPizza() {
}
public FruitsPizza(String name, int size, int price, String burdening) {
super(name, size, price);
this.burdening = burdening;
}
//重写父类showPizza方法:
@Override
public String showPizza() {
return super.showPizza()+"\配料水果:"+burdening;
}
}
测试类:
import java.util.Scanner;
/**
* @Auther: themyth
*/
public class Test {
//这是一个main方法,是程序的入口:
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());
}
}
工厂类:
import java.util.Scanner;
/**
* @Auther: themyth
*/
public class PizzaStore {
public static Pizza getPizza(int choice){//父类当作方法返回值,返回的是一个具体的对象--->多态的应用之一
Scanner sc = new Scanner(System.in);
Pizza p = null;
switch (choice){
case 1:
{
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);
p = bp;//将一个具体的培根匹萨对象赋值给p
//可以合成一句话->p = new BaconPizza("培根匹萨",size,price,weight);
}
break;//也可以在代码块中
case 2:
{
System.out.println("请录入你想要加入的水果:");
String burdening = sc.next();
System.out.println("请录入匹萨的大小:");
int size = sc.nextInt();
System.out.println("请录入匹萨的价格:");
int price = sc.nextInt();
//将录入的信息封装为水果匹萨的对象:
FruitsPizza fp = new FruitsPizza("水果匹萨",size,price,burdening);
p = fp;//将一个具体的水果匹萨对象赋值给p
//可以合成一句话->p = new FruitsPizza("水果匹萨",size,price,burdening);
}
break;//也可以在代码块中
}
return p;//返回一个具体的匹萨对象
}
}