【发布时间】:2012-05-03 08:50:28
【问题描述】:
我有以下情况:
public class GeoLocation
{
public double Longitude { get; set; }
public double Latitude { get; set; }
public string LocationName { get; set; }
}
public abstract class Base
{
public abstract GeoLocation GeoLocation { get; set; }
}
public class Concrete : Base
{
public override GeoLocation GeoLocation
{
get;
set;
}
}
现在,如果我创建一个类 Concrete2,它也继承自 Base,并且我希望 GeoLocation 对象拥有更多属性:
public string Address{ get; set; }
实现这一点的最佳方法是什么?
我可以创建一个名为 GeoLocationEx : GeoLocation 的新类并将 Address 属性放在那里,但是在我的 Concrete2 对象中,我将有 2 个属性:GeoLocation 和 GeoLocationEx,我不喜欢它们......
我还可以将 GeoLocation 类设为局部类,并使用 Address 属性扩展它以用于 Concrete2 类,但我不确定这是否是对局部类的“正确”使用。
最好的方法是什么?
提前感谢您的帮助!
【问题讨论】:
-
为什么要让后代类使用扩展的
GeoLocation对象?在这种情况下,如果您想通过Base的接口使用Concrete2对象,则必须检查它是否是Concrete2对象,而不是Concrete,然后才传入GeoLocationEx对象。 -
为什么不能简单地将“地址”添加到 GeoLocation 对象?
-
我在看这篇文章时找到了解决方案:abstract class Request
where T : Parameters { T Parameters; } 类专业化:请求 { } -
从基类继承的某些类在此 GeoLocation 属性中具有与其他不同的属性...