2017-04-14 90 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类抽象出来,以便仅使用派生类。