覆盖在类上没有预期的效果

huangapple go评论76阅读模式
英文:

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 ();

huangapple
  • 本文由 发表于 2020年9月28日 17:19:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64099316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定