面向对象继承封装多态经典案例:
编写程序实现比萨制作。需求说明编写程序,接收用户输入的信息,选择需要制作的比萨。可供选择的比萨有:培根比萨和海鲜比萨。
实现思路及关键代码
1)分析培根比萨和海鲜比萨
2)定义比萨类
3)属性:名称、价格、大小
4)方法:展示
5)定义培根比萨和海鲜比萨继承自比萨类
6)定义比萨工厂类,根据输入信息产生具体的比萨对象。
运行效果如下图:
参考代码:
public class Pizza { //披萨类-->父类
//属性私有
private int size;
private int price;
private String name;
//get set方法
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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pizza() { //无参构造
super();
}
public Pizza(int size, int price, String name) { //有参构造
super();
this.size = size;
this.price = price;
this.name = name;
}
//show方法
public String show(){
return "这是制作披萨的方法!";
}
}
public class BaconPizza extends Pizza { //培根披萨类-->子类
//属性私有
private int gramNum; //克数
//get set方法
public int getGramNum() {
return gramNum;
}
public void setGramNum(int gramNum) {
this.gramNum = gramNum;
}
public BaconPizza() { //无参构造
super();
}
//有参构造
public BaconPizza(int size, int price, String name, int gramNum) {
super(size, price, name);
this.gramNum = gramNum;
}
//重写shou方法
public String show(){ //方法重写
return "名称:"+super.getName()+"\n价格:"+super.getPrice()+"元"+"\n大小:"+super.getSize()+"寸"+"\n培根克数:"+gramNum;
}
}
public class SeafoodPizza extends Pizza { //海鲜披萨类-->子类
//属性私有
private String peiLiao; //配料
//get set方法
public String getPeiliao() {
return peiLiao;
}
public void setPeiliao(String peiliao) {
this.peiLiao = peiLiao;
}
public SeafoodPizza() {//无参构造
super();
}
//有参构造
public SeafoodPizza(int size, int price, String name, String peiLiao) {
super(size, price, name);
this.peiLiao = peiLiao;
}
//重写show方法
public String show(){
return "名称:"+super.getName()+"\n价格:"+super.getPrice()+"元"+"\n大小:"+super.getSize()+"寸"+"\n配料:"+peiLiao;
}
}
import java.util.Scanner;
public class PizzaStore { //披萨工厂类-->主要是生产披萨
public static Pizza getPizza(int order) { //得到披萨的方法
Scanner input = new Scanner(System.in);
Pizza p = null; //赋初始值
if (order == 1) { // 培根披萨
System.out.println("请输入培根克数:");
int gramNum = input.nextInt();
System.out.println("请输入披萨大小:");
int size = input.nextInt();
System.out.println("请输入披萨价格:");
int price = input.nextInt();
// 根据上面录入的信息,创建具体的培根披萨:--创建对象:
p = new BaconPizza(size, price, "培根披萨", gramNum);//多态-->向上转型
}
if (order == 2) { // 海鲜披萨
System.out.println("请输入配料信息:");
String peiLiao = input.next();
System.out.println("请输入披萨大小:");
int size = input.nextInt();
System.out.println("请输入披萨价格:");
int price = input.nextInt();
// 根据上面录入的信息,创建具体的培根披萨:--创建对象:
p = new SeafoodPizza(size, price, "海鲜披萨", peiLiao);//多态-->向上转型
}
return p; //最终根据需求生产具体披萨后,并返回
}
}
import java.util.Scanner;
public class Test { //测试类
public static void main(String[] args) {
//具体的逻辑:---买披萨:
Scanner input = new Scanner(System.in);
System.out.print("请输选择要制作的披萨:1.培根披萨 2.海鲜披萨");
int num = input.nextInt();
Pizza p=PizzaStore.getPizza(num); //调用披萨工厂类PizzaStroe中的方法-->返回的是披萨类
System.out.println(p.show());
}
版权声明:本文为weixin_42517667原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。