简体   繁体   English

Java:从超类变量调用子类方法

[英]Java: Calling a subclass method from superclass variable

(Question previously asked here . I did not quite get the desired answer.) (之前在这里问过问题。我没有得到想要的答案。)

A quote from a book: 一本书的引用:

What if you want to call a method that's defined by a subclass from an object that's referenced by a variable of the superclass? 如果要从超类的变量引用的对象中调用由子类定义的方法,该怎么办? Suppose that the SoftBall class has a method named riseBall that isn't defined by the Ball class. 假设SoftBall类具有一个Ball类未定义的名为riseBall的方法。 How can you call it from a Ball variable? 如何从Ball变量中调用它? One way to do that is to create a variable of the sub- class and then use an assignment statement to cast the object: 一种方法是创建子类的变量,然后使用赋值语句强制转换对象:

Ball b = new SoftBall();
SoftBall s = (SoftBall)b;
// cast the Ball to a
// SoftBall
s.riseBall();

In the code snippet above, it shows a new Softball object being created and assigned as a reference to the variable b , which is completely legal since the class Softball is a subclass of the class Ball . 在上面的代码片段中,它显示了一个新的Softball对象,该对象正在创建并分配为对变量b的引用,这是完全合法的,因为类SoftballBall类的子类。 Yet the quote states (indirectly) that you have to cast the variable to type Softball before you can use the variable to call methods from the Softball class. 然而,报价状态(间接),你必须转换的变量输入垒球 ,然后才能使用该变量调用从垒球类的方法。 Why is that? 这是为什么? Why can't I directly use the variable b of type Ball (which contains the reference to the Softball object) to call the desired method? 为什么不能直接使用Ball类型的变量b(包含对垒球对象的引用)来调用所需的方法? The variable b already has the object. 变量b已经具有对象。

(Note: I already read this post.) (注意:我已经阅读了这篇文章。)

Java is a statically typed language. Java是一种静态类型的语言。

That means that the compiler checks if the type of the variable has the method you are trying to call. 这意味着编译器将检查变量的类型是否具有您尝试调用的方法。

The type of your variable b is Ball . 变量b的类型为Ball Ball does not have a riseBall method. Ball没有riseBall方法。

That means your code would crash unless that b at runtime happens to contain a Softball (which the compiler cannot guarantee). 这意味着除非运行时b恰好包含Softball (编译器无法保证),否则您的代码将崩溃。 You may know that it does, but you have to convince the compiler, too (ie give your variables the necessary types). 您可能知道它确实如此,但是您也必须说服编译器(即为变量提供必要的类型)。

Ball b = new SoftBall();

This just means that variable b is of type Ball, but if the methods present in class Ball if are overridden in extended class SoftBall, then using variable 'b', we shall be invoking the method definition of class 'SoftBall' rather than 'Ball'. 这只是意味着变量b是Ball类型,但是如果Ball类中的方法if在扩展类SoftBall中被覆盖,则使用变量'b',我们将调用类'SoftBall'而不是'Ball'的方法定义。 '。 Now, although variable b refers to the implementation of class 'SoftBall', it still is of type class 'Ball' and class Ball does not have any method 'riseBall' 现在,尽管变量b引用了类'SoftBall'的实现,但它仍然是类'Ball'的类型,并且Ball类没有任何方法'riseBall'

Hope that makes sense. 希望有道理。

Because the methods that are exposed depends on the declared type of the variable. 因为公开的方法取决于变量的声明类型。

Ball b = new SoftBall();

b is actually a SoftBall but you are "hinding" it because you declared it as a Ball type. b实际上是一个SoftBall但是您“隐藏”它是因为您将其声明为Ball类型。 Since Ball type doesn't have a riseBall method you cannot access it by asking b . 由于Ball类型没有riseBall方法,因此无法通过询问b来访问它。

As we know java is a statically typed language . 众所周知, java是一种statically typed language

This is a feature of statically-typed languages where variables are assigned a type ahead of time, and checked at compile-time to see that the types match. 这是静态类型语言的功能,在这种类型中 ,变量会提前分配类型,然后在compile-time检查类型是否匹配。

If this code were performed on a dynamically-typed language , where the types are checked at `runtime, something like the following could be allowed: 如果此代码是在动态类型的语言 (在运行时检查类型)上执行的,则可以允许以下内容:

Ball b = new SoftBall();
if (b instanceof SoftBall){
  b.riseBall();
}

b.riseBall() is guaranteed only to execute when the instance is a SoftBall , so the call to riseBall will always work. b.riseBall()仅在实例为SoftBall时执行,因此对riseBall的调用将始终有效。

However, Java is a statically-typed language, so this type of code is not allowed. 但是,Java是一种静态类型的语言,因此不允许这种类型的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM