如何在C#中为类属性赋值?

时间:2014-09-15 12:20:25

标签: c#-4.0 c#-3.0

我是C#编程的新手,并尝试使用“Car”,“Truck”类中继承的“Vihicle”界面属性编写以下程序。我面临的问题是这个错误:

  

发生了“System.StackOverflowException”类型的未处理异常“

我在将值分配给Car属性时得到了这个。这是我的代码:

namespace Inheritance_Assignment_2
{
    interface Vihicle
    {
        string Make
        {
            get;
            set;
        }
        String Model
        {
            get;
            set;
        }
        int Year
        {
            get;
            set;
        }
        void DisplayInfo();
        float calculateMileage();
    }
    class Car : Vihicle
    {
        private string make;
        public string Make  // read-write instance property
        {
            get
            {
                return Make;
            }
            set
            {
                Make = value;
            }
        }
        private string model;
        public string Model  // read-write instance property
        {
            get
            {
                return Model;
            }
            set
            {
                Model = value;
            }
        }
        private int year;
        public int Year  // read-write instance property
        {
            get
            {
                return Year;
            }
            set
            {
                Year = value;
            }
        }
        public void DisplayInfo()
        {
            Console.WriteLine(Make);
            Console.WriteLine(Model);
            Console.WriteLine(Year);

        }
        public float calculateMileage()
        {
            Random random = new Random();
            float value = random.Next(10, 20);
            return value;
        }
    }
    class Truck : Vihicle
    {
        private string make;
        public string Make  // read-write instance property
        {
            get
            {
                return Make;
            }
            set
            {
                Make = value;
            }
        }
        private string model;
        public string Model  // read-write instance property
        {
            get
            {
                return Model;
            }
            set
            {
                Model = value;
            }
        }
        private int year;
        public int Year  // read-write instance property
        {
            get
            {
                return Year;
            }
            set
            {
                Year = value;
            }
        }
        public void DisplayInfo()
        {
            Console.WriteLine(Make);
            Console.WriteLine(Model);
            Console.WriteLine(Year);

        }

        public float calculateMileage()
        {
            throw new NotImplementedException();
        }
    }
    class TowingTruck : Truck
    {
        public string TowingCapacity  // read-write instance property
        {
            get
            {
                return TowingCapacity;
            }
            set
            {
                TowingCapacity = value;
            }
        }
        public void DisplayInfo()    // Overrided function of class truck because this function doing some extra printing of
        {                           //TowingCapacity that is present in this TowingTruck Child of Truck Class
            Console.WriteLine(Make);
            Console.WriteLine(Model);
            Console.WriteLine(Year);
            Console.WriteLine(TowingCapacity);

        }

        public float calculateMileage()
        {
            throw new NotImplementedException();
        }
    }
    class DeliveryTruck : Truck
    {
      public string Make  // read-write instance property
        {
            get
            {
                return Make;
            }
            set
            {
                Make = value;
            }
        }
        public string Model  // read-write instance property
        {
            get
            {
                return Model;
            }
            set
            {
                Model = value;
            }
        }
        public int Year  // read-write instance property
        {
            get
            {
                return Year;
            }
            set
            {
                Year = value;
            }
        }
        /*
        public void DisplayInfo()
        {

            Console.WriteLine(Make);
            Console.WriteLine(Model);
            Console.WriteLine(Year);

        }

        public float calculateMileage()
        {
         //   throw new NotImplementedException();
            return 0;
        }
     */
    }
    class Program
    {
        static void Main(string[] args)
        {
            //while (true) // Loop indefinitely
            //{
            //    string name;
            //    int age;
            //    double height;

            //    Console.Write("Enter your name: ");
            //    name = Console.ReadLine();
            //    Console.Write("Enter your age: ");
            //    age = Convert.ToInt32(Console.ReadLine());
            //    Console.Write("Enter your height: ");
            //    height = Convert.ToDouble(Console.ReadLine());

            //    //Print a blank line
            //    Console.WriteLine();

            //    //Show the details you typed
            //    Console.WriteLine( name);
            //    Console.WriteLine( age);
            //    Console.WriteLine("Height is ", height);
            //    Console.WriteLine('\n');

            //}
            Car C = new Car();
            float rnum = C.calculateMileage();
            Console.WriteLine("Here is the Milage : " + rnum);
            C.Make = System.Console.ReadLine();
            System.Console.WriteLine("The employee information:");
            System.Console.WriteLine("Employee name: {0}", C.Make);

            //Console.Write("Enter your Model : ");
            //C.Model = Console.ReadLine();
            //Console.WriteLine(C.Model);
            //Console.ReadLine();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

查看您的房产:

private string make;
public string Make  // read-write instance property
{
    get
    {
        return Make;
    }
    set
    {
        Make = value;
    }
}

当您从Make读取值时,它在内部从Make读取值(与写入值相同),这会导致无限递归。您需要从保存值的变量读取/写入:

private string make;
public string Make  // read-write instance property
{
    get
    {
        return make;
    }
    set
    {
        make = value;
    }
}

属性的内部逻辑无法引用本身。实际上必须存储价值。

编辑除非有任何特殊原因要使用此类属性(例如在getter / setter中需要更多逻辑),否则您可以使用自动生成的属性来简化代码。所以不要这样:

private string make;
public string Make  // read-write instance property
{
    get
    {
        return make;
    }
    set
    {
        make = value;
    }
}

你可以使用它:

public string Make { get; set; }

编译器自动将后者转换为与前者非常相似的东西(可能只有不同的后备变量名)。