与面向对象的方法作斗争

我无法弄清楚getExits()需要什么才能获得问题请求的输出。


//构造函数


public class Room {


    private String name;

    private String description;

    private Room north;

    private Room east;

    private Room south;

    private Room west;


    public Room (String name, String description){

        this.name = name;

        this.description = description;

    }


    public Room getEast(){

        return this.east;

    }


    public String getExits (){

        //

    }   


    public String getName(){

        return this.name;

    }


    public Room getNorth(){

        return this.north;

    }


    public Room getWest(){

        return this.west;

    }


    public Room getSouth(){

        return this.south;

    }


    public void setExits (Room n, Room e, Room w, Room s){

        this.north = n;

        this.east = e;

        this.west = w;

        this.south = s;

    }


    public String toString(){

        return String.format("%s\n%s\n%s", this.name, this.description,getExits());

    }

}

//主要方法


public class Tester{


    public static void main(String []args){


        Room hall = new Room ("Hall", "It's Dark");

        Room bed = new Room ("Bed", "Tiny Room");

        Room bath = new Room ("Bath", "Toilets here");

        Room dine = new Room ("Dine", "Table and chairs");

        hall.setExits(bed, bath, dine, null);


        System.out.println(hall);


    }

}

预期输出:


Hall

It's Dark

North: Dine

East: Bath

West: Dining


蓝山帝景
浏览 103回答 3
3回答

繁花不似锦

获得所需内容的“面向对象”方法是覆盖类toString()中的方法Room,以便它返回房间的名称。然后修改getExits(),如下所示:public String getExits (){    StringBuilder sb = new StringBuilder();    if(this.north != null) sb.append(this.north.toString()).append(" North") else sb.append("No Exit for: North");    ...    return sb.toString();}....public class Room {    private String name;    ...    @Override    public String toString() {        return this.name;    }}

ITMISS

exit不是您可以用String. 在 OO 世界中,它应该是对更有意义的对象的引用。我会和public Collection<Room> getExits();或者public Map<String, Room> getExits();它准确地描述了您可以从大厅到达哪里。在这里,我们假设“出口”是通往另一个房间的门口。你可以回来Arrays.asList(northRoom, eastRoom, southRoom, westRoom);或者Map<String, Room> map = new HashMap<>();map.put("north", northRoom);...return map;然后您将能够提供String返回集合中的任何表示。它就像一个放置在大厅里的标志,可以帮助人们导航。尽管它可以用另一个标志(更详细/准确的标志)代替,但建筑物的结构是不变的,您不会改变它。您只是以不同的方式表示它。String simpleSign = "You can go to: " + getExits().stream().map(Object::toString).collect(Collectors.join(", "));或者String detailedSign = "Directions to go: " + getExits().entrySet().stream().map(e -> e.getKey() + " -> " + e.getValue().toString()).collect(Collectors.join("\n"));

冉冉说

这是一种做事的方法。这有点尴尬,因为您必须为每种情况检查 null - 如果不是这种情况,您可以删除这些检查。&nbsp; &nbsp;public String getExits (){&nbsp; &nbsp; &nbsp; &nbsp;List<String> exits = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; if (north != null) exits.add("North: " + north.name);&nbsp; &nbsp; &nbsp; &nbsp; if (south != null) exits.add("South: " + south.name);&nbsp; &nbsp; &nbsp; &nbsp; if (east != null) exits.add("East: " + east.name);&nbsp; &nbsp; &nbsp; &nbsp; if (west != null) exits.add("West: " + west.name);&nbsp; &nbsp; &nbsp; &nbsp; return String.join("\n", exits);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java