2012-08-15 171 views
0

我有一個名爲Post的實體類。 Post的父項基於一個名爲AbstractPost的抽象類。 AbstractPost有一個變量「作者」,其設置者的簽名是在一個名爲PostInterface的接口中定義的。擴展一個抽象類來擴展另一個抽象類,並且需要重寫父抽象類實現的接口

我處於一個場景,我需要覆蓋作者的制定者,但不想修改父母(ofcourse)。

這些是父類:

class Post extends AbstractPost 
abstract class AbstractPost implements PostInterface 
interface PostInterface 

我有以下類別:

class MyPost extends MyAbstractPost 
abstract class MyAbstractPost extends AbstractPost implements MyPostInterface 
interface MyPostInterface extends PostInterface 

但是,我得到的錯誤:

Fatal error: Declaration of MyAbstractPost::setAuthor() must be compatible with that of PostInterface::setAuthor()

我讀過的孩子繼承父類的「實現」部分(這裏有什麼是正確的行話?),所以我想知道如何重寫那個。或者我不以正確的方式擴展/實施?

謝謝!

+0

在setAuthor方法中究竟需要改變什麼?由於您在界面中定義了方法簽名,而您的其他類未正確實施該方法,因此會出現該錯誤。你可以在錯誤信息中提到的接口和抽象類中發佈代碼嗎? – martynthewolf 2012-08-15 08:47:05

+0

你不能。如果你有 - 這是一種設計氣味 - 你將需要引入一種新的代理方法。 – 2012-08-15 08:47:50

+0

@ delete-me-please好的,我該怎麼做? – 2012-08-15 08:51:52

回答

0

答案是否定的。

重載方法簽名失敗了接口的目的。