Java扩展继承程序不工作?

时间:2015-02-12 07:55:05

标签: java inheritance netbeans

所以我有3个文件 - 在文件中它给我这个错误:

  

类Package中的包不能应用于给定的类型;需要   int,char /找不到参数

我已经尝试了我能想到的一切,并且无法弄清楚如何修复它,这可能是一个菜鸟错误!?所以我希望有人可以帮助解释我做错了什么。我将粘贴下面的代码!

Package.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);
    }
}

3 个答案:

答案 0 :(得分:1)

更改

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;
    }
}

答案 1 :(得分:0)

由于您在Package中没有默认构造函数,因此您需要在Package构造函数中调用InsuredPackage个构造函数之一。例如:

public InsuredPackage(int weight, char shippingMethod, double shippingCost) {
    super(weight, shippingMethod);

    ...
}

答案 2 :(得分:0)

问题是,当子类的构造函数不调用父类的构造函数时,编译器会在第一行调用默认构造函数,我的意思是super(),但是你没有定义在你的Package课程中,这就是错误。