java - 从子类引用父类(super class)时,父类(super class)的字段值显示为零

标签 java inheritance methods subclass superclass

我目前在从子类获取父类(super class)中的值时遇到问题。在父类(super class)中,如果 choice == 5,则调用子类中的 power 方法。问题是,当它从子类中的 power 方法调用 getCurrentValue() 时,即使字段 currentValue 的值不是 0,它也会打印出 0.0。我想知道当我扩展父类(super class)时我是否搞砸了一些事情。当我从子类中的 power 方法调用 getCurrentValue() 时,我是否可以调用不同的 currentValue 字段,也许是值未更改的字段?

这是我的父类(super class)的代码:

import java.util.Scanner;


public class TrippelTylerMemoryCalculator {

     //Fields
     private double currentValue;

     //Methods
     public static int displayMenu(){
         Scanner input = new Scanner(System.in);
         int choice;

     System.out.print("\nMenu \n" +
            "1. Add \n" +
            "2. Subtract \n" +
            "3. Multiply \n" +
            "4. Divide \n" +
            "5. Power \n" + 
            "6. Logarithm \n" +
            "7. Clear \n" +
            "8. Quit \n\n" +
            "What would you like to do? \n");

    choice = input.nextInt();
    if(choice<1 || choice>6){
        while(choice<1 || choice>6){
            System.out.println("You have entered an invalid menu option. \n" +
                "What would you like to do? \n");
            choice = input.nextInt();
        }
        return choice;
    }
    else{
        return choice;
    }
    }

public static double getOperand(String prompt){
    Scanner input = new Scanner(System.in);
    double operand;
    System.out.println(prompt);
    operand = input.nextDouble();
    return operand;
}

public double getCurrentValue(){
    return currentValue;
}

public void add(double operand2){
    double operand = operand2;
    double answer;
    answer = currentValue + operand;
    currentValue = answer;
}

public void subtract(double operand2){
    double operand = operand2;
    double answer;
    answer = currentValue - operand;
    currentValue = answer;
}

public void multiply(double operand2){
    double operand = operand2;
    double answer;
    answer = currentValue * operand;
    currentValue = answer;
}

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

public void clear(){
    currentValue = 0;
}

//Main Method
public static void main(String[] args) {
    TrippelTylerMemoryCalculator instance = new TrippelTylerMemoryCalculator();
    TrippelTylerScientificMemoryCalculator instance2 = new TrippelTylerScientificMemoryCalculator();

    //Fields for main method
    double operand;
    boolean repeat = true;

    //Program starts here
    while(repeat){
        System.out.print("The current value is: " + instance.getCurrentValue() + "\n");
        int choice;
        choice = displayMenu();
        if(choice == 1){
            operand = getOperand("What is the second value?");
            instance.add(operand);
        }
        else if(choice == 2){
            operand = getOperand("What is the second value?");
            instance.subtract(operand);
        }
        else if(choice == 3){
            operand = getOperand("What is the second value?");
            instance.multiply(operand);
        }
        else if(choice == 4){
            operand = getOperand("What is the second value?");
            instance.divide(operand);
        }
        else if(choice == 5){
            operand = getOperand("What is the second value?");
            instance2.power(operand);
        }
        else if(choice == 6){

        }
        else if(choice == 7){
            instance.clear();
        }
        else if(choice == 8){
            System.out.println("Goodbye!");
            System.exit(0);
        }

    }
}

}

这是我的子类的代码:

 public class TrippelTylerScientificMemoryCalculator extends TrippelTylerMemoryCalculator{

    public void power(double operand2){
         double operand = operand2;
         double answer;
         //answer = Math.pow(temp, operand);
         System.out.println(getCurrentValue());
    }
 }

任何意见都将不胜感激!谢谢大家!

最佳答案

您有两个实例,由变量 instanceinstance2 引用。这些是代码中使用 instance2唯一行:

TrippelTylerScientificMemoryCalculator instance2 = new 
    TrippelTylerScientificMemoryCalculator();

...

instance2.power(operand);

您期望值(value)从何而来?我怀疑您只需要一个实例,并将其作为子类的实例。那么你的所有操作都会作用在同一个对象上。因此只需将 instance 变量声明更改为:

TrippelTylerScientificMemoryCalculator instance = new 
    TrippelTylerScientificMemoryCalculator();

...完全删除 instance2 声明,并在要调用 power 方法时使用 instance

(我也强烈建议较短的类型名称,但这是另一回事。)

此外,请注意,与所有其他操作不同,您的 power 方法不会更改当前值(事实上,也不会执行任何有用的操作),但会负责打印它。这对我来说似乎很奇怪。您应该尽量使您的方法的作用保持一致。

关于java - 从子类引用父类(super class)时,父类(super class)的字段值显示为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24879980/

相关文章:

java - 将字符串转换为 swt 颜色

java - Guava 破坏了 maven-plugin

java - 使用 Java 查找句子中的确切单词

java - 用其他值替换 ArrayList<String> 中填充的字符串

Java:静态初始化 block 中的方法比主方法中的方法慢

java - 更新了 util.scanner 重复方法

java - MicrosoftWebDriver.exe 应预安装在提升的命令提示符中

java - 协变结构因 Java 中的捕获错误而失败

java - 普通接口(interface)类和只有抽象方法的抽象类有什么区别吗?

inheritance - Typescript commonjs 继承