简体   繁体   English

Java多态性-如何在不同的类中调用相同的方法

[英]Java polymorphism - how to call same method in different classes

So I'm making a game in java, and I have objects all with the 2 methods; 因此,我正在用Java制作游戏,而我所有的对象都使用这两种方法。 update() and render() . update()render()

In my main class, where the game loop resides, I have to call both the update() and render() methods for every object that is updatable and renderable. 在我的游戏循环所在的主类中,我必须为每个可更新和可渲染的对象调用update()render()方法。
Is there any way to set up some form of interface where I can call the methods once and it will call it in all implemented objects? 有什么方法可以设置某种形式的接口,使我可以一次调用方法并将在所有已实现的对象中调用该方法?

The other answers are correct, however it would be much better to use the composite pattern : 其他答案是正确的,但是使用复合模式会更好:

public interface GameComponent {

    void render();

    void update();
}

public abstract class ChildComponent implements GameComponent {

    protected ContainerComponent parent; // (see below)

    // getter and setter for parent
}

public class ContainerComponent implements GameComponent {

    protected List<GameComponent> children = new ArrayList<>();

    public void add(GameComponent child) {
        this.children.add(child);
        child.setParent(this);
    }

    @Override
    public void update() {
        for (GameComponent c : this.children) {
            c.update();
        }
    }

    @Override
    public void render() {
        for (GameComponent c : this.children) {
            c.render();
        }
    }

}

Then you implement your specific GameComponent s so that they extend either ChildComponent or ContainerCompoenent : 然后,实现特定的GameComponent以便它们扩展ChildComponentContainerCompoenent

public class Player extends ChildComponent {

    @Override
    public void update() {
        // update player
    }

    @Override
    public void render() {
        // render player
    }

}

public class World extends ContainerComponent {

    @Override
    public void update() {
        super.update(); // update world's children (the player)
        // update the world (this can be done either after or before updating children,
        // you choose how to update your world)
    }

    @Override
    public void render() {
        super.render(); // render world's children (the player)
        // render the world (this can be done either after or before rendering children,
       // you choose how to render your world)
    }

}

Then, in your main loop: 然后,在您的主循环中:

// Create player and world
Player p = new Player();
World w = new World();

// Add player to world
w.add(p);

// Update everything
w.update();

// Render everything
w.render();

This way, you create a composite of GameComponent s, and then you update() and render() just the topmost-level ContainerComponent . 这样,您将创建一个GameComponent组合 ,然后只对最顶层的ContainerComponent update()render()

That is interface are for: 那是接口用于:

interface I {
    void render();
}

class A implements I {
     public void render() {
         // render an A object ...
     }
}

class B implements I {
     public void render() {
         // render an B object ...
     }
}

and anywhere you may have 在任何地方

List<I> list = new ArrayList();
list.add(new A());
list.add(new B());

for(I i:list) {
    i.render();
}

Objects of class A and B are diferent, but they implements the same contract (interface). 类A和B的对象是不同的,但是它们实现相同的协定(接口)。

This is one of the pillars of object oriented programming, polymorphism . 这是面向对象编程的支柱之一,即多态

Basically, you need to define an interface with the methods you need. 基本上,您需要使用所需的方法定义一个接口。 Note that in java, interface methods are public by default. 请注意,在Java中,默认情况下接口方法是公共的。

public interface Renderable {
  void render();
  void update();
}

And then you define the implementation. 然后定义实现。 To implement an interface you need to use the "implements" key word. 要实现接口,您需要使用“ implements”关键字。 In the example below, you'll see "implements Renderable" 在下面的示例中,您将看到“可呈现的实现”

public class MyRenderable implements Renderable {
  public void render() {
     ... // method impl
  }

  public void update() {
     ... // method impl
  }
}

And finally, you create an instance and call the methods through the interface. 最后,您创建一个实例并通过该接口调用方法。

Renderable r = new MyRenderable();
r.render();
r.update();

From here you can populate a list with the type being that of your interface. 在这里,您可以填充类型为界面类型的列表。 You can iterate over that list, calling the methods from the interface, invoking the implementations. 您可以遍历该列表,从接口调用方法,调用实现。

One of the main use of Interface is to achieve polymorphism , or the ability to perform the same operation on a number of different objects. Interface的主要用途之一是实现多态 ,即对多个不同对象执行相同操作的能力。 If different objects all implement the same interface and have the same method, you can store all of those objects in a Vector/List, for example, and iterate through the Vector calling that method on each one. 如果所有不同的对象都实现相同的接口并具有相同的方法,则可以将所有这些对象存储在一个Vector / List中,例如,并在每个对象的Vector上调用该方法进行迭代。

As per your requirements : 根据您的要求:

    interface I {
    void render();
    void update();
   }

class first implements I {
     public void update(){
       // doWhaterver you want to do 
     }
     public void render() {
         // doWhaterver you want to do 
     }
}

class second implements I {
     public void update(){
       // doWhaterver you want to do 
     }
     public void render() {
         // doWhaterver you want to do 
     }
}

Now add them to List 现在将它们添加到列表

List<I> list = new ArrayList();
list.add(new first());
list.add(new second());

for(I i:list) {
    i.update();
    i.render();
}

it will call the implemented function ,thats called Polymorphism 它将调用已实现的函数,即多态性

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

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