落魄joker  
why so serious

今天看了Anytao大侠写的有关base和this关键字的文章(http://kb.cnblogs.com/page/42316/),有2个小问题让我比较困扰,先把代码贴出来

 1using System;
 2namespace Anytao.net.My_Must_net
 3{
 4    public class Action
 5    {
 6        public static void ToRun(Vehicle vehicle)
 7        {
 8            Console.WriteLine("{0} is running.", vehicle.ToString());
 9        }

10    }

11    public class Vehicle
12    {
13        private string name;
14        private int speed;
15        private string[] array = new string[10];
16
17        public Vehicle()
18        {
19        }

20        //限定被相似的名称隐藏的成员
21        public Vehicle(string name, int speed)
22        {
23            this.name = name;
24            this.speed = speed;
25        }

26        public virtual  void ShowResult()
27        {
28            Console.WriteLine("The top speed of {0} is {1}.", name, speed);
29        }

30        public void Run()
31        {
32            //传递当前实例参数
33            Action.ToRun(this);
34        }

35        //声明索引器,必须为this,这样就可以像数组一样来索引对象
36        public string  this[int param]
37        {
38            get{return array[param];}
39            set{array[param] = value;}
40        }

41    }

42    public class Car: Vehicle
43    {
44        //派生类和基类通信,以base实现,基类首先被调用
45        //指定创建派生类实例时应调用的基类构造函数
46        public Car()
47            : base("Car"200)
48        { }
49
50        public Car(string name, int speed)
51            : this()
52        { }
53
54        public override void ShowResult()
55        {
56            //调用基类上已被其他方法重写的方法
57            base.ShowResult();
58            Console.WriteLine("It's a car's result.");
59        }

60    }

 在46行-52行中,假设建立Car类的对象时传递了两个参数,这时调用Car(string name,int speed)构造函数,之后立即把控制权传送给Car()构造函数,因为这里:base("Car",200)的原因,输出的话始终只能是Car,200。这样做构造函数的意义在哪里呢? 这是其一。

 下面是自己写的测试代码:

 Car car = new Car("tt",300,100000);

 初始化对象car,对应调用Car(string name ,int speed,int price)

 Car(string name ,int speed,int price)转移控制权到Car(),对字段name和speed进行赋值(此时name和speed的值为Car和200)

接着执行Car()构造函数,然后是Car(string name ,int speed,int price)构造函数(此时name,speed和price的值为tt,300,100000)

当执行到car.showResult()的时候name和speed的值立即又变成Car和200了,不知道这到底是什么原因呢?

 

 

 1namespace BaseTest
 2{
 3    public class Action
 4    {
 5        public static void AcRun(Vehicle vehicle)
 6        {
 7            Console.WriteLine("{0} is running", vehicle);
 8        }

 9    }

10
11    public class Vehicle
12    {
13        protected string name;
14        protected int speed;
15
16        public Vehicle()
17        { }
18
19        public Vehicle(string name, int speed)
20        {
21            this.name = name;
22            this.speed = speed;
23        }

24
25        public virtual void showResult()
26        {
27            Console.WriteLine("The Top Speed of {0} is {1} ", name, speed);
28        }

29
30        public void Run()
31        {
32            Action.AcRun(this);
33        }

34    }

35
36    public class Car:Vehicle
37    {
38        int price;
39        public Car() 
40            : base("Car"200)
41        { }
42
43        public Car(string name, int speed,int price)
44            : this()
45        {
46            this.price = price;
47        }

48
49        public override void showResult()
50        {
51            Console.WriteLine("The Top Speed of {0} is {1} and price is {2} ", name, speed,price);
52        }

53    }

54
55    public class Audi:Car
56    {
57        
58        public Audi()
59            : base("Audi"300,10000)
60        { }
61
62        public Audi(string name, int speed)
63            : this()
64        {
65            
66        }

67
68        public override void showResult()
69        {
70            base.Run();
71            Console.WriteLine("It's Audi's result");
72        }

73    }

74
75    class Program
76    {
77        static void Main(string[] args)
78        {
79           //Audi audi = new Audi();
80           Car car = new Car("tt",300,100000);
81           car.showResult();
82           //car.Run();
83           //audi.Run();
84           //audi.showResult();
85           Console.ReadLine();
86        }

87    }

88}

89

 

 

 

posted on 2008-10-28 01:42  bravecheese  阅读(260)  评论(0编辑  收藏  举报