我正在尝试创建一个接口继承系统,该系统使用相同的属性但始终具有进一步的派生类型。所以基属性应该被派生接口以某种方式覆盖或隐藏。
例如,派生为丈夫和妻子的两个界面,男人和女人,也是界面。Man 和 Husband 接口都有一个“sweetheart”属性,而 Woman 和 Wife 有一个“darling”属性。现在,Man 的“sweetheart”属性是 Woman 类型,而 Husband 的相同“sweetheart”属性应该是 Wife(源自 Woman)。女人和妻子的“宠儿”财产也是如此。
public interface Man // base interface for Husband
{
Woman sweetheart { get; set; }
}
public interface Woman // base interface for Wife
{
Man darling { get; set; }
}
public interface Husband : Man // extending Man interface
{
new Wife sweetheart { get; set; } // narrowing "sweetheart" property's type
}
public interface Wife : Woman // extending Woman interface
{
new Husband darling { get; set; } // narrowing "darling" property's type
}
public class RandomHusband : Husband // implementing the Husband interface
{
private RandomWife wife;
public Wife sweetheart { get { return wife; } set { wife = value; } }
}
public class RandomWife : Wife // implementing the Wife interface
{
private RandomHusband husband;
public Husband darling { get { return husband; } set { husband = value; } }
}
这段代码是错误的,它不起作用。我收到通知说我没有实现基本Man.sweetheart
和Woman.darling
属性,并且已实现Husband.sweetheart
并且Wife.darling
不会做,因为类型不匹配。有没有办法将属性的类型缩小到派生的类型?你如何在 C# 中实现它?