1.写了个城堡游戏,突然报错了,子类中已经继承了父类的方法,为啥还报错??呢
2.接口
package ChenBan;
public interface scene {
//设置出口
public void setExit(String name,scene room);
//获得出口
public String getExit();
//房间中的物体
public void res();
//对当期场景的叙述
public void narration();
//对当前房间的描述
public String toString();
}
实现接口
package ChenBan;
import java.util.HashMap;
public abstract class Room implements scene {
private String description;
private HashMap<String,scene> roomList=new HashMap<String,scene>();
public Room(String description){
this.description=description;
}
//单个房间出口的初始化
public void setExit(String name,scene room){
roomList.put(name, room);
}
//获得出口
public String getExit(){
StringBuffer s=new StringBuffer();
for(String str:roomList.keySet()){
s.append(str);
s.append(roomList.get(str));
s.append("");
}
if(roomList.isEmpty()){
System.out.println("这个房间的周边没有相邻的房间入口");
}
return s.toString();
}
//去当前房间的下个出口
public Room getDirect(String direction){
Room ret=(Room)roomList.get(direction);
return ret;
}
//输入当前房间的描述
public String toString(){
return this.description;
}
}
子类
package ChenBan;
public class BedRoom extends Room{
public BedRoom(String description){
super(description);
}
@Override
public void res() {
// TODO Auto-generated method stub
}
@Override
public void narration() {
System.out.println("这是主卧");
}
}
业务
package ChenBan;
public class Initial {
//初始化当前的房间
private scene currentRoom;
//显示欢迎的信息
public void welcome(){
System.out.println("欢迎来到城堡");
System.out.println("这是一个房间探险的游戏");
System.out.println("你可以输入'go 方向'去你想去的方向");
System.out.println("你可以输入'bye'来结束游戏");
System.out.println("如果需要帮助,请输入'help'");
}
//设置所有的房间
BedRoom bedRoom=new BedRoom("卧室");
scene s=new BedRoom("");
FrontRoom frontRoom=new FrontRoom("客厅");
Kitchen kRoom=new Kitchen("厨房");
//设置房间的关联
s.setExit();//这里报错了
}
3.s.setExit();报错Syntax error on token "setExit", Identifier expected after this token
没弄明白哪里的问题,先谢谢各位了
牧羊人nacy
相关分类