繁体   English   中英

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

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

因此,我正在用Java制作游戏,而我所有的对象都使用这两种方法。 update()render()

在我的游戏循环所在的主类中,我必须为每个可更新和可渲染的对象调用update()render()方法。
有什么方法可以设置某种形式的接口,使我可以一次调用方法并将在所有已实现的对象中调用该方法?

其他答案是正确的,但是使用复合模式会更好:

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();
        }
    }

}

然后,实现特定的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)
    }

}

然后,在您的主循环中:

// 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();

这样,您将创建一个GameComponent组合 ,然后只对最顶层的ContainerComponent update()render()

那是接口用于:

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 ...
     }
}

在任何地方

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

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

类A和B的对象是不同的,但是它们实现相同的协定(接口)。

这是面向对象编程的支柱之一,即多态

基本上,您需要使用所需的方法定义一个接口。 请注意,在Java中,默认情况下接口方法是公共的。

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

然后定义实现。 要实现接口,您需要使用“ implements”关键字。 在下面的示例中,您将看到“可呈现的实现”

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

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

最后,您创建一个实例并通过该接口调用方法。

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

在这里,您可以填充类型为界面类型的列表。 您可以遍历该列表,从接口调用方法,调用实现。

Interface的主要用途之一是实现多态 ,即对多个不同对象执行相同操作的能力。 如果所有不同的对象都实现相同的接口并具有相同的方法,则可以将所有这些对象存储在一个Vector / List中,例如,并在每个对象的Vector上调用该方法进行迭代。

根据您的要求:

    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 
     }
}

现在将它们添加到列表

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

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

它将调用已实现的函数,即多态性

暂无
暂无

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

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