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

时间:2018-02-12 04:44:31

标签: java minecraft

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

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

每个游戏类都扩展了Arena类。因此,当制作这些游戏时,它们会被添加到散列图(double,arena)中。当我遍历这些寻找特定游戏模式时,我需要能够为该游戏模式调用该方法以供玩家加入。

示例:CaptureTheFlag.join(播放器)

每个游戏都有相同的方法,称为连接和相同的参数,但每个游戏都有不同的东西,因为这些是不同的游戏。因此,当我遍历我的hashmap并找到特定值(竞技场)时,我希望能够像这样调用Capture标记连接方法:

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 个答案:

答案 0 :(得分:1)

解决方案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:使用界面:

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