我现在有两个类基类为car派生类为bus,其中基类的weight为protected 的变量。
    class car {        public int wheel;
        protected float weight;        protected float a()
        {
            
        }
    }    class bus:car {        public void b(){
            car v1 = new car();
            
        }        
        
    }问题是:在子类bus中,不能访问受保护的weight变量。是什么原因呢。

解决方案 »

  1.   

    另外说明一点。这两个类在同一个命名空间类。我在BUS中调用,没有这个V1.weight。
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
        }    class car
        {        public int wheel;
            protected float weight;        public float a()
            {
                //代码
            }
        }    class bus : car
        {
            public void b()
            {
                car v1 = new car();
                v1.wheel = 100;
                v1.weight //没有个这个属性
            }
        }}
      

  3.   

    好象是这样的,v1.weight是另一个对象的保护成员,所以不能访问
      

  4.   

    但是protected在其子类里是可以访问的呀。
      

  5.   

    你的概念错误
    bus类继承了car类,代表着bus类内可以使用这个protected的字段weight
    你在bus类仍然实例化的是car的对象,对于car这个类来说,在bus类就是类外,当然访问不到weight这个字段
      

  6.   

    using System;class Program
    {
    static void Main(string[] args)
    { }
    }class Car
    { public int wheel;
    protected float weight; public float A()
    {
    //代码
    }
    }class Bus: Car
    {
    public void B(float weight)
    {
    //因为父类(Car)有protected的weight,所以子类(Bus)也就有了这个weight,所以可以这样写
    this.weight = weight;
    //但这里是不能访问到Car类的weight的!
    }
    }
      

  7.   

    顺便说一下,你的命名不规范,看看.NET的类库,类名和方法名首字母都是大写