代码之家  ›  专栏  ›  技术社区  ›  James111

Java扩展继承程序不工作?

  •  1
  • James111  · 技术社区  · 11 年前

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

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

    保险包.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 回复  |  直到 11 年前
        1
  •  1
  •   Eran    11 年前

    改变

    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;
        }
    }
    
        2
  •  0
  •   colavitam    11 年前

    因为您在中没有默认构造函数 Package ,您需要拨打 包裹 的构造函数 InsuredPackage 构造函数。例如:

    public InsuredPackage(int weight, char shippingMethod, double shippingCost) {
        super(weight, shippingMethod);
    
        ...
    }
    
        3
  •  0
  •   jjlema    11 年前

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

    推荐文章