Java:子类无法获得超类中字段的值

时间:2014-11-10 03:59:27

标签: java field subclass calculator superclass

这是超类:

public class MemoryCalc {

private double currentValue;


public double getCurrentValue() {
    return currentValue;
}


public void setCurrentValue(double currentValue) {
    this.currentValue = currentValue;
}


public int displayMenu() {
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);

    int choice = -1;

    while (choice < 1 || choice > 6) {
        System.out.println();
        System.out.println("Menu");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");
        System.out.println("5. Clear");
        System.out.println("6. Quit");
        System.out.println();
        System.out.print("What would you like to do? ");

        choice = input.nextInt();

        if (choice < 1 || choice > 6) {
            System.out.println(choice + " wasn't one of the options");
        }

        if (choice == 6) {
            System.out.println("Goodbye!");
            System.exit(0);
        }
    }

    return choice;
}


public double getOperand(String prompt) {
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    System.out.print(prompt);
    return input.nextDouble();
}


public void add(double op2) {
    currentValue += op2;
}


public void subtract(double op2) {
    currentValue -= op2;
}


public void multiply(double op2) {
    currentValue *= op2;
}


public void divide(double op2) {
    if (op2 == 0) {
        currentValue = Double.NaN;
    } else {
        currentValue /= op2;
    }
}


public void clear() {
    currentValue = 0;
}
}

这是子类:

class SciMemCalc extends MemoryCalc{

private double currentValue;

public int displayMenu(){
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    int choice = -1;
    while (choice < 1|| choice > 8){
        System.out.println();
        System.out.println("Menu:");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");
        System.out.println("5. Power");
        System.out.println("6. Logarithm");
        System.out.println("7. Clear");
        System.out.println("8. Quit");
        System.out.println();
        System.out.println("What would you like to do?");
    choice = input.nextInt();
    if (choice < 1|| choice > 8){
        System.out.println(choice +" wasn't one of the options");
    }
    if (choice == 8){
        System.out.println("Thank you, good bye!");
        System.exit(0);
    }
    }
    return choice;
}
public void power(double op2){
    Math.pow(currentValue, op2);
}
public void log() {
    Math.log(currentValue);
}
}

这是驱动程序:

public class SciCalcDriver {

public static void main(String[] args) {

    SciMemCalc calc = new SciMemCalc();
    while (true){
        System.out.println("The current value is " + calc.getCurrentValue());

        int choice = calc.displayMenu();

        double second = 0;
        if (choice < 6) {
            second = calc.getOperand("What is the second number? ");
        }

        if (choice == 1) {
            calc.add(second);
        } else if (choice == 2){
            calc.subtract(second);
        } else if (choice == 3){
            calc.multiply(second);
        } else if (choice == 4){
            calc.divide(second);
        } else if (choice == 5){
            calc.power(second);
        } else if (choice == 6){
            calc.log();
        } else if (choice == 7){
            calc.clear();
        }
    }
}
}

现在计算器可以加,减,乘,除细,但是当我使用power或log方法时,没有任何反应。我已经尝试使用调试器,它说它得到了所有必要的输入,但currentValue似乎没有改变。我认为我不需要对超类进行任何更改。建议?

4 个答案:

答案 0 :(得分:3)

class MemoryCalc {
    private double currentValue;
}

class SciMemCalc extends MemoryCalc {
    private double currentValue;
}

这里发生的事实是实际上有两个变量用同一个名称声明。 SciMemCalc无法访问MemoryCalc中声明的变量,因为它是私有的。

相反,您通常会通过设置者和获取者保护currentValue或与之互动。

class MemoryCalc {
    protected double currentValue;
}

class SciMemCalc extends MemoryCalc {
    // SciMemCalc has access to currentValue
}

答案 1 :(得分:0)

您的超类“currentValue shadowing通过在您的子类中重新声明它。要解决此问题,请在子类SciMemCalc中删除声明currentValue的行:

private double currentValue;

并将SciMemCalc的所有访问权限更改为currentValue以使用超类'访问者(getter)和mutator(setter)方法:

public void power(double op2){
    setCurrentValue(Math.pow(getCurrentValue(), op2));
}
public void log() {
    setCurrentValue(Math.log(getCurrentValue()));
}

或者,您可以在超类中将currentValue声明为protected,这样您就可以直接在子类中访问它。但是,在这种情况下,您还必须确保将计算结果分配回currentValue

public void power(double op2){
    currentValue = Math.pow(currentValue, op2);
}
public void log() {
    currentValue = Math.log(currentValue);
}

答案 2 :(得分:0)

从子类中删除private double currentValue;并将超类中的private double currentValue;更改为protected double currentValue;。如果您有私有值,则超类无法访问它,并且您创建的重复值只隐藏原始值。受保护解决了这个问题。

答案 3 :(得分:0)

您已将currentValue声明为类的私有成员,因此其他

无法访问它

类。当你使它受到保护或公开时它会起作用。并将解决你的问题。

只需更改超类中的currentValue字段。

protected double currentValue //只能由其子类

访问

public double currentValue //将被此包中的所有类访问