日期:2014-05-17  浏览次数:20806 次

关于隐藏和用重写的区别
//隐藏 
public class a
    {
        public float speed;
        public float Run(float distance)
        {
            return distance / speed;
        }
    }

    public class b : a
    {
        public  b()
        {
            speed = 40;
        }
        public new  float Run(float distance)
        {
            return 1.6f * base.Run(distance);
        }
    }


    class Program
    {

        static void Main()
        {
            b t1 = new b();
            Console.WriteLine("卡车行驶1000公里需{0}小时",t1.Run(1000));
            a a1 = t1;
            Console.WriteLine("汽车行驶1000公里需 {0}小时", a1.Run(1000));
            Console.Read();
        }



    }
输出结果为:卡车行驶1000公里需40小时
汽车行驶1000公里需25小时

//重写:
 public class a
    {
        public float speed;
        public  virtual  float   Run(float distance)
        {
            return distance / speed;
        }
    }

    public class b : a
    {
        public  b()
        {
            speed = 40;
        }
        public override  float Run(float distance)
        {
            return 1.6f * base.Run(distance);
        }
    }


    class Program
    {

        static void Main()
        {
            b t1&nbs