如何忽略/绕过重写的方法? [英] How to ignore/bypass an overridden method?

查看:57
本文介绍了如何忽略/绕过重写的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Java 还很陌生,想知道如何完成以下任务,以及是否被认为是糟糕的风格,即使可能.谢谢.

I'm fairly new to Java and would like to know how to accomplish the following task, and also, whether it is considered bad style, even if it is possible. Thank you.

Fish f;  // Fish is a superclass,
Tuna t = new Tuna();  // to which Tuna is a subclass.

f=t;  // the Fish object "f" now refers to the Tuna object "t".

// Both Fish and Tuna have an identical method (signature wise) called swim() ,
f.swim();  // and so Tuna's overridden swim() method is invoked here.

但是我现在可以使用相同的f.swim()"语法来调用 Fish 的swim() 方法吗?

But can I now get Fish's swim() method to be invoked, using the same "f.swim()" syntax?

// I would now like Fish's swim() method to be invoked here,
// but is it bad style and/or am I missing some point about OOP?
f.swim();

回复:答案,谢谢大家!关于 SO 用户 Rinke 在下面的回答——他说你可以将你的金枪鱼实例分配给金枪鱼和鱼类型的变量,但它永远是金枪鱼."

Re: answers, thanks people! Regarding SO user Rinke's answer below - he states that "You can assign your tuna instance to both tuna and fish typed variables, but it'll always be a tuna."

这句话的最后一部分让我的新手 OOD 大脑想知道 - 为什么允许超级对象"引用子对象"?这种灵活性有什么好处?允许 Fish 对象能够在"指代鱼或金枪鱼时切换"有什么好处?谢谢.

The last part of that sentence got my novice OOD brain wondering - why allow a "super object" to refer to a "sub object" anyway? What is the benefit of this flexibility? What benefit is there in allowing a Fish object the ability to "switch between" referring to either a fish or a tuna? Thank you.

编辑 2:

下面是一些示例代码,用于说明 SO 用户 Rinke 的回答编辑"响应的概念,如下所示.

Here is some example code to illustrate the concept of SO user Rinke's "answer to edit" response, below.

Bear b = new Bear();
Fish f = getAnyFish();
b.eat(f);

Fish getAnyFish(){
    //To toggle the returned fish type, change true to false
    if (true) return new Tuna();
    else return new Salmon();
}

推荐答案

简短的回答是否".这里的混淆来自运行时类型和声明类型之间的差异.您可以将金枪鱼实例分配给金枪鱼和鱼类型的变量,但它始终是金枪鱼.因此,如果您对该对象调用游泳方法,金枪鱼将像金枪鱼一样游泳(即将调用覆盖的方法).

The short answer is "No". The confusion here comes from the difference between run-time types and declared types. You can assign your tuna instance to both tuna and fish typed variables, but it'll always be a tuna. Hence, if you call the swim method on that object the tuna will swim like a tuna (i.e. the overridden method will be called).

请注意,您可以通过调用 super.swim() 从子类内部调用超类的 Swim 方法.

Note that you can call the swim method of a superclass from within the subclass by invoking super.swim().

当然,如果您不覆盖游泳方法,那么金枪鱼只需从fish 继承该方法.在这种情况下,fish-code 将在 tuna 实例上运行.

Of course, if you don't override the swim method then tunas simply inherit the method from fish. In that case the fish-code will be run on the tuna instance.

回答

您并不总是控制所使用的代码.也许您需要将您的金枪鱼提供给接受鱼的其他人的 API.

You don't always control the code you use. Maybe you need to provide your tuna to someone else's API that accepts fish.

从相反的角度来看:也许您正在实施熊市.熊吃鱼.你不想关心它是金枪鱼还是鲑鱼.所以你得到了 voideat(Fish f),大自然母亲(由你控制之外的一段代码实现)为你的熊提供鱼.

From the opposite perspective: maybe you're implementing a bear. Bears eat fish. You don't want to care if it's a tuna or a salmon. So you get void eat(Fish f), where mother nature (implemented by a piece of code outside of your control) provides fish to your bear.

离开有趣的鱼的例子:看看Collections.sort().您可以对任何列表进行排序.是 ArrayList 还是 LinkedList 都没有关系.

To leave the funny fish example: look at Collections.sort(). You can sort any list. It doesn't matter if it's an ArrayList or a LinkedList.

这篇关于如何忽略/绕过重写的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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