对于我的 Java 类,我们正在学习接口、多态性、继承等。
我正在做的家庭作业是一个记忆游戏,你有一对面朝下的卡片,你一次翻开两张来寻找匹配。如果它们匹配,则它们保持可见,如果它们不匹配,则将牌翻过来,然后再选择两张牌。
到目前为止,我的设计如下:
public interface Hideable
public abstract void hide();
public abstract void show();
public interface Speakable
public abstract String speak();
public interface AnimalCard extends Hideable, Speakable
public abstract boolean equals(Object obj);
public class Animal implements AnimalCard
public void hide() { ... }
public void show() { ... }
public boolean equals(Object obj) { ... }
// What do I do for the speak method since a generic Animal
// can't speak, but I have to provide a definition since the
// Animal class is implementing the interfaces.
public class Puppy extends Animal
// Here is where I need to define the speak method.
public String speak() { ... }
我的问题在上面的评论中。我觉得我对 speak() 方法的实现不正确。