class Vehicle
    {
        public int wheels;//公共成员,轮子数
        protected float weight;//保护成员,重量
        public void F()
        {
            wheels = 4;
            weight = 10;
        }
    }
    class Car : Vehicle
    { 
        int passenages;
        public void F()//有意隐藏,要用什么new
        {
            Vehicle v2 = new Vehicle();
            v2.wheels = 6;
            v2.weight = 6;//出错,无法访问
        }
    }Car类从Vehicle继承,//为出错位置,按道理,派生类应该可以从基类里面继承到protected变量啊
还有就是方法F()问题,有意隐藏,要用什么new这样的警告

解决方案 »

  1.   

    Vehicle v2 = new Vehicle();
    你把这个换成Car V2=new Car()就不会错了,想想原因吧。
      

  2.   

    第二个应该是告诉你显示指定重写吧,我对C#不是很熟悉,如果是VB.net应该是用overrides显示指定重写该函数
      

  3.   

    搂主应该这样改:  
         
    new public void F()//有意隐藏,要用什么new
    {
        Vehicle v2 = new Vehicle();
        v2.wheels = 6;
        v2.weight = 6;//出错,无法访问
    }
    这是强制重写基类方法的意思
      

  4.   

    或者搂主这样做,是比较常用的方法:
        class Vehicle
        {
            public int wheels;//公共成员,轮子数
            protected float weight;//保护成员,重量
            protected visual void F()
            {
                wheels = 4;
                weight = 10;
            }
        }
        class Car : Vehicle
        { 
            int passenages;
            protected override void F()//有意隐藏,要用什么new
            {
                Vehicle v2 = new Vehicle();
                v2.wheels = 6;
                v2.weight = 6;//出错,无法访问
            }
        }把基类方法F做成虚函数