Since the value
field is private
, it's not accessible in the derived class. Thus, the declaration in the derived class does not really hide anything. You shouldn't add new
to the declaration. If you do, nothing changes, except the compiler will warn you about using new
incorrectly. If the value
field was accessible in the derived class (e.g. it was public
), then you should have used new
to express your intention to hide the base member:
class A {
public int field;
}
class B : A {
public int field; // warning. `B.field` hides `A.field`.
}
Using new
will silence that warning (it will have no other effect):
class B : A {
public new int field; // Dear compiler, shut up please.
}
You can't declare a method as both override
and new
. They are mutually exclusive.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…