如何在另一个类中使用一个类的方法而不扩展
抱歉,如果我的问题听起来很奇怪,哈哈,我会尽力解释。 我有 4 个职业:Karakter、Karakters、兽人、人类。兽人和人类都扩展了 Karakter。 Karakters 是一个包含 Karakter 的 ArrayList。
我在 Orc 和 Human 中都有一个名为:public String getRace() 的方法。现在我想在 Karakters 中使用这个方法?!!当我尝试这样做时,它失败了,因为兽人和人类扩展了 Karakter,而不是 Karakters!有办法做到这一点吗?我听说过一些关于制作抽象的东西:P
谢谢
Sorry if my question sounds weird lol I'll try to explain.
I have 4 classes: Karakter, Karakters, Orc, Human. Orc and Human both extend Karakter. Karakters is an ArrayList with Karakter in it.
I have a method in both Orc and Human called: public String getRace(). Now I want to use this method in Karakters?!! When I try to do this, it fails because Orc and Human extend Karakter and not Karakters! Is there a way to do this? I heard something about making something abstract :P
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在
Karakter
中声明getRace()
方法(请注意,英文拼写是“Character”,但这里和那里都不是这样)。由于
Karakters
只知道它正在处理Karakter
类型的对象,因此它无法知道这两个实现都有getRace
方法。在基类中声明该方法可以解决这个问题。它应该如下所示:
并且
Karakter
类也必须被设为abstract
。Declare the
getRace()
method inKarakter
(note that the English spelling is "Character", but that's neither here nor there).Since
Karakters
only knows that it's dealing with objects of typeKarakter
, it can't know that both implementations have agetRace
method. Declaring the method in the base class solves this problem.It should look like this:
And the
Karakter
class will also have to be madeabstract
.当您调用
Karakters.getRace()
时,您预计会发生什么?由于
Karakters
是一个 ArrayList,那么为什么不使用一个方法getRace(intposition)
来查找感兴趣的人的种族。我不明白获得数组的竞争有什么意义。
What do you expect to happen when you call
Karakters.getRace()
?Since
Karakters
is an ArrayList then why not just have a methodgetRace(int position)
so that you can find the race of the person of interest.I don't see how getting the race of an array makes any sense.
这确实没有道理。如果您有 ArrayList,则可以对每个元素调用
getRace()
:This doesn't really make sense. If you have an ArrayList, then you can call
getRace()
on each element:要调用 Orc 或 Human 中定义的 getRace(),您需要 Orc 或 Human 或其派生类之一的对象(假设该方法在基类中不是私有的)。当然,如果 getRace() 是公共静态的,那么您可以使用类名从任何地方调用它。
To call getRace() defined in Orc or Human you need an object of Orc or Human or one of its derived classes (assuming the method is not private in base). Of course if getRace() is public static then you can use the class name to invoke it from anywhere.
您可以在实现该方法的对象上调用该方法,只要可以从当前类访问该方法即可。特别是,由于
getRace
方法是公共的,任何类都可以调用它,只要它们有适当的Orc
或Human
实例来调用即可就可以了。我认为你的症结在于你有一个卡拉克特人的列表,而不是兽人或人类。因此,
Karakters
此时只知道该对象是Karakter
,因此只能调用Karakter
类/接口中定义的方法。一种解决方案是将
getRace
方法也添加到Karakter
类中。如果在超类中返回值没有意义,那么您可以将超类抽象化(这意味着您不能直接构造它的实例,您只能构造它的一个实例)子类),并在Karakter
中声明方法 abstract:这会强制子类实现
getRace
(这不是问题在这里,正如他们无论如何所做的那样),并且意味着Karakters
现在可以确定,无论它拥有哪种Karakter
对象,都有一个调用 getRace() 方法。根据我理解您的意图,这只是解决方案的一种方法。但根本问题是
Karakter
类未定义getRace
,因此无法在该类型的引用上直接调用该方法。You can call the method on an object that implements that method, so long as it is accessible from your current class. In particular, since the
getRace
method is public, any class can call it so long as they have an appropriate instance ofOrc
orHuman
to call it on.I expect that your sticking point is that you have a list of
Karakter
s, rather than Orcs or Humans. ThusKarakters
only knows at that point that the object is aKarakter
, and so can only call methods defined in theKarakter
class/interface.One solution is to add the
getRace
method to theKarakter
class as well. If it doesn't make sense for this to return a value in the superclass, then you can make the superclass abstract (which means you cannot construct an instance of it directly, you can only construct one of its subclasses), and declare the method abstract inKarakter
too:This forces the subclasses to have an implementation of
getRace
(which isn't a problem here, as they do anyway) and means thatKarakters
can now be sure that no matter what kind ofKarakter
object it has, there is a getRace() method to call.This is just one approach to the solution based on what I understand your intent to be. But the underlying issue is that the
Karakter
class doesn't definegetRace
, and so the method cannot be called directly on references of that type.您可以在运行时测试
Karakter
的具体类型是什么,并转换为适当的子类型。另外,如果对
Karakter
有意义的话,您似乎可以将getRace()
方法推入到Karakter
中。 code> 拥有getRace()
。如果
Karakter
拥有getRace()
没有意义,例如,如果有另一种类型Alien extends Karakter
没有您可以使用附加接口进行抽象的getRace()
:这样您就可以这样做:
此外,它看起来像您的
class Karakters
扩展了ArrayList.不。优先选择组合而不是继承,并且始终使用 ArrayList的通用版本。
You can test at runtime what is the concrete type of
Karakter
and cast to the appropriate SubType.Also, it looks like you could push up the method
getRace()
intoKarakter
, if it makes sense for aKarakter
to have agetRace()
.If it doesn't make sense for
Karakter
to have agetRace()
, for instance, if there is another typeAlien extends Karakter
that doesn't have agetRace()
you could abstract with an additional interface:This way you could do:
Also, it looks like your
class Karakters
extendsArrayList
. Don't. Favor Composition over Inheritance and, always use the generic version ofArrayList<Karakter>
.