简体   繁体   English

在不同的类之间访问相同的方法?

[英]Access the same method among different classes?

I am programming a simple platformer game, and I have several types of platforms. 我正在编写一个简单的平台游戏,我有几种类型的平台。 I created a class for the most simple type and made the rest subclasses, with the only difference between each class being the value of their variables (so they all share the same variable and method names). 我为最简单的类型创建了一个类,并创建了其余的子类,每个类之间的唯一区别是它们的变量值(因此它们共享相同的变量和方法名称)。

In my collision detection, I loop through a HashMap . 在我的碰撞检测中,我遍历HashMap Within that HashMap are ArrayLists of the instances of each class. 在该HashMap是每个类的实例的ArrayLists But when I use a nested loop to loop through try to call their methods implicitly, I have found that I cannot access these methods without explicitly declaring which class I want to call the method from. 但是当我使用嵌套循环来遍历尝试隐式调用它们的方法时,我发现如果没有明确地声明我想从哪个类调用该方法,我就无法访问这些方法。

I have done research, although the only way I can see of doing this is to loop through the instances of each class separately, meaning one loop per class; 我已经做过研究,虽然我能看到这样做的唯一方法是分别遍历每个类的实例,这意味着每个类一个循环; I would rather not do this, since it would be a lot more code than I feel is necessary. 我宁愿不这样做,因为它会比我认为必要的代码多得多。

Simple, make each platform class implement an IPlatform interface or extand a base class. 简单,使每个平台类实现一个IPlatform接口或者扩展一个基类。 Look up java polymorphism and interfaces. 查找java多态和接口。

In order to be able to call a common method on classes of different types you need to give your objects a common supertype declaring the common method - ie they should have a common superclass, or implement a common interface. 为了能够在不同类型的类上调用公共方法,您需要为对象提供一个公共超类型来声明公共方法 - 即它们应该具有公共超类,或者实现公共接口。

Interfaces provide an easier way of declaring common functionality, because a class can implement multiple interfaces, but it can extend only one class. 接口提供了一种更简单的方式来声明常用功能,因为类可以实现多个接口,但它只能扩展一个类。

Provide an interface with the common method, then declare the map to use objects of that interface, ie 提供具有公共方法的接口,然后声明地图以使用该接口的对象,即

interface CommonInterface {
    void commonMethod(int arg);
}

class One implements CommonInterface {
    public void commonMethod(int arg) {
        ...
    }
}

class Two implements CommonInterface {
    public void commonMethod(int arg) {
        ...
    }
}

Here is what you can do now: 以下是您现在可以做的事情:

Map<String,CommonInterface> myMap = new HashMap<>();
myMap.put("one", new One());
myMap.put("two", new Two());
for (Map.Entry<String,CommonInterface> e : myMap.entrySet()) {
    System.out.println(e.getKey());
    CommonInterface c = e.getValue();
    c.commonMethod(123);
}

Are your subclasses overriding the common methods from the super class? 您的子类是否重写了超类中的常用方法? In other words, are your subclass' common methods declared in your simpler class? 换句话说,你的子类是在你的简单类中声明的常用方法吗? If it is the case, you can simply call the method as if it is a simple class: 如果是这种情况,您可以简单地调用该方法,就好像它是一个简单的类:

public abstract class Fruit {
    public abstract void method();
}

public class Apple extends Fruit {
    @Override
    public void method() {
        System.out.println("I'm an apple");
    }
}

public class Orange extends Fruit {
    @Override
    public void method()
        System.out.println("I'm an orange");
    }
}

Using this you can simply call your method from any fruit, since it has your method declared. 使用此方法,您只需从任何水果中调用您的方法,因为它已声明您的方法。 No need to know which fruit it is. 不需要知道它是哪种水果。 The following code: 以下代码:

Fruit fruit = new Orange();
fruit.method();

will output: "I'm an orange". 将输出:“我是一个橘子”。

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

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