从Java子类调用的方法不在父类中 [英] Calling method from java child class not in parent class

查看:53
本文介绍了从Java子类调用的方法不在父类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下基类/派生类:

Consider the following base/derived classes:

public class Car {
    private int cylinders;

    public Car(int cyl) {
        cylinders = cyl;
    }

    public int getCylinders() {
        return cylinders;
    }
}


public class SportsCar extends Car  {
    private int topSpeed;

    public SportsCar(int cyl, int top) {
        super(cyl);
        topSpeed = top;
    }

    public int getTopSpeed() {
        return topSpeed;
    }
}

现在,考虑以下两个对象:

Now, consider the following two objects:

    SportsCar lambo = new SportsCar(8,255);
    Car m5 = new SportsCar(10,240);

以下方法调用可正常编译:

The following method call compiles fine:

lambo.getTopSpeed();

但是此方法调用因错误找不到符号-方法getTopSpeed()"而中断

However this method call breaks with the error "cannot find symbol - method getTopSpeed()"

m5.getTopSpeed();   

现在,我知道 getTopSpeed 方法必须存在于基类中才能编译,因为 m5 Car 类型,因此,如果我将 getTopSpeed 包含在 Car 中,则 m5.getTopSpeed(); 可以很好地编译.

Now I understand that the getTopSpeed method must exist in the base class in order for it to compile since m5 is a Car type, so if I include getTopSpeed in Car then m5.getTopSpeed(); compiles nicely.

我的问题是:

  1. 为什么在编译时而不是运行时发生错误找不到符号-方法getTopSpeed()"?
  2. 为什么后期绑定"不能防止此错误?
  3. 假设 getTopSpeed 在程序运行时在 Car 中实现(因此可以编译),编译器是否首先检查以查看 getTopSpeed 存在于 Car 中,然后检查它是否在 SportsCar 中被覆盖,或者只是知道"它已从编译器中被覆盖并直接使用重写方法?
  1. Why does the error "cannot find symbol - method getTopSpeed()" happen at compile time and not run time?
  2. Why doesn't "late binding" prevent this error?
  3. Assuming getTopSpeed is implemented in Car (so it compiles) when the program is run, does the compiler first check to see if getTopSpeed exists in Car and then checks to see if it is over-ridden in SportsCar or does it just "know" that it is over-ridden already from the compiler and directly uses the over-ridden method?

推荐答案

  1. Java是静态类型的语言,因此在编译时应该知道变量类型.如果编译器已知的类型没有公开这种方法-则是编译失败.正是出于不允许它运行的原因.

  1. Java is statically typed language, so a variable type should be known at compile time. If type that is known to the compiler doesn't expose such a method - it is compilation failure. Exactly for the reason to not let it to runtime.

为什么要这么做?它只是发现方法主体较晚,而不是签名.静态类型仍然意味着必须在编译时满足签名.

Why should it? It just finds the method body late, not signature. Still, static typing means that signature must be satisfied at compile time.

在运行时,JVM尝试查找最具体的实现.在您的情况下,在SportsCar中.如果它是从父级继承而在子级中不存在,则使用父级的代码.

At runtime JVM tries to find the most specific implementation. In SportsCar, in your case. If it is inherited from parent but absent in child - parent's code is used.

如果您需要从变量类型不存在的特定子对象中调用方法-您可以在运行时强制转换该类,但要冒获取ClassCastException的风险.

If you need to call method from specific child which is absent in variable type - you can cast it at runtime at your own risk of getting ClassCastException.

这篇关于从Java子类调用的方法不在父类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆