英文:
Override not having expected effect on class
问题
我正在测试扩展一个类并覆盖函数。使用以下代码,我没有得到编译错误,但当我调用JonsClass2.jonsCalcFromClass(2)时,我仍然得到来自JonsClass2而不是我预期的来自JonsClass3的覆盖响应。
另外,我不得不从主函数中进行这些调用:JonsClass2 JC2 = new JonsClass2();,然后JC2.jonsCalcFromClass(2);否则编译器会抱怨我从静态上下文中调用非静态内容。
class JonsClass3 extends JonsClass2 {
@Override
protected int jonsCalcFromClass(int a) {
if (a == 2) System.out.print("从主函数调用JonsClass3,使用2作为传递变量 ");
if (a == 1) System.out.print("从主函数调用JonsClass3,使用1作为传递变量 ");
int c = (a + 2);
return c;
}
@Override
protected double jonsCalcFromClass(double b) {
System.out.print("这是在JonsClass3中被调用的double行 > ");
double c = (b + 300.10);
return c;
}
}
class JonsClass2 {
protected int jonsCalcFromClass(int a) {
System.out.print("这是从JonsClass2中被调用的int行 > ");
int c = (a + 2);
return c;
}
protected double jonsCalcFromClass(double b) {
System.out.print("这是在JonsClass2中被调用的double行 > ");
double c = (b + 3.10);
return c;
}
}
英文:
I am testing out extending a class and overriding functions. With the below I get no compile errors yet when I call JonsClass2.jonsCalcFromClass(2) I still get the response from JonsClass2 not the override from JonsClass3 that I expected.
As an aside I am having to make these calls from main using JonsClass2 JC2 = new JonsClass2(); then JC2.jonsCalcFromClass(2); else the compiler complains that I am calling a non static from a static context.
class JonsClass3 extends JonsClass2 {
@Override
protected int jonsCalcFromClass(int a) {
if (a==2) System.out.print("JonsClass3 Called from main using 2 as passed variable ");
if (a==1) System.out.print("JonsClass3 from main using 1 as passed variable ");
int c = (a + 2);
return c;
}
@Override
protected double jonsCalcFromClass(double b) {
System.out.print("This is double line being called in JonsClass3> ");
double c = (b + 300.10);
return c;
}
}
class JonsClass2 {
protected int jonsCalcFromClass(int a) {
System.out.print("This is the int line from JonsClass2 being called > ");
int c = (a + 2);
return c;
}
protected double jonsCalcFromClass(double b) {
System.out.print("This is double line being called in JonsClass2> ");
double c = (b + 3.10);
return c;
}
}
答案1
得分: 2
请注意你创建了哪个实例。
JonsClass2 js2 = new JonsClass2();
JonsClass2 js3 = new JonsClass3();
js2.jonsCalcFromClass(2); // 这是从 JonsClass2 调用的 int 行 >
js3.jonsCalcFromClass(2); // 从主函数中使用 2 作为传递变量来调用 JonsClass3
在你的示例中,`onsClass2 JC2 = new JonsClass2();` 你创建了 `JonsClass2` 的实例,所以不会调用 `JonsClass3` 中的方法。
英文:
Pay attention on what instanse you create.
JonsClass2 js2 = new JonsClass2();
JonsClass2 js3 = new JonsClass3();
js2.jonsCalcFromClass(2); // This is the int line from JonsClass2 being called >
js3.jonsCalcFromClass(2); //JonsClass3 Called from main using 2 as passed variable
In your example onsClass2 JC2 = new JonsClass2();
you create instance of JonsClass2
, so methods from JonsClass3
will not be called.
答案2
得分: 0
> 编译器报错,称我从静态上下文中调用非静态方法。
我们不能直接从静态方法(比如 main 方法)中调用非静态方法,因为静态方法是类的方法,而非静态方法是对象方法,所以我们应该在对象上调用它。所以:
public static void main(String[] args) {
jonsCalcFromClass(1) // 编译错误
JonsClass2 JC2 = new JonsClass3();
JC2.jonsCalcFromClass(1) // 正确
}
如果你想要多态行为,应该这样做:
JonsClass2 JC2 = new JonsClass3();
英文:
> the compiler complains that I am calling a non static from a static
> context.
We can't call non-static methods from a static method (like main) directly. because a static method is a class' method. but a non-static method is object method so we should call it on an object. so:
public static void main(String[] args) {
jonsCalcFromClass(1) // compile error
JonsClass2 JC2 = new JonsClass3()
JC2.jonsCalcFromClass(1) // correct
}
If you want polymorphism behaviour you should do this:
JonsClass2 JC2 = new JonsClass3 ();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论