题目:
普通实现:
package com.gao.Project.Pro2;
public class Pizza {
//父类:共同的属性:名称,大小,价格
//共同的方法:展示披萨的信息
//属性:
private String name; //名称
private int size; //大小
private int price; //价格
//方法:
//为属性添加Setter,Getter方法 快捷键:alt+insert
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价格:"+price + "元\n大小:"+size +"寸\n";
}
//构造器:
//保证空参构造器的存在:
//(1)便于通过反射,创建运行时类的对象
//(2)便于子类继承此运行实类是,默认调用空参构造器时,保证父类有此构造器
public Pizza() {
}
//包括全部参数的构造器
public Pizza(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
}
package com.gao.Project.Pro2;
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;
}
}
package com.gao.Project.Pro2;
public class BaconPizza extends Pizza{
//属性:克数
private double weight;
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
//构造器
//空参构造器
public BaconPizza() {
}
//包括全部参数的构造器
public BaconPizza(String name, int size, int price, double weight) {
super(name, size, price);
this.weight = weight;
}
//重写父类showpizza方法
@Override
public String showpizza() {
return super.showpizza()+"培根克数:"+weight+"g";
}
}
package com.gao.Project.Pro2;
import java.net.Socket;
import java.sql.SQLOutput;
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:
{
System.out.println("请输入培根的克数:");
double weight = sc.nextDouble();
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 burdening = sc.next();
System.out.println("请输入披萨的大小:");
int size = sc.nextInt();
System.out.println("请输入披萨的价格:");
int price = sc.nextInt();
//将录入的信息封装成水果披萨的信息
FruitsPizza fp = new FruitsPizza("水果披萨",size,price,burdening);
System.out.println(fp.showpizza());
//加代码块,作用在不同的作用域
}
break;
}
}
}
利用工厂加强:
新加了PizzaStore类
package com.gao.Project.Pro2;
import java.util.Scanner;
public class PizzaStore {
public static Pizza getPizza(int choice){
Pizza p = null;
Scanner sc = new Scanner(System.in);
switch (choice){
case 1:
{
System.out.println("请输入培根的克数:");
double weight = sc.nextDouble();
System.out.println("请输入披萨的大小;");
int size = sc.nextInt();
System.out.println("请输入披萨的价格:");
int price = sc.nextInt();
//将录入的信息封装成培根披萨的信息
BaconPizza bp = new BaconPizza("培根披萨",size,price,weight);
p = bp;
}
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;
//加代码块,作用在不同的作用域
}
break;
}
return p;
}
}
只修改了原代码的Test类
package com.gao.Project.Pro2;
import java.net.Socket;
import java.sql.SQLOutput;
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());
}
}
标签:题目,商店,int,price,披萨,System,public,size
From: https://www.cnblogs.com/gaoxiaocuo/p/16768341.html