示例 在该例中,基类 MyBaseC 和派生类 MyDerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。该例说明了 new 修饰符的使用。同时也说明了如何使用完全限定名访问基类的隐藏成员。// cs_modifier_new.cs // The new modifier using System; public class MyBaseC { public static int x = 55; public static int y = 22; }public class MyDerivedC : MyBaseC { new public static int x = 100; // Name hiding public static void Main() { // Display the overlapping value of x: Console.WriteLine(x); // Access the hidden value of x: Console.WriteLine(MyBaseC.x); // Display the unhidden member y: Console.WriteLine(y); } } 输出 100 55 22 如果移除 new 修饰符,程序将继续编译和运行,但您会收到以下警告:The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'. 如果嵌套类型正在隐藏另一种类型,如下例所示,也可以使用 new 修饰符修改此嵌套类型。
new关键字就是终止该方法的虚拟性。比如: class A { public abstract void func(){}; } class B:A { public override void func(){}; } class C:B { public new void func(){}; } 那么,你可以: A a = new B(); a.func(); 但不能这么做: A a = new C(); a.func(); 而且也不能再override这个方法,因为这个方法已经被终止了虚拟属性,所以不能这样: class D:C { public override void func(){}; }
我的理解是 override 和 new的最大区别在于:override 与 virtual的搭配使用,正是为了实现C#语言的多态性(polymorphism ): Polymorphism enables you to define a method multiple times throughout your class hierarchy in such a way that the runtime calls the appropriate version of that method depending on the exact object being used. (摘自Inside C#)例如: 在基类baseC中定义了一个方法:a(){Console.writeline("baseC");} 在继承类deriveC用new对方法a进行重载,如下: deriveC1: new a() {Console.writeline("deriveC");}那么如果执行下列语句: baseC c = new deriveC(); c.a();结果是:baseC. 而不是:deriveC. 实际调用的是基类中的方法。如果换用override和virtual,则可以解决这一问题。 baseC: virtual a() {Console.writeline("baseC");} deriveC: override a(){Console.writeline("deriveC");}执行下列语句: baseC c = new deriveC(); c.a();结果是:deriveC. 实际调用的就是deriveC中的方法。ps: override 只能重载基类中用virtual, abstract 和override修饰的方法。
这个问题也.... override-覆盖(旧的没有了) new-新的(新旧共存)
public class Father { public virtual void Display() { Console.WriteLine("I am the Virtual"); } public void Show() { Console.WriteLine("I am Father"); } }public class Child:Father { public override void Display() { Console.WriteLine("I am the Override"); } new public void Show() { Console.WriteLine("I am Child"); } }static void Main(string[] args) { Father F1=new Father(); Child C1=new Child(); F1.Show(); C1.Show(); Console.WriteLine(); F1.Display(); C1.Display(); }程序的输出为: I am the Father I am the ChildI am the virtual I am the override可见,父类的方法并没有消失,当我创建一个父类的Object时同样可以调用父类所拥有的方法。 当我在父类里声明virtual时,在子类里必须要有override来覆盖它。 如果没有声明virtual,则必须要加new,否则的话编译器怎么知道你子类里的这个和父类同名同参数的方法算什么,因为你是继承的,父类里已经有了一个方法,那你又出来了一个方法和它一样,你所创建的Object到底用哪个方法呢?所以要加new。
override和new 这两个对定义属性来说是什么意思呢? public class Dbedit : System.Windows.Forms.UserControl { private System.Windows.Forms.TextBox textBox1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Dbedit() { // 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } //文本框颜色 这个怎么定义呢。是 public new Color BackColor { get { return textBox1.BackColor ; } set { textBox1.BackColor=value; } } ///这个怎么定义呢。 /*是这样,public new Color BackColor 还是这样new public Color BackColor 还是这样。override public Color BackColor 还是这样 public override Color BackColor System.Windows.Forms.UserControl有一个BackColor属性。 */ }
override和new 这两个对定义属性来说是什么意思呢? public class Dbedit : System.Windows.Forms.UserControl { private System.Windows.Forms.TextBox textBox1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Dbedit() { // 该调用是 Windows.Forms 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } //文本框颜色 这个怎么定义呢。是 public new Color BackColor { get { return textBox1.BackColor ; } set { textBox1.BackColor=value; } } ///这个怎么定义呢。 /*是这样,public new Color BackColor 还是这样new public Color BackColor 还是这样。override public Color BackColor 还是这样 public override Color BackColor System.Windows.Forms.UserControl有一个BackColor属性。 */ }
/*是这样,public new Color BackColor 还是这样new public Color BackColor 还是这样。override public Color BackColor 还是这样 public override Color BackColor System.Windows.Forms.UserControl有一个BackColor属性。 */ 这些修饰符的顺序都是无所谓的,编译都能通过。关键是你选择用new,还是用override. 你应该查过,UserControl中BackColor属性是virtual的,所以,我建议你在定义Dbedit的BackColor属性时,用override修饰为好,这样可以实现其多态性。 也就是说,如果你在别的程序中用到UserControl myDbedit = new Dbedit(),在调用myDbedit.BackColor,实际使用的就是你在Dbedit.BackColor,而不会去调用UserControl.BackColor.仔细看一下我前面的那个贴子。
/*是这样,public new Color BackColor 还是这样new public Color BackColor 还是这样。override public Color BackColor 还是这样 public override Color BackColor System.Windows.Forms.UserControl有一个BackColor属性。 */ 这些修饰符的顺序都是无所谓的,编译都能通过。关键是你选择用new,还是用override. 你应该查过,UserControl中BackColor属性是virtual的,所以,我建议你在定义Dbedit的BackColor属性时,用override修饰为好,这样可以实现其多态性。 也就是说,如果你在别的程序中用到UserControl myDbedit = new Dbedit(),在调用myDbedit.BackColor,实际使用的就是你在Dbedit.BackColor,而不会去调用UserControl.BackColor.仔细看一下我前面的那个贴子。
Override是重载父类中的方法,其方法在父类中必须是Abstract或是Virtul的
而New是覆盖父类中的方法,完全是新的方法,虽然和父类中的方法同名字,但是完全无关!
new 是新的方法。可以访问以前覆盖的方法。
1——
OVERRIDE和NEW的方法都必须是基类中VIRTUAL或ABSTRACT的方法2——
OVERRIDE的方法是可以通过BASE前缀调用基类中被OVERRIDE的方法的3——
NEW的方法是一个全新的方法,不能使用BASE前缀来调用基类中被NEW后的方法。
在该例中,基类 MyBaseC 和派生类 MyDerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。该例说明了 new 修饰符的使用。同时也说明了如何使用完全限定名访问基类的隐藏成员。// cs_modifier_new.cs
// The new modifier
using System;
public class MyBaseC
{
public static int x = 55;
public static int y = 22;
}public class MyDerivedC : MyBaseC
{
new public static int x = 100; // Name hiding
public static void Main()
{
// Display the overlapping value of x:
Console.WriteLine(x); // Access the hidden value of x:
Console.WriteLine(MyBaseC.x); // Display the unhidden member y:
Console.WriteLine(y);
}
}
输出
100
55
22
如果移除 new 修饰符,程序将继续编译和运行,但您会收到以下警告:The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.
如果嵌套类型正在隐藏另一种类型,如下例所示,也可以使用 new 修饰符修改此嵌套类型。
class A
{
public abstract void func(){};
}
class B:A
{
public override void func(){};
}
class C:B
{
public new void func(){};
}
那么,你可以:
A a = new B();
a.func();
但不能这么做:
A a = new C();
a.func();
而且也不能再override这个方法,因为这个方法已经被终止了虚拟属性,所以不能这样:
class D:C
{
public override void func(){};
}
Polymorphism enables you to define a method multiple times throughout your class hierarchy in such a way that the runtime calls the appropriate version of that method depending on the exact object being used. (摘自Inside C#)例如:
在基类baseC中定义了一个方法:a(){Console.writeline("baseC");}
在继承类deriveC用new对方法a进行重载,如下:
deriveC1: new a() {Console.writeline("deriveC");}那么如果执行下列语句:
baseC c = new deriveC();
c.a();结果是:baseC. 而不是:deriveC. 实际调用的是基类中的方法。如果换用override和virtual,则可以解决这一问题。
baseC: virtual a() {Console.writeline("baseC");}
deriveC: override a(){Console.writeline("deriveC");}执行下列语句:
baseC c = new deriveC();
c.a();结果是:deriveC. 实际调用的就是deriveC中的方法。ps: override 只能重载基类中用virtual, abstract 和override修饰的方法。
override-覆盖(旧的没有了)
new-新的(新旧共存)
{
public virtual void Display()
{
Console.WriteLine("I am the Virtual");
} public void Show()
{
Console.WriteLine("I am Father");
}
}public class Child:Father
{
public override void Display()
{
Console.WriteLine("I am the Override");
}
new public void Show()
{
Console.WriteLine("I am Child");
}
}static void Main(string[] args)
{
Father F1=new Father();
Child C1=new Child(); F1.Show();
C1.Show();
Console.WriteLine();
F1.Display();
C1.Display();
}程序的输出为:
I am the Father
I am the ChildI am the virtual
I am the override可见,父类的方法并没有消失,当我创建一个父类的Object时同样可以调用父类所拥有的方法。
当我在父类里声明virtual时,在子类里必须要有override来覆盖它。
如果没有声明virtual,则必须要加new,否则的话编译器怎么知道你子类里的这个和父类同名同参数的方法算什么,因为你是继承的,父类里已经有了一个方法,那你又出来了一个方法和它一样,你所创建的Object到底用哪个方法呢?所以要加new。
这两个对定义属性来说是什么意思呢? public class Dbedit : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Dbedit()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
//文本框颜色 这个怎么定义呢。是
public new Color BackColor
{
get
{
return textBox1.BackColor ;
}
set
{
textBox1.BackColor=value;
}
}
///这个怎么定义呢。
/*是这样,public new Color BackColor 还是这样new public Color BackColor
还是这样。override public Color BackColor 还是这样 public override Color BackColor
System.Windows.Forms.UserControl有一个BackColor属性。
*/
}
这两个对定义属性来说是什么意思呢? public class Dbedit : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Dbedit()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
//文本框颜色 这个怎么定义呢。是
public new Color BackColor
{
get
{
return textBox1.BackColor ;
}
set
{
textBox1.BackColor=value;
}
}
///这个怎么定义呢。
/*是这样,public new Color BackColor 还是这样new public Color BackColor
还是这样。override public Color BackColor 还是这样 public override Color BackColor
System.Windows.Forms.UserControl有一个BackColor属性。
*/
}
还是这样。override public Color BackColor 还是这样 public override Color BackColor
System.Windows.Forms.UserControl有一个BackColor属性。
*/
这些修饰符的顺序都是无所谓的,编译都能通过。关键是你选择用new,还是用override.
你应该查过,UserControl中BackColor属性是virtual的,所以,我建议你在定义Dbedit的BackColor属性时,用override修饰为好,这样可以实现其多态性。
也就是说,如果你在别的程序中用到UserControl myDbedit = new Dbedit(),在调用myDbedit.BackColor,实际使用的就是你在Dbedit.BackColor,而不会去调用UserControl.BackColor.仔细看一下我前面的那个贴子。
还是这样。override public Color BackColor 还是这样 public override Color BackColor
System.Windows.Forms.UserControl有一个BackColor属性。
*/
这些修饰符的顺序都是无所谓的,编译都能通过。关键是你选择用new,还是用override.
你应该查过,UserControl中BackColor属性是virtual的,所以,我建议你在定义Dbedit的BackColor属性时,用override修饰为好,这样可以实现其多态性。
也就是说,如果你在别的程序中用到UserControl myDbedit = new Dbedit(),在调用myDbedit.BackColor,实际使用的就是你在Dbedit.BackColor,而不会去调用UserControl.BackColor.仔细看一下我前面的那个贴子。
如果用new 在设计窗体的时候从属性窗体修改它的BackColor属性它的效果可以看到,
但在程序运行的时候,没有看到它修改了的颜色的效果。为什么会这样,是不是一个BUG。要不你用上面的代码试试看。
就是这几句。
public class Dbedit : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Dbedit()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
//文本框颜色 这个怎么定义呢。是
public new Color BackColor
{
get
{
return textBox1.BackColor ;
}
set
{
textBox1.BackColor=value;
}
} }