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

C# 4.0 如何在C#中为类属性赋值?,c#-4.0,c#-3.0,C# 4.0,C# 3.0,我不熟悉C#编程,并尝试使用在“Car”、“Truck”类中继承的“Vihicle”接口属性编写以下程序。我面临的问题是这个错误: 发生“System.StackOverflowException”类型的未处理异常 我在将值分配给汽车属性时得到了这个值。以下是我的代码: namespace Inheritance_Assignment_2 { interface Vihicle { string Make { get;

我不熟悉C#编程,并尝试使用在“Car”、“Truck”类中继承的“Vihicle”接口属性编写以下程序。我面临的问题是这个错误:

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

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

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();
        }
    }
}

看看你的财产:

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; }

编译器会自动将后者转换为与前者非常相似的内容(可能只是使用不同的支持变量名).

感谢您的帮助。还有一件事,我是否应该将接口Vihicle属性添加到Truck类及其子类TowingTruck???因为继承者权限就像类Truck继承接口Vihicle类TowingTruck继承Truck感谢advance@HasanBadshah:继承类时,不需要重新实现继承的成员,否。
Truck
需要实现接口的成员,但是从
Truck
继承的任何东西都已经从
Truck
本身得到了实现。非常感谢你David:)