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

发布于 2024-08-11 07:54:43 字数 298 浏览 6 评论 0原文

抱歉,如果我的问题听起来很奇怪,哈哈,我会尽力解释。 我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

﹎☆浅夏丿初晴 2024-08-18 07:54:43

Karakter 中声明 getRace() 方法(请注意,英文拼写是“Character”,但这里和那里都不是这样)。

由于 Karakters 只知道它正在处理 Karakter 类型的对象,因此它无法知道这两个实现都有 getRace 方法。在基类中声明该方法可以解决这个问题。

它应该如下所示:

public abstract String getRace();

并且 Karakter 类也必须被设为 abstract

Declare the getRace() method in Karakter (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 type Karakter, it can't know that both implementations have a getRace method. 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.

初见终念 2024-08-18 07:54:43

当您调用 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 method getRace(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.

不再让梦枯萎 2024-08-18 07:54:43

这确实没有道理。如果您有 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
    }
}

This doesn't really make sense. If you have an ArrayList, then you can call getRace() on each element:

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
    }
}
难如初 2024-08-18 07:54:43

要调用 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.

红衣飘飘貌似仙 2024-08-18 07:54:43

您可以在实现该方法的对象上调用该方法,只要可以从当前类访问该方法即可。特别是,由于 getRace 方法是公共的,任何类都可以调用它,只要它们有适当的 OrcHuman 实例来调用即可就可以了。

我认为你的症结在于你有一个卡拉克特人的列表,而不是兽人或人类。因此,Karakters 此时只知道该对象是 Karakter,因此只能调用 Karakter 类/接口中定义的方法。

一种解决方案是将 getRace 方法也添加到 Karakter 类中。如果在超类中返回值没有意义,那么您可以将超类抽象化(这意味着您不能直接构造它的实例,您只能构造它的一个实例)子类),并在 Karakter 中声明方法 abstract

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

     public abstract String getRace();
}

这会强制子类实现 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 of Orc or Human to call it on.

I expect that your sticking point is that you have a list of Karakters, rather than Orcs or Humans. 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.

One solution is to add the getRace method to the Karakter 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 in Karakter too:

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.

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.

咋地 2024-08-18 07:54:43

您可以在运行时测试Karakter的具体类型是什么,并转换为适当的子类型。

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

另外,如果对 Karakter 有意义的话,您似乎可以将 getRace() 方法推入Karakter 中。 code> 拥有 getRace()

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

如果 Karakter 拥有 getRace() 没有意义,例如,如果有另一种类型 Alien extends Karakter没有您可以使用附加接口进行抽象的 getRace()

public interface IRacer {
    abstract String getRace();
}

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

这样您就可以这样做:

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

此外,它看起来像您的 class Karakters 扩展了 ArrayList.不。优先选择组合而不是继承,并且始终使用 ArrayList的通用版本。

You can test at runtime what is the concrete type of Karakter and cast to the appropriate 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().

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:

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. Don't. Favor Composition over Inheritance and, always use the generic version of ArrayList<Karakter>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文