English 中文(简体)
• 如何在接口中安装接收器/设计器?
原标题:How do you implement getters/setters in an interface?

I m making a chess game, and I have a base piece interface that I want all the pieces to implement. I have some common variables that each need to have, but I don t want to make getters and setters in each class (seems like it goes against DRY anyway).

我所做的事被称为我的接口PieceBase,有一个称为Piece的具体班子,我实际上从Piece到PieceBase。 我不想听说过Piece的话,因为它本身没有意义,而关于这种方法的东西使我感到不安。

通常如何这样做?

具体来说,这是在 Java,但我不知道它是否用其他业务协调办公室语言有所不同。

最佳回答

它通常认为,至少在贾瓦和C++,使用继承法进行再使用(见Joshua Blach s“Effective Java”的论点)。 通常最好使用composition(或某种形式的代表团)。

在您的案例中,我把事情完全放在一起,例如<代码>。 Piece 仅为enum。 代表零件类型:

public final class Piece {

    public static enum Type {
        PAWN {
            List<Move> getLegalMoves(Color c, Location l) { /* ... */ }
        },
        BISHOP { /* ... */ },
        KNIGHT { /* ... */ },
        ROOK { /* ... */ },
        QUEEN { /* ... */ },
        KING { /* ... */ }; // i hope i didn t forget anything :P

        abstract List<Move> getLegalMoves(Color c, Location l);
        // ... etc.
    }

    private final Type type;
    private final Color color;
    private Location location;

    public Piece(Type type, Color color, Location location) {
        this.type = type;
        this.color = color;
        this.location = location;
    }

    public List<Move> getLegalMoves() {
        return type.getLegalMoves(color, location);
    }

    // ... etc.
}

因此,所有部分的共同逻辑一旦在<代码>Piece上实施,不同部分则按<代码>排列。 类型。 如果你需要知道你重新研究的哪类东西,那么你就不需要使用<条码>的<>instanceof,并做任何事情,那么你只是执行<条码>。

问题回答

看来,你想要扩大Piece和实施PieceBase...,但也许我不会看到什么。 如果你使用一个接口,执行该接口的所有班子仍须界定自己的开户/开户。

改用一个抽象的类别似乎是明智的?

因此,他们都使用同样的接收器/制造器......。 相信你会重新努力完成。

在这种情况下,我看不到接口的价值。 我建议简单地把抽象的“皮奇”类同必要的共同点和编织物加以定义,然后由您的具体班级延伸。

abstract class Piece { ... common stuff ...}
class Pawn extends Piece { ... pawn-specific stuff ... }
class Knight extends Piece { ... knight-specific stuff ... }
...




相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签