我有一个接口ProductType
,其中包含多个实现:RawMaterial
,PaintType
,IArticle
等。
然后我将我认为是Color
和public interface IReference
{
Color Color { get; set; }
IArticle Article { get; set; }
}
之间的复合元素的引用(SKU):
Product : IReference
然后我有几个实现,每个实现都有一个相应的IArticle实现:
ProductType
将Article
作为SemifinishedGood: IReference
,RawMaterial
将Article
作为Paint : IReference
PaintType
将Article
作为Article
所以问题是......如何覆盖public class Paint: IReference
{
public virtual Color Color { get; set; }
public virtual PaintType Article { get; set; }
}
类型,如下所示:
PaintType
这样,当我处理IArticle
而不必每次都进行投射时,我可以访问Paint.Article
个特定属性,而不仅仅是$now = time(); //current date - Date Availing the Reservation
$your_date_needed = strtotime(date('YYYY-MM-DD'));
$datediff = $your_date_needed - $now;
$daysLeft = round($datediff/(60*60*24));
if($daysLeft>0) {
echo "success! ".$daysLeft;
} else {
echo "date should not be from the past";
}
?这个架构有什么问题?
答案 0 :(得分:7)
Generic type parameters很容易解决这个问题。
public interface IReference<TArticle> where TArticle : IArticle
{
Color Color { get; set; }
TArticle Article { get; set; }
}
public class Paint : IReference<PaintType>
{
public virtual Color Color { get; set; }
public virtual PaintType Article { get; set; }
}