【发布时间】:2013-03-15 20:32:30
【问题描述】:
我知道在 C++ 中,继承是“公共”或“私有”或“受保护” 这意味着如果我将 A 类公开继承到 B 类,如下所示
class A
{
public int pub1;
private int prvt1;
protected int proc1;
}
class B : public A
{
//public int pub1;//This variable is because of inheritacne and is internal.
//public int proc1;//This variable is because of inheritacne and is internal.
public int pub2;
private int prvt2;
protected int pro2;
}
即A 类的两个变量 (pub1, proc1) 被继承,但访问说明符是公共的。 但是在C#中是这样的
class A
{
public int pub1;
private int prvt1;
protected int proc1;
}
class B : A
{
//public int pub1; //This variable is because of inheritacne and is internal.
//protected int proc1;//This variable is because of inheritacne and is internal.
public int pub2;
private int prvt2;
protected int pro2;
}
即A 类的两个变量 (pub1, proc1) 被继承,但访问说明符与 A 类中的相同。
为什么在 .NET 框架中给出这种实现。这有什么好处和坏处?
【问题讨论】:
-
@AlokSave:谢谢,它有帮助
-
其实
prvt1字段也是继承的,但只能从A内部“看到”。例如,如果类A包含处理B实例的代码,它可以更改B上的字段prvt1。 -
在非密封、非静态
class中,还有两个可能的可访问级别:internal int intnl1;和protected internal int protintnl1;