简体   繁体   English

如何在不扩展的情况下使用另一个类中的类的方法

[英]How to use a method from a class in another class without extending

Sorry if my question sounds weird lol I'll try to explain. 对不起,如果我的问题听起来很奇怪,我会试着解释一下。 I have 4 classes: Karakter, Karakters, Orc, Human. 我有4个班:Karakter,Karakters,Orc,Human。 Orc and Human both extend Karakter. 兽人和人类都延伸了卡拉克特。 Karakters is an ArrayList with Karakter in it. Karakters是一个带有Karakter的ArrayList。

I have a method in both Orc and Human called: public String getRace(). 我在Orc和Human中都有一个方法叫做:public String getRace()。 Now I want to use this method in Karakters?!! 现在我想在Karakters中使用这种方法?!! When I try to do this, it fails because Orc and Human extend Karakter and not Karakters! 当我尝试这样做时,它失败了,因为Orc和Human扩展了Karakter而不是Karakters! Is there a way to do this? 有没有办法做到这一点? I heard something about making something abstract :P 我听到了一些关于制作抽象的东西:P

Thanks 谢谢

Declare the getRace() method in Karakter (note that the English spelling is "Character", but that's neither here nor there). 声明getRace()的方法Karakter (注意,英文拼写是“字符”,但那是不伦不类)。

Since Karakters only knows that it's dealing with objects of type Karakter , it can't know that both implementations have a getRace method. 由于Karakters只知道它在处理类型的对象Karakter ,它无法知道两个实现有getRace方法。 Declaring the method in the base class solves this problem. 在基类中声明方法可以解决此问题。

It should look like this: 它应该如下所示:

public abstract String getRace();

And the Karakter class will also have to be made abstract . Karakter课程也必须是abstract

What do you expect to happen when you call Karakters.getRace() ? 当你打电话给Karakters.getRace()时,你会发生什么?

Since Karakters is an ArrayList then why not just have a method getRace(int position) so that you can find the race of the person of interest. 由于Karakters是一个ArrayList,为什么不只是有一个方法getRace(int position)这样你就可以找到感兴趣的人的种族。

I don't see how getting the race of an array makes any sense. 我不知道如何让阵列的竞争变得有意义。

This doesn't really make sense. 这没有多大意义。 If you have an ArrayList, then you can call getRace() on each element: 如果你有一个ArrayList,那么你可以在每个元素上调用getRace()

class Karakters {
    ArrayList<Karakter> kars = new ArrayList<Karakter> ();
    public String getRace () {
        for (Karakter k: kars) {
            ... uh ... which one to return?
        }
        return null; // List is empty
    }
}

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). 要调用Orc或Human中定义的getRace(),您需要一个Orc或Human对象或其派生类之一(假设该方法在base中不是私有的)。 Of course if getRace() is public static then you can use the class name to invoke it from anywhere. 当然,如果getRace()是public static,那么您可以使用类名从任何地方调用它。

You can test at runtime what is the concrete type of Karakter and cast to the appropriate SubType. 您可以在运行时测试什么是Karakter的具体类型并转换为适当的SubType。

for(Karakter k : karakters) {
   if (k instanceof Orc) {
       ((Ork)k).getRace();
   } else if (k instanceof Human) {
       ((Human)k).getRace();
   }
}

Also, it looks like you could push up the method getRace() into Karakter , if it makes sense for a Karakter to have a getRace() . 此外,它看起来像你可以在方法 getRace()Karakter ,如果它是有道理的,一个KaraktergetRace()

class/interface Karakter {
   abstract String getRace();
}

If it doesn't make sense for Karakter to have a getRace() , for instance, if there is another type Alien extends Karakter that doesn't have a getRace() you could abstract with an additional interface: 如果它没有意义的KaraktergetRace()举例来说,如果有其他类型的Alien extends Karakter不具有getRace()你可以用抽象额外的接口:

public interface IRacer {
    abstract String getRace();
}

public class Human extends Karakter implements IRacer { ... }
public class Orc extends Karakter implements IRacer { ... }

This way you could do: 这样你就可以:

for(Karakter k : karakters) {
   if (k instanceof IRacer) {
       ((IRacer)k).getRace();
   }
}

Also, it looks like your class Karakters extends ArrayList . 此外,看起来你的class Karakters扩展了ArrayList Don't. 别。 Favor Composition over Inheritance and, always use the generic version of ArrayList<Karakter> . 支持Composition over Inheritance,并始终使用ArrayList<Karakter>的通用版本。

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 of Orc or Human to call it on. 特别是,由于getRace方法是公共的,所以任何类都可以调用它,只要它们具有适当的OrcHuman实例来调用它。

I expect that your sticking point is that you have a list of Karakter s, rather than Orcs or Humans. 我希望你的关键点是你有一个Karakter的名单,而不是兽人或人类。 Thus Karakters only knows at that point that the object is a Karakter , and so can only call methods defined in the Karakter class/interface. 因此, Karakters只知道该对象是一个Karakter ,因此只能调用Karakter类/接口中定义的方法。

One solution is to add the getRace method to the Karakter class as well. 一个解决方案是将添加getRace方法将Karakter类为好。 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 in Karakter too: 如果在超类中返回一个值没有意义,那么你可以使超类抽象 (这意味着你不能直接构造它的实例,你只能构造它的一个子类),并声明方法Karakter 摘要也是:

public abstract class Karakter
{
    /*
     ...
     ... Same as before
     ...
     */

     public abstract String getRace();
}

This forces the subclasses to have an implementation of getRace (which isn't a problem here, as they do anyway) and means that Karakters can now be sure that no matter what kind of Karakter object it has, there is a getRace() method to call. 这迫使子类有一个getRace的实现(这里不是问题,因为它们无论如何都是这样)并且意味着Karakters现在可以确定无论它有什么类型的Karakter对象,都有一个getRace()方法打电话。

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 define getRace , and so the method cannot be called directly on references of that type. 但根本的问题是, Karakter类没有定义getRace ,所以该方法不能直接在该类型的引用调用。

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

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