gpt4 book ai didi

java - 从扩展另一个对象的对象调用方法(不是您想象的那样)

转载 作者:行者123 更新时间:2023-12-02 11:38:36 26 4
gpt4 key购买 nike

我有一个问题。如何在对象的减速中放置一个方法?让我解释。我正在制作的是 Minecraft 网络。我们提供的所有游戏都是他们自己的类别。

示例:CaptureTheFlag 是它自己的对象,但它扩展了一个名为 arena 的类。

每个游戏类别都扩展了竞技场类别。因此,当制作这些游戏时,它们会被添加到 HashMap (double、a​​rena)中。当我迭代这些寻找特定的游戏模式时,我需要能够调用该游戏模式的方法以便玩家加入。

示例:CaptureTheFlag.join(player)

每个游戏都有相同的称为 join 的方法和相同的参数,但每个游戏都会执行不同的操作,因为这些是不同的游戏。因此,当我迭代 HashMap 并找到特定值(arena)时,我希望能够像这样调用捕获标志连接方法:

Iterator<Entry<Double, Arena>> it = 
ArenaManager.local_arenas.entrySet().iterator();
while (it.hasNext()) {
Entry<Double, Arena> pair = it.next();
if (pair.getKey().doubleValue() == game_mode) {



pair.getValue().join(player)

}

}

请注意,这不是完整的代码。 (我正在手机上输入此内容)

有没有人有解决办法?

最佳答案

解决方案1:使用抽象方法:

import java.util.HashMap;
import java.util.Map;

public class Foo {

static class Player {
private final String name;

Player(final String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}

static abstract class Arena {
abstract void join(Player player);
}

static class CaptureTheFlagArena extends Arena {

@Override
void join(final Player player) {
System.out.printf("%s is joining a capture the flag game!%n", player);
}
}

static class DominationArena extends Arena {

@Override
void join(final Player player) {
System.out.printf("%s is joining a domination arena!%n", player);
}
}

/**
* Prints:
* Bob is joining a domination arena!
* Bob is joining a capture the flag game!
*/
public static void main(String[] args) {
final Map<Double, Arena> map = new HashMap<>();
map.put(7d, new CaptureTheFlagArena());
map.put(6d, new DominationArena());
for (Map.Entry<Double, Arena> entry : map.entrySet()) {
entry.getValue().join(new Player("Bob"));
}
}
}

解决方案 2:使用接口(interface):

import java.util.HashMap;
import java.util.Map;

public class Foo {

static class Player {
private final String name;

Player(final String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}

interface Joinable {
void join(Player player);
}

static abstract class Arena implements Joinable {

}

static class CaptureTheFlagArena extends Arena {

@Override
public void join(final Player player) {
System.out.printf("%s is joining a capture the flag game!%n", player);
}
}

static class DominationArena extends Arena {

@Override
public void join(final Player player) {
System.out.printf("%s is joining a domination arena!%n", player);
}
}

static class ChessGame implements Joinable {

@Override
public void join(final Player player) {
System.out.printf("%s is joining a chess game!%n", player);
}
}

/**
* Prints:
* Bob is joining a domination arena!
* Bob is joining a capture the flag game!
* Bob is joining a chess game!
*/
public static void main(String[] args) {
final Map<Double, Joinable> map = new HashMap<>();
map.put(7d, new CaptureTheFlagArena());
map.put(6d, new DominationArena());
map.put(2d, new ChessGame());
for (Map.Entry<Double, Joinable> entry : map.entrySet()) {
entry.getValue().join(new Player("Bob"));
}
}
}

关于java - 从扩展另一个对象的对象调用方法(不是您想象的那样),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48739676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com