很難用詞來解釋我所追求的內容,但希望下面的代碼示例和註釋足夠。基本上我想SubClass sc = new Subclass().method1()
行來返回子類實例。父類要返回子類
public class SuperClass {
public SuperClass method1()
{
//do whatever
return this
}
}
public class SubClass extends SuperClass {
//we inherit method 1
//method2
public SubClass method2()
{
//do whatever
return this
}
}
//succesfully returns instance of Sublass, but...
SubClass sc = new Subclass().method2()
//...the following line returns an instance of SuperClass and not Sublass
//I want Sublass's instance, without having to using overides
//Is this possible?
SubClass sc = new Subclass().method1()
編輯:----------------------------用例場景---------- ---------------------
Message myMessage = new ReverseTransactionMessageBuilder()
.policyNo(POLICY_NO) //on ReverseTransactionMessageBuilder
.audUserId(AUD_USER_ID) //on inherited MessageBuilder
.audDate(new Date()) //on inherited MessageBuilder
.processNo(EProcessConstants.FINANCE_MANUAL_ADJUSTMENT.getProcessCd()) //on inherited MessageBuilder
.serviceName("finance.ProcessReversalCmd") //on inherited MessageBuilder
.create(); //create is overridden so this is ReverseTransactionMessageBuilder
第一件事烈注意到的是,sbrattla方式讓我打電話給這些.audDate().XXX()方法以任何順序。與類結構上面你不得不呼籲最後sublcass方法(或使用一個非常醜陋的施法)
是不是就足夠了投它? –
我同意。它應該工作。 – fireshadow52
不,不是用於我打算使用它的用例 – n4rzul