Java 从子类引用超类时,超类的字段值显示为零
我目前在从子类获取超类中的值时遇到问题。在超类中,如果choice==5,则调用子类中的power方法。问题是,当它从子类中的幂方法调用getCurrentValue()时,即使currentValue字段的值不是0,它也会打印出0.0。我想知道我扩展超类时是否弄错了什么。当我从子类中的power方法调用getCurrentValue()时,是否可以调用不同的currentValue字段,可能是一个值没有更改的字段 下面是我的超类的代码:Java 从子类引用超类时,超类的字段值显示为零,java,inheritance,methods,subclass,superclass,Java,Inheritance,Methods,Subclass,Superclass,我目前在从子类获取超类中的值时遇到问题。在超类中,如果choice==5,则调用子类中的power方法。问题是,当它从子类中的幂方法调用getCurrentValue()时,即使currentValue字段的值不是0,它也会打印出0.0。我想知道我扩展超类时是否弄错了什么。当我从子类中的power方法调用getCurrentValue()时,是否可以调用不同的currentValue字段,可能是一个值没有更改的字段 下面是我的超类的代码: import java.util.Scanner;
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);
}
}
}
}
任何意见都将不胜感激!谢谢大家 您有两个实例,由变量
instance
和instance2
引用。以下是代码中唯一使用instance2
的行:
TrippelTylerScientificMemoryCalculator instance2 = new
TrippelTylerScientificMemoryCalculator();
...
instance2.power(operand);
你认为价值从何而来?我怀疑您只需要一个实例,并将其作为子类的实例。然后,所有操作都将作用于同一对象。因此,只需将实例
变量声明更改为:
TrippelTylerScientificMemoryCalculator instance = new
TrippelTylerScientificMemoryCalculator();
。。。完全删除instance2
声明,并在需要调用power
方法时使用instance
(我也强烈建议使用较短的类型名称,但那是另一回事。)
此外,请注意,与所有其他操作不同,您的
power
方法不会更改当前值(实际上也不会做任何有用的事情),而是负责打印当前值。我觉得这很奇怪。你应该在方法上保持一致。谢谢你的回复Jon!您所说的一切都会很好地工作,除非我删除instace2并将实例重定向到我的子类,否则超类中的某些方法将不再工作,因为它们不是静态方法,我无法在主方法中调用它们,除非它们是静态的。不幸的是,这就是我的困境所在。另外,将方法更改为静态并不是一个真正的选项,因为这是一个学校作业,作业说明就是这样让我们设置的up@user3795283:您不需要更改有关静态方法的任何内容。他们根本不受我的建议的影响。哦,我现在明白了,通过将实例定向到子类,它仍然在该实例中包含超类,因为它固有地包含在子类中。这就是我搞砸的地方。非常感谢乔恩!我真的很感激!
TrippelTylerScientificMemoryCalculator instance = new
TrippelTylerScientificMemoryCalculator();