在Java中重写方法和继承

时间:2015-01-27 11:12:00

标签: java inheritance methods

我正在尝试使用Parent class Pet和子类Dog和Cat完成代码。 Pet类定义变量,子类计算每只宠物可以安全摄取的每种药物(卡洛芬和乙酰丙嗪)的含量。

我在主方法中调用carprofen和acepromazine方法时遇到问题。我不知道自己做错了什么。

它将carprofen和acepromazine值始终返回为0.0

import java.util.Scanner;

class Pet {
    String kind;
    double weight;
    double atotalAmount;
    double ctotalAmount;

    public Pet(String startKind, double startWeight) {
        kind = startKind;
        weight = startWeight;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String newValue) {
        kind = newValue;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double newValue) {
        weight = newValue;
    }

    public double calcCarprofen(double weight) {
        return ctotalAmount;
    }

    public double calcAcepromazine(double weight) {
        return atotalAmount;
    }
}

class Cat extends Pet {
    public Cat(String startKind, double startWeight) {
        super(startKind, startWeight);
    }

    public double calcCarprofen(double weight) {
        double c = 12.0;
        double ccatdose = .25;
        double ctotalAmount = (((weight) / (2.2)) * ((ccatdose) / (c)));
        return ctotalAmount;
    }

    public double calcAcepromazine(double weight) {
        double a = 10.0;
        double acatdose = .002;
        double atotalAmount= (((weight) / (2.2)) * ((acatdose) / (a)));
        return atotalAmount;
    }
}

class Dog extends Pet {
    public Dog(String startKind, double startWeight) {
        super(startKind, startWeight);
    }

    public double calcCarprofen(double weight) {
        double c = 12.0;
        double cdogdose = .5;
        double ctotalAmount = 0.0;
        ctotalAmount = (((weight) / (2.2)) * ((cdogdose) / (c)));
        return ctotalAmount;
    }

    public double calcAzepromazine(double weight) {
        double a = 10.0;
        double adogdose = .03;
        double atotalAmount = 0.0;
        atotalAmount = (((weight) / (2.2)) * ((adogdose) / (a)));
        return atotalAmount;
    }
}

public class Ch7Ex8 {
    public static void main(String[] args) {
        double startWeight;
        String startKind;

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Is this a cat or dog?: ");
        startKind = keyboard.nextLine();
        System.out.println("Please enter the pet's weight: ");
        startWeight = keyboard.nextDouble();

        Cat usersCat = new Cat(startKind, startWeight);
        Dog usersDog = new Dog(startKind, startWeight);

        double catcdose = usersCat.calcCarprofen( double weight);
        double catadose = usersCat.calcAcepromazine( double weight);
        double dogcdose = usersDog.calcCarprofen( double weight);
        double dogadose = usersDog.calcAcepromazine( double weight);
        System.out.println("The total cat carprofen dose is: " + catcdose);
        System.out.println("The total cat acepromazine dose is: " + catadose);
        System.out.println("The total dog carprofen dose is: " + dogcdose);
        System.out.println("The total dog acepromazine dose is: " + dogadose);
    }
}    

3 个答案:

答案 0 :(得分:0)

不确定这是否是唯一的问题,但您应该更改

    double catcdose=usersCat.calcCarprofen(double weight);
    double catadose=usersCat.calcAcepromazine(double weight);
    double dogcdose=usersDog.calcCarprofen(double weight);
    double dogadose=usersDog.calcAcepromazine(double weight);

    double catcdose=usersCat.calcCarprofen(weight);
    double catadose=usersCat.calcAcepromazine(weight);
    double dogcdose=usersDog.calcCarprofen(weight);
    double dogadose=usersDog.calcAcepromazine(weight);

如果您打算将weight变量传递给这些方法,则必须为其提供初始值。也许你的意思是改为startWeight

再看一下,也许这些方法根本不需要权重参数,因为你已经将权重传递给构造函数并将其存储在实例的成员中。

还要确保正确使用方法名称。我在某些地方看到calcAzepromazine,在其他地方看到calcAcepromazine

答案 1 :(得分:0)

你不能以这种方式调用方法

Pet.
    double catcdose=usersCat.calcCarprofen(double weight);

应该是

double catcdose=usersCat.calcCarprofen(weight);

答案 2 :(得分:0)

这是代码:

    public class Ch7Ex8 {
public void Ch7Ex8 () {

}

public static void main(String [] args) {


    double startWeight;
    String startKind;
    double weight=0;

    Scanner keyboard= new Scanner(System.in);

    System.out.println("Is this a cat or dog?: ");
        startKind=keyboard.nextLine();
    System.out.println("Please enter the pet's weight: ");
        startWeight= keyboard.nextDouble();

        if (startKind=="cat" || startKind=="Dog") {
            Pet usersPet=new Pet(startKind, startWeight);
            Cat usersCat= new Cat(startKind, startWeight);
            double catcdose=usersCat.calcCarprofen();
            double catadose=usersCat.calcAcepromazine();
            System.out.println("The total cat carprofen dose is: "+catcdose);
            System.out.println("The total cat acepromazine dose is: "+catadose);

        } else if (startKind=="dog" || startKind=="Dog") {
            Pet usersPet=new Pet(startKind, startWeight);
            Dog usersDog= new Dog(startKind, startWeight);
            double dogcdose=usersDog.calcCarprofen();
            double dogadose=usersDog.calcAcepromazine();
            System.out.println("The total dog carprofen dose is: "+dogcdose);
            System.out.println("The total dog acepromazine dose is: "+dogadose);
        }


}

}