Java 多态性 - 如何在不同的地方调用相同的方法 类
Java polymorphism - how to call same method in different classes
所以我在 java 中制作了一个游戏,并且我的对象都具有这 2 种方法; update()
和 render()
。
在游戏循环所在的主 class 中,我必须为每个可更新和可渲染的对象调用 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();
}
classA和B的对象不同,但实现的是同一个契约(接口)
这是面向对象编程的支柱之一,polymorphism。
基本上,您需要用您需要的方法定义一个接口。请注意,在java中,接口方法默认为public。
public interface Renderable {
void render();
void update();
}
然后定义实现。要实现接口,您需要使用 "implements" 关键字。在下面的示例中,您会看到 "implements Renderable"
public class MyRenderable implements Renderable {
public void render() {
... // method impl
}
public void update() {
... // method impl
}
}
最后,您创建一个实例并通过接口调用方法。
Renderable r = new MyRenderable();
r.render();
r.update();
从这里您可以使用您的界面类型填充一个列表。您可以遍历该列表,从接口调用方法,调用实现。
接口的主要用途之一是实现多态性,或者对多个不同的对象执行相同操作的能力对象。如果不同的对象都实现相同的接口并具有相同的方法,您可以将所有这些对象存储在一个 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();
}
它将调用实现的函数,即所谓的多态性
其他答案是正确的,但是使用 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();
}
}
}
然后您实现特定的 GameComponent
s,以便它们扩展 ChildComponent
或 ContainerCompoenent
:
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
的 复合 ,然后 update()
和 render()
只是最顶层 ContainerComponent
.
所以我在 java 中制作了一个游戏,并且我的对象都具有这 2 种方法; update()
和 render()
。
在游戏循环所在的主 class 中,我必须为每个可更新和可渲染的对象调用 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();
}
classA和B的对象不同,但实现的是同一个契约(接口)
这是面向对象编程的支柱之一,polymorphism。
基本上,您需要用您需要的方法定义一个接口。请注意,在java中,接口方法默认为public。
public interface Renderable {
void render();
void update();
}
然后定义实现。要实现接口,您需要使用 "implements" 关键字。在下面的示例中,您会看到 "implements Renderable"
public class MyRenderable implements Renderable {
public void render() {
... // method impl
}
public void update() {
... // method impl
}
}
最后,您创建一个实例并通过接口调用方法。
Renderable r = new MyRenderable();
r.render();
r.update();
从这里您可以使用您的界面类型填充一个列表。您可以遍历该列表,从接口调用方法,调用实现。
接口的主要用途之一是实现多态性,或者对多个不同的对象执行相同操作的能力对象。如果不同的对象都实现相同的接口并具有相同的方法,您可以将所有这些对象存储在一个 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();
}
它将调用实现的函数,即所谓的多态性
其他答案是正确的,但是使用 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();
}
}
}
然后您实现特定的 GameComponent
s,以便它们扩展 ChildComponent
或 ContainerCompoenent
:
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
的 复合 ,然后 update()
和 render()
只是最顶层 ContainerComponent
.