我有以下情形:PHP抽象類和接口
abstract class Contractor {
// Generic contractor methods...
}
abstract class PrivatePerson extends Contractor {
// Adds methods specific to private contractors
}
abstract class Company extends Contractor {
// Adds methods specific to Company contractors
}
class CustomerPrivate extends PrivatePerson {
// Customers that are contractors, but physical persons
}
class CustomerCompany extends Company {
// Customers that are contractors, but companies
}
並與供應商和經銷商,它可以是私人或者公司發生同樣的情況。現在的問題是:如何強制CustomerPrivate和CustomerCompany類的對象同時是Customer類(我尚未定義),供應商和經銷商也是如此。在這種情況下使用接口是一種很好的做法?
interface Customer {
}
class PrivateCustomer extends PrivatePerson implements Customer {
// Customers that are physical persons, but are CUSTOMERS!
}
感謝您的任何建議!
但CustomerPrivate類的對象如何能夠在客戶的同一時間實例?那麼我應該如何定義客戶類? – mlattari
非常感謝!你幫了我很多。 – mlattari
我嘗試過性狀,但它部分解決問題。我可以在對象的特徵中使用特性和方法,但對象不是特性的實例....對於接口,我有相反的情況;-) – mlattari