亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

與面向對象的方法作斗爭

與面向對象的方法作斗爭

藍山帝景 2023-04-13 15:02:52
我無法弄清楚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);    }}預期輸出:HallIt's DarkNorth: DineEast: BathWest: Dining
查看完整描述

3 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

獲得所需內容的“面向對象”方法是覆蓋類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;

    }

}


查看完整回答
反對 回復 2023-04-13
?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

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"));



查看完整回答
反對 回復 2023-04-13
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

這是一種做事的方法。這有點尷尬,因為您必須為每種情況檢查 null - 如果不是這種情況,您可以刪除這些檢查。


   public String getExits (){

       List<String> exits = new ArrayList<>();

        if (north != null) exits.add("North: " + north.name);

        if (south != null) exits.add("South: " + south.name);

        if (east != null) exits.add("East: " + east.name);

        if (west != null) exits.add("West: " + west.name);

        return String.join("\n", exits);

    }


查看完整回答
反對 回復 2023-04-13
  • 3 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號