所以我有3个文件-在这些文件中,它给了我这个错误:
Package中的Package不能应用于给定类型;需要
int,char /未找到参数
我已经尝试了所有可能想到的方法,并且不知道如何解决它,这可能是菜鸟错误!所以我希望有人可以帮助解释我做错了什么。我将粘贴以下代码!
包.java
package chap10q5;
/**
*
* @author james
*/
public class Package {
int weight;
char shippingMethod;
double shippingcost;
public Package(int weight, char shippingMethod){
this.weight = weight;
this.shippingMethod = shippingMethod;
if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'A'){
this.shippingcost = 2.00;
}
else if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'T'){
this.shippingcost = 1.50;
}
else if( (this.weight > 0 && this.weight <= 8) && this.shippingMethod == 'M'){
this.shippingcost = 0.50;
}
if( (this.weight >=9 && this.weight <= 16) && this.shippingMethod == 'A'){
this.shippingcost = 3.00;
}
else if( (this.weight >= 9 && this.weight <= 16) && this.shippingMethod == 'T'){
this.shippingcost = 2.35;
}
else if( (this.weight >= 9 && this.weight <= 16) && this.shippingMethod == 'M'){
this.shippingcost = 1.50;
}
if( (this.weight > 17) && this.shippingMethod == 'A'){
this.shippingcost = 4.50;
}
else if( (this.weight > 17) && this.shippingMethod == 'T'){
this.shippingcost = 3.25;
}
else if( (this.weight > 17) && this.shippingMethod == 'M'){
this.shippingcost = 2.15;
}
}
public String toString(){
return "Weight: " + this.weight + "\nShipping Method: " +
this.shippingMethod + "\nCost: " + this.shippingcost;
}
}
InsuredPackage.java
package chap10q5;
/**
*
* @author james
*/
public class InsuredPackage extends Package {
public InsuredPackage(int weight, char shippingCost){
if(super.shippingcost <= 1){
super.shippingcost += 2.45;
}
else if( (super.shippingcost > 1.00) && (super.shippingcost <= 3.00) ){
super.shippingcost += 3.95;
}
else if( super.shippingcost > 3 ){
super.shippingcost += 5.55;
}
}
@Override
public String toString(){
return super.toString();
}
}
UsePackage.java(我的主要方法):
package chap10q5;
/**
*
* @author james
*/
public class UsePackage {
public static void main(String[] args){
Package one = new Package(1, 'A');
System.out.println(one);
}
}
最佳答案
更改
public InsuredPackage(int weight, char shippingCost){
if(super.shippingcost <= 1){
super.shippingcost += 2.45;
}
else if( (super.shippingcost > 1.00) && (super.shippingcost <= 3.00) ){
super.shippingcost += 3.95;
}
else if( super.shippingcost > 3 ){
super.shippingcost += 5.55;
}
}
至
public InsuredPackage(int weight, char shippingMethod){
super(weight,shippingMethod);
if(shippingcost <= 1){
shippingcost += 2.45;
}
else if( (shippingcost > 1.00) && (shippingcost <= 3.00) ){
shippingcost += 3.95;
}
else if(shippingcost > 3 ){
shippingcost += 5.55;
}
}
关于java - Java扩展继承程序不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28472124/