我正在尝试创建一个接口继承系统,该系统使用相同的属性,但始终是另一个派生类型。因此,应该以某种方式覆盖或隐藏派生接口的基本属性。
例如,采取两个界面,男人和女人,来自丈夫和妻子,也是接口。男人和丈夫的接口都有“甜心”财产,而女人和妻子则有“亲爱的”财产。现在,男人的“甜心”财产是女人,而丈夫的“甜心”财产应该是妻子(源自女人)。和女人和妻子的“亲爱的”财产一样。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#中实现它?
答案 0 :(得分:15)
您可以通过使用具体实现类型参数化Man
和Woman
接口来实现此目的:
public interface IMan<M, W>
where M : IMan<M, W>
where W : IWoman<W, M>
{
W Sweetheart { get; set; }
}
public interface IWoman<W, M>
where W : IWoman<W, M>
where M : IMan<M, W>
{
M Darling { get; set; }
}
您的实施是:
public class Man : IMan<Man, Woman>
{
public Woman Sweetheart { get; set; }
}
public class Woman : IWoman<Woman, Man>
{
public Man Darling { get; set; }
}
public class Husband : IMan<Husband, Wife>
{
public Wife Sweetheart { get; set; }
}
public class Wife : IWoman<Wife, Husband>
{
public Husband Darling { get; set; }
}
由于类型变得非常复杂,您可能需要考虑将关系移动到外部类/接口:
public interface Relationship<TMan, TWoman>
where TMan : Man
where TWoman : Woman
{
TMan Darling { get; }
TWoman Sweetheart { get; }
}
public class Marriage : Relationship<Husband, Wife>
{
}
然后,您可以在处理具体实现时使用此类来保持类型安全性:
public static void HandleMarriage(Relationship<Husband, Wife> marriage)
{
Husband h = marriage.Darling;
Wife w = marriage.Sweetheart;
}
答案 1 :(得分:3)
你仍然需要满足男人和女人的界面以及丈夫和妻子...
public class RandomWife : Wife // implementing the Wife interface
{
private RandomHusband husband;
public Husband darling { get { return husband; } set { husband = value; } }
public Man Wife.darling { get { return husband; } set { /* can't set anything */ } }
}