如果Boat类需要继承物品类,也就是说,Boat类即是交通工具,也是属于物品的一种,代码该做怎样的调整:抽象类: MaterialObject:抽象物品类 AbstracTransport:抽象交通工具类 (place:所在地;capacity:容量;goodes:货物列表) moveTo(String destination)移动到目的地 AbstractRule:抽象规则类 judge()根据规则判断结果 AbstractGame:抽象游戏类 (gameOverRule:游戏结束规则; gameSuccessRule:游戏输赢规则; gameData:游戏数据) play()执行游戏 Animal:动物类 addedToRecipe(MaterialObject m)添加某物品进该动物的食谱 isFood(MaterialObject m)判断某物品是否在它的食谱中 canBeEat(MaterialObject m)判断某物品能不能被吃到 eat(MaterialObject m)吃某样食物 Person:人类 Plante:植物类 Boat:船类 crossRiver()过河 crossRiver(Person person)将人运载过河 crossRiver(Person person,MaterialObject m)将人和某样物品运载过河 board(MaterialObject m)物品m上船 crossRiver(MaterialObject m)物品m下船 CrossRiverRule:判断是否都渡过河的规则类 hasCross(MaterialObject m)物品m是否渡过了河 ObjectExistRule:判断物品是否都还存在的规则类 GameOverRule:判断游戏是否结束的规则类 GameSuccessRule:判断游戏输赢的规则类 GameUI:游戏界面类
时间: 2023-06-19 22:05:50 浏览: 88
代码应该做如下调整:
```java
// 抽象物品类
abstract class MaterialObject {
protected String name; // 物品名称
public MaterialObject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract boolean isFood(); // 判断是否是食物
}
// 抽象交通工具类
abstract class AbstractTransport {
protected String place; // 所在地
protected int capacity; // 容量
protected List<MaterialObject> goods; // 货物列表
public AbstractTransport(String place, int capacity) {
this.place = place;
this.capacity = capacity;
this.goods = new ArrayList<>();
}
public String getPlace() {
return place;
}
public int getCapacity() {
return capacity;
}
public List<MaterialObject> getGoods() {
return goods;
}
public abstract void moveTo(String destination); // 移动到目的地
}
// 抽象规则类
abstract class AbstractRule {
public abstract boolean judge(); // 根据规则判断结果
}
// 抽象游戏类
abstract class AbstractGame {
protected AbstractRule gameOverRule; // 游戏结束规则
protected AbstractRule gameSuccessRule; // 游戏输赢规则
protected Object gameData; // 游戏数据
public abstract void play(); // 执行游戏
}
// 动物类
class Animal extends MaterialObject {
protected List<MaterialObject> recipe; // 食谱
public Animal(String name) {
super(name);
this.recipe = new ArrayList<>();
}
public void addToRecipe(MaterialObject m) {
recipe.add(m);
}
@Override
public boolean isFood() {
return true;
}
public boolean canBeEat(MaterialObject m) {
return recipe.contains(m);
}
public void eat(MaterialObject m) {
if (canBeEat(m)) {
System.out.println("吃掉了" + m.getName());
} else {
System.out.println("不能吃" + m.getName());
}
}
}
// 人类
class Person extends Animal {
public Person(String name) {
super(name);
}
@Override
public boolean isFood() {
return false;
}
}
// 植物类
class Plant extends MaterialObject {
public Plant(String name) {
super(name);
}
@Override
public boolean isFood() {
return true;
}
}
// 船类
class Boat extends AbstractTransport {
public Boat(String place, int capacity) {
super(place, capacity);
}
public void crossRiver() {
// 过河
}
public void crossRiver(Person person) {
// 将人运载过河
}
public void crossRiver(Person person, MaterialObject m) {
// 将人和某样物品运载过河
}
public void board(MaterialObject m) {
// 物品m上船
}
public void disembark(MaterialObject m) {
// 物品m下船
}
}
// 判断是否都渡过河的规则类
class CrossRiverRule extends AbstractRule {
private List<AbstractTransport> transports;
public CrossRiverRule(List<AbstractTransport> transports) {
this.transports = transports;
}
@Override
public boolean judge() {
// 判断是否都渡过河
return false;
}
}
// 判断物品是否都还存在的规则类
class ObjectExistRule extends AbstractRule {
private List<MaterialObject> objects;
public ObjectExistRule(List<MaterialObject> objects) {
this.objects = objects;
}
@Override
public boolean judge() {
// 判断物品是否都还存在
return false;
}
}
// 判断游戏是否结束的规则类
class GameOverRule extends AbstractRule {
private AbstractGame game;
public GameOverRule(AbstractGame game) {
this.game = game;
}
@Override
public boolean judge() {
// 判断游戏是否结束
return false;
}
}
// 判断游戏输赢的规则类
class GameSuccessRule extends AbstractRule {
private AbstractGame game;
public GameSuccessRule(AbstractGame game) {
this.game = game;
}
@Override
public boolean judge() {
// 判断游戏输赢
return false;
}
}
// 游戏界面类
class GameUI {
private AbstractGame game;
public GameUI(AbstractGame game) {
this.game = game;
}
public void show() {
// 显示游戏界面
}
}
```
阅读全文