How to access derived class members from an interface?

后端 未结 4 706
误落风尘
误落风尘 2021-01-07 12:26

I have three classes; Stamp, Letter and Parcel that implement an interface IProduct and they also have some of their own functionality.

public interface IP         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 12:33

    Why can't I access the additional members of derived classes from a List ?

    That is because, IProduct interface does not know about UnitPrice, Destination etc of the derived class properties.

    Are you trying add the intelligence to calculate the Amount to each of the derived class objects Stamp, Letter, Parcel ?

    Then, I would say you need to redesign a bit and use the Decorator design pattern.

    DerivedClass::Amount()
    {
      Base::Amount() + 
      //Amount logic based on derived class
    }
    

提交回复
热议问题