2016-12-25 94 views
-1

我在Java中面臨一個問題。使幾個類具有相同的屬性沒有繼承

我需要有幾個具有相同屬性的類(例如Position和boolean isWalkable)。

但我不希望這些類繼承自類Element,因爲這會阻止我稍後使用繼承。

我想到了一個接口(使接口具有屬性),但顯然你不能從一個類繼承接口。

必須有一種方法,否則我將不得不復制/粘貼我的屬性和方法。

在此先感謝任何有關如何解決此問題的想法的人。

+0

*但顯然你不能有一個接口從類繼承*:你爲什麼要這麼做?你想要類來實現你的接口。不過,您將無法共享屬性。只有方法簽名。 –

+0

組成怎麼樣?設計一個包裝類,可以引用您的實際類,以及您的位置和isWalkable字段,然後引用您的代碼中的其他位置。 –

+0

因此,我的每個類都有一個屬性元素,它是您建議的包裝類? – Irindul

回答

0

我懷疑你可能需要接口反正,如果你想一般對待你的對象 - 例如循環遍歷它們並繪製每一個。例如。假設你的元素包括「貓」和「房子」:這是對幾個系統我寫的不夠好

interface Element{ 
    public point getPosition(); 
    public boolean isWalkable(); 
} 
class Cat implements Element{ 
    private Point position; 
    private String catBreed; // example of cat-specific data 
    public point getPosition() {return position;} 
    public boolean isWalkable() {return true;} // cats can walk 
    ... 
} 
class House implements Element{ 
    private Point position; 
    private String streetAddress; // example of house-specific data 
    public point getPosition() {return position;} 
    public boolean isWalkable() {return false;} // houses cannot walk 
    .. 
} 

// Finally, using that common interface: 
Element[] allGameObjects= {new Cat(...), new Cat(...), new House(...) }; 
for(Element e:allGameObjects) 
    draw(e, e.getPosition()); 

...但對方回覆正確提到的,你可能會重構可用的組合物 - 但它可能不是100%清晰。我的意思是,如果你覺得貓或者家應該獨立於他們的位置來管理,我可以理解......但是可行的是什麼?

// Position is easy to separate: 
class Cat { String catBreed; ... } 
class House{ String streetAddress; ... } 

class ElementWrapper implements Element{ 
    Point position; 
    Object theObject; // could hold Cat or House 
    public Point getPosition() {return position;} 
    // however, isWalkable is a bit tricky... see remark below 
} 

但「isWalkable」是棘手,因爲在傳統的多態性你所期望的衆議院/貓告訴你他們是否可以走(這意味着他們無論如何都應該實現一個接口)。如果你絕對不想(或不能)擁有它,你可能會在多態性上妥協,並在instanceof中做一些事情(如果object是instanceof Cat,那麼它可以走路,如果它是instanceof House它不能走路等)。

0

您可以擴展一個抽象基類(不包含任何內容)或者您可以使用Decorator模式,如評論者所建議的,有關Decorator模式的更多信息,您可以閱讀這個link

1

爲此,我會考慮composition over inheritance

public class Main { 

    public static void main(String[] args) { 
     AgentWrapper agentWrapper = new AgentWrapper(new Agent1(), false, 1); 
     System.out.println("isWalkable: " + agentWrapper.isWalkable()); 
     System.out.println("position: " + agentWrapper.getPosition()); 
     agentWrapper.getAgent().doSomething(); 
    } 
} 

interface Agent { 
    void doSomething(); 
} 

class Agent1 implements Agent { 

    public void doSomething() { 
     System.out.println("Agent1"); 
    } 
} 

class Agent2 implements Agent { 

    public void doSomething() { 
     System.out.println("Agent1"); 
    } 
} 

class AgentWrapper { 

    private final Agent agent; //Wrapped instance. 
    private final boolean isWalkable; 
    private final int position; 

    public AgentWrapper(Agent agent, boolean isWalkable, int position) { 
     this.agent = agent; 
     this.isWalkable = isWalkable; 
     this.position = position; 
    } 

    public Agent getAgent() { 
     return agent; 
    } 

    public boolean isWalkable() { 
     return isWalkable; 
    } 
相關問題