new与override
2009-12-15 00:06
134 查看
new:隐藏了父类的虚拟方法;在内存里面重新分配了内存空间来存放新的方法,所以仍然可以访问父类的方法;
override:重写了父类的虚拟方法;在内存里面使用的是父类方法使用的内存空间.
1 namespace ConsoleApplication3
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 Bird bird = new Chicken();
8 bird.ShowType(); //output1:Type2 is Chicken
Console.WriteLine(Chicken.x); //output: chicken x
Console.WriteLine(Bird.x); //output: bird x 仍然可以访问父类的字段
//demo2
//Chicken bird = new Chicken();
8 //bird.ShowType(); //output2:Type2 is Chicken
9 Console.ReadLine();
10 }
11 }
12
13
14 public abstract class Animal
15 {
16 public abstract void ShowType();
17
18 public void Eat()
19 {
20 Console.WriteLine("Animal always eat.");
21 }
22 }
23
24 public class Bird : Animal
25 {
public new static string x = "bird x";
26 private string type = "Bird";
27
28 public override void ShowType()
29 {
30 Console.WriteLine("Type is {0}",type);
31 }
32
33 public string color;
34
35 public string Color
36 {
37 get { return color;}
38 set {color = value;}
39 }
40 }
41
42 public class Chicken : Bird
43 {
public new static string x = "chicken x";
44 private string type = "Chicken";
45
46 public override void ShowType()
47 {
48 Console.WriteLine("Type2 is {0}", type);
49 }
50 public void ShowColor()
51 {
52 Console.WriteLine("Color is {0}",Color);
53 }
54 }
55 }
56
尝试2:
修改:
public class Chicken : Bird
{
private string type = "Chicken";
public new void ShowType()
{
Console.WriteLine("Type2 is {0}", type);
}
public void ShowColor()
{
Console.WriteLine("Color is {0}",Color);
}
}
//output1:Type is Bird
//output2:Type2 is Chicken
尝试2:
override:重写了父类的虚拟方法;在内存里面使用的是父类方法使用的内存空间.
1 namespace ConsoleApplication3
2 {
3 class Program
4 {
5 static void Main(string[] args)
6 {
7 Bird bird = new Chicken();
8 bird.ShowType(); //output1:Type2 is Chicken
Console.WriteLine(Chicken.x); //output: chicken x
Console.WriteLine(Bird.x); //output: bird x 仍然可以访问父类的字段
//demo2
//Chicken bird = new Chicken();
8 //bird.ShowType(); //output2:Type2 is Chicken
9 Console.ReadLine();
10 }
11 }
12
13
14 public abstract class Animal
15 {
16 public abstract void ShowType();
17
18 public void Eat()
19 {
20 Console.WriteLine("Animal always eat.");
21 }
22 }
23
24 public class Bird : Animal
25 {
public new static string x = "bird x";
26 private string type = "Bird";
27
28 public override void ShowType()
29 {
30 Console.WriteLine("Type is {0}",type);
31 }
32
33 public string color;
34
35 public string Color
36 {
37 get { return color;}
38 set {color = value;}
39 }
40 }
41
42 public class Chicken : Bird
43 {
public new static string x = "chicken x";
44 private string type = "Chicken";
45
46 public override void ShowType()
47 {
48 Console.WriteLine("Type2 is {0}", type);
49 }
50 public void ShowColor()
51 {
52 Console.WriteLine("Color is {0}",Color);
53 }
54 }
55 }
56
尝试2:
修改:
public class Chicken : Bird
{
private string type = "Chicken";
public new void ShowType()
{
Console.WriteLine("Type2 is {0}", type);
}
public void ShowColor()
{
Console.WriteLine("Color is {0}",Color);
}
}
//output1:Type is Bird
//output2:Type2 is Chicken
尝试2:
相关文章推荐
- 总结virtual override sealed new在方法上的应用
- new 与 override的区别 转
- C#中new和override区别
- C#的override、new、vitutal一例
- C# new virtual override 之间的区别
- C#中new、override、virtual关键字的区别
- new override的区别
- 使用 Override 和 New 关键字进行版本控制
- 重写、隐藏基类(new, override)的方法
- new , virtual , override
- override new virtual 的比较
- c#中隐藏(new)和方法重写(override)和重写(overload)的区别
- override new shadowing
- 溯因推理:为什么需要override和new两种多态方案
- 学习日志-virtual,override,new,overload
- 了解何时使用 Override 和 New 关键字(C# 编程指南)
- 有关C#关键字new、override、virtual的总结
- abstract、virtual、override、new
- 使用 Override 和 New 关键字进行版本控制(C# 编程指南)
- C#中new, override, virtual的具体用法