问题遇到的现象和发生背景
“prpject_饮料”中的“咖啡(java.lang.String,int,int,java.lang.String)”。Coffee“不能应用于“(java.lang.String,java.lang.String,int,java.lang.String)”
'Coffee(java.lang.String, int, int, java.lang.String)' in 'prpject_beverage.Coffee' cannot be applied to '(java.lang.String, java.lang.String, int, java.lang.String)'
操作环境、软件版本等信息
IDEA
package prpject_beverage;
public class Beverage {
private String name;//名称
private int size;//规格
private int price;//价格
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 showBeverage(){
return "选择的饮料:"+name+"\n饮料规格:"+size+"\n"+"价格是:"+price+"元";
}
public Beverage() {
}
public Beverage(String name, int size, int price) {
this.name = name;
this.size = size;
this.price = price;
}
}
package prpject_beverage;
public class Coffee extends Beverage{
private String remark;
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Coffee() {
}
public Coffee(String name, int size, int price, String remark) {
super(name, size, price);
this.remark = remark;
}
@Override
public String showBeverage() {
return super.showBeverage()+remark;
}
}
package prpject_beverage;
public class Coke extends Beverage{
private String remark;
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Coke() {
}
public Coke(String name, int size, int price, String remark) {
super(name, size, price);
this.remark = remark;
}
@Override
public String showBeverage() {
return super.showBeverage()+"选择的可乐是"+remark;
}
}
package prpject_beverage;
public class Tea extends Beverage {
private String remark;
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Tea() {
}
public Tea(String name, int size, int price, String remark) {
super(name, size, price);
this.remark = remark;
}
@Override
public String showBeverage() {
return super.showBeverage()+remark;
}
}
package prpject_beverage;
import java.util.Scanner;
public class Factory {
public static Beverage getBeverage(int choice){
Scanner sc = new Scanner(System.in);
Beverage b= null;
switch (choice){
case 1:{
System.out.println("输入咖啡的规格");
String size=sc.next();
System.out.println("输入咖啡的备注");
String remark= sc.next();
System.out.println("输入咖啡的价格");
int price = sc.nextInt();
Coffee c = new Coffee("咖啡", size,price,remark);
b=c;
}
break;
case 2:{
System.out.println("输入可乐的名称");
String name= sc.nextLine();
System.out.println("输入可乐的规格");
String size = sc.next();
System.out.println("输入可乐的价格");
int price = sc.nextInt();
System.out.println("输入可乐的备注");
String remark= sc.next();
Coke co = new Coke("可乐",size,price,remark);
b=co;
}
break;
case 3:{
System.out.println("输入奶茶的名称");
String name= sc.nextLine();
System.out.println("输入奶茶的规格");
String size= sc.next();
System.out.println("输入奶茶价格");
int price = sc.nextInt();
System.out.println("输入奶茶备注");
String remark = sc.next();
Tea t = new Tea("奶茶",size,price,remark);
b=t;
}
}
return b;
}
}
package prpject_beverage;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("选择尼需要的饮料(1、 咖啡 2、可乐 3、奶茶)");
int choice = sc.nextInt();//选择
// 工厂提取
Beverage beverage = Factory.getBeverage(choice);
System.out.println(beverage.showBeverage());
}
}