2017-04-14 58 views
2

我正在使用方法鏈接編寫DerivedClass(extends SuperClass)。例如,Java:派生類中的方法鏈接

public class SuperClass { 
    protected int responseCode; 
    protected String responseMessage; 

    public void setResponseCode(int code) { 
      this.responseCode = code; 
    } 

    public int getResponseCode() { 
      return responseCode; 
    } 

    public SuperClass withResponseCode(int code) { 
      setResponseCode(code); 
      return this; 
    } 

    public void setResponseMessage(String message) { 
      this.responseMessage = message; 
    } 

    public String getResponseMessage() { 
      return responseMessage; 
    } 

    public SuperClass withResponseMessage(String message) { 
      setResponseMessage(message); 
      return this; 
    } 

}

而且

public class DerivedClass extends SuperClass { 
    protected String credentialType; 
    protected String credentialId; 

    public void setCredentialType(String type) { 
      this.credentialType = type; 
    } 

    public String getCredentialType() { 
      return credentialType; 
    } 

    public DerivedClass withCredentialType(String type) { 
      setCredentialType(type); 
      return this; 
    } 


    public void setCredentialId(String id) { 
      this.credentialId = id; 
    } 

    public String getCredentialId() { 
      return credentialId; 
    } 

    public DerivedClass withCredentialId(String id) { 
      setCredentialId(id); 
      return this; 
    } 

    public static void main(String[] args) { 
      DerivedClass dc = new DerivedClass(); 
      /*dc.setResponseCode(200); 
      dc.setResponseMessage("SUCCESS"); 
      dc.setCredentialType("MobileIdentifier"); 
      dc.setCredentialId("678882");*/ 

      dc.withResponseCode(200) 
        .withResponseMessage("SUCCESS") 
        .withCredentialType("MobileIdentifier") 
        .withCredentialId("678882"); 

      System.out.println("Derived Class: Code - " + dc.getResponseCode() + " - Response Message - " + dc.getResponseMessage() + " - Credential Type - " + dc.getCredentialType()); 
    } 

}

在上述的彙編我越來越:

DerivedClass.java:41: error: cannot find symbol 
        .withCredentialType("MobileIdentifier") 
        ^
symbol: method withCredentialType(String) 
location: class SuperClass 
1 error 

爲什麼我收到這個時CredentialType DerivedClass中的字段而不是SuperClass中的字段?如何使用DerivedClass對象鏈接SuperClass & DerivedClass的混合方法?

+0

因爲你對'withResponseMessage'的調用返回一個'SuperClass','SuperClass'沒有'withCredentialType'方法。 – csmckelvey

+0

它有它 - publicResponseClassResponseMessage(String message) –

+0

這不是我正在談論的那個。該方法返回一個'SuperClass',然後你嘗試在返回的對象上調用'withCredentialType'方法,因爲該方法**在'SuperClass'中不存在**。 – csmckelvey

回答

1

方法withReponseMessage()返回SuperClass類型的對象。

SuperClass沒有方法稱爲withCredentialType()這就是爲什麼你會收到錯誤信息。

解決此問題的一個策略是使SuperClass抽象,然後定義所有API方法,即使方法是抽象的。否則,你將不得不投的是從第1種方法調用(這可能使你的程序容易在未來的失配例外返回的對象。

+0

這有效。 dc.withCredentialType(「MobileIdentifier」) .withCredentialId(「678882」) .withResponseCode(200) .withResponseMessage(「SUCCESS」); –

+0

這種情況下的訂單會產生差異 –

+1

如果訂單有所作爲,那麼您沒有正確地遵循設計模式。 – csmckelvey

1

您需要將其向下到DerivedClass,或者使用DerivedClass實例從一開始,

3

正如其他人所指出的,您獲得的編譯錯誤,因爲你的方法SuperClass返回SuperClass類型,它不包含您的DerivedClass的方法。

正確的方法來解決這是爲了使SuperClass抽象並在上使用泛型用遞歸型(也稱爲F-bound type):

public abstract class SuperClass<T extends SuperClass<T>> { 

    // TODO attributes, getters and setters 

    public T withResponseCode(int code) { 
     setResponseCode(code); 
     return (T) this; // this cast is absolutely safe! 
    } 

    // TODO other SuperClass builder methods 
} 

然後,您可以定義DerivedClass如下:

public class DerivedClass extends SuperClass<DerivedClass> { 

    // TODO attributes, getters and setters 

    public DerivedClass withCredentialType(String type) { 
     setCredentialType(type); 
     return this; 
    } 

    // TODO other DerivedClass builder methods 
} 

而且不會有任何更多的編譯錯誤。這種方法的不足之處在於,它迫使您將SuperClass類抽象出來,以便僅使用派生類。