[英]How can I get subclass value from super class method in Java?
I have these two classes: 我有这两节课:
Class A
{
public int max = 100;
public int getMax()
{
return max;
}
}
Class B extends A
{
public int max = 200;
}
I write the following code: 我编写以下代码:
A clsA = new A();
B clsB = new B();
valA = clsA.getMax();
valB = clsB.getMax();
valA is 100 valB is 100 again valA是100 valB又是100
How can I write code that clsB.getMax();
我怎么写这样的代码
clsB.getMax();
returns 200? 返回200?
You can override getMax in class B: 您可以在B类中覆盖getMax:
@Override
public int getMax()
{
return max;
}
Though, in general, it's a bad idea to have variables with the same name in the super class and the sub-class. 虽然,总的来说,在超类和子类中具有相同名称的变量是一个坏主意。
Note that after the change, the following will also return 200 : 请注意,更改后,以下内容也会返回200:
A clsB = new B();
valB = clsB.getMax();
Override the getMax
method in B
: 覆盖
B
的getMax
方法:
class B extends A
{
public int max = 200;
public int getMax()
{
return max;
}
}
In the getter method, return max;
在getter方法中,
return max;
is equivalent to return this.max
where this
references the current instance of B
. 等价于
return this.max
,其中this
引用了B
的当前实例。 On the other hand, calling return super.max
returns the max
field value referenced in the parent subclass. 另一方面,调用
return super.max
将返回父子类中引用的max
字段值。
the short answer is that you can't override members in subclasses like you are trying. 简短的答案是,您不能像尝试的那样覆盖子类中的成员。 What you instead should do is make B change the value of max directly.
相反,您应该做的是使B直接更改max的值。 Something like this:
像这样:
class B extends A
{
public B()
{
max = 200;
}
}
You have to add method getMax()
to class B
. 您必须将方法
getMax()
添加到类B
The implementation might be the same as in Class A 实现方式可能与A类相同
@Overwrite
public int getMax()
{
return max;
}
When you DO NOT overwrite the method one from B
calls parent's method getMax()
. 当您不覆盖方法时,
B
调用父方法的方法getMax()
。 And parent metod getMax()
returns 150 父方法
getMax()
返回150
Try Method Overriding 尝试方法覆盖
In Java, the "closest method" will be called. 在Java中,将调用“最接近的方法”。 Because clsB extends A, but doesn't have it's own definition of getMax(), A's version of getMax() will be called.
由于clsB扩展了A,但没有它自己的getMax()定义,因此将调用A的getMax()版本。 However, because clsB also shares the variables of A, getMax (called from class A) will return class A's number.
但是,由于clsB也共享A的变量,因此getMax(从A类调用)将返回A类的编号。
The solution? 解决方案? add
加
public int getMax()
{
return max;
}
to class B. 到B级。
Implement Method override concept 实施方法覆盖概念
@Override
public int getMax()
{
return max;
}
Overriding happens at run time 覆盖在运行时发生
A clsB = new B();
valB = clsB.getMax();
Hope this should work. 希望这应该工作。
Java does not support override any variables in a class. Java不支持覆盖类中的任何变量。
You can have two ways to do it. 您可以通过两种方法来实现。
Pass the variable through a constructor . 通过构造函数传递变量 。 I suggest doing this way because, if the method do the same action.
我建议这样做,因为如果该方法执行相同的操作。 By overriding it and doing it again is a bad practice.
覆盖它并再次执行它是一个不好的做法。
Class A { public int max = 100; public A(int maxVal) { this.max=maxVal; } public int getMax(){ return max; } } B clsB = new B(); A clsA = new A(clsB.max);
Override the method 覆盖方法
@override public int getMax(){ max=200; //do the job. }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.