我想隐藏基本公共财产(数据成员)在我的派生类:如何隐藏在派生类的基类的公共财产
class Program
{
static void Main(string[] args)
{
b obj = new b();
obj.item1 = 4;// should show an error but it doent ???
}
}
class a
{
public int item1 {get; set;}
public int item2 { get; set; }
}
class b : a
{
new private int item1;
}
class c : a
{
}
我有成员作为公众,因为我想要的成员要在c类中继承,但想隐藏b类中的成员,我该怎么做?
不要我有选择性地继承我想在我的基类中的变量?这就是非常糟糕的,我认为微软应该为我们提供了一个选项(可能是修改)来执行此
编辑:
,我发现自己的答案(我听到很多人讲这是不可能在C#中,但你可以种做)
我,包括情况下,代码是非常有用
class Program
{
static void Main(string[] args)
{
b obj = new b();
obj.item1 = 4; // shows an error :)
}
}
class a
{
public int item1 { get; set; }
public int item2 { get; set; }
}
class b : a
{
new public static int item1
{
get;
private set;
}
}
此代码是否正确编译? – vpram86 2009-09-18 11:15:10
我相信如果你告诉我们这个用例,还有一个替代解决方案。你为什么想这样做? – 2009-09-18 11:21:56
你的解决方案显然只隐藏了setter。 虽然它停止立即指派,没有什么可以阻止下列: ((a)中OBJ).item1 = 4; B不能说ITEM1永远不会改变,因为它可能的假设下进行编程。它停止了简单的情况,但并非每个案例。更健壮的解决方案是重构继承层次结构。 – ICR 2009-09-18 13:18:41