首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为C#中的类属性赋值?

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

Stack Overflow用户
提问于 2014-09-15 12:20:24
回答 1查看 1.6K关注 0票数 0

我对C#编程很陌生,并试图使用"Vihicle“接口属性编写以下程序,这些属性继承在'Car‘、’卡车‘类中。我面临的问题是这个错误:

出现了“System.StackOverflowException”类型的未处理异常“

我在将值赋值给汽车属性时得到了这个结果。这是我的代码:

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-09-15 12:26:21

看看你的财产:

代码语言:javascript
复制
private string make;
public string Make  // read-write instance property
{
    get
    {
        return Make;
    }
    set
    {
        Make = value;
    }
}

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

代码语言:javascript
复制
private string make;
public string Make  // read-write instance property
{
    get
    {
        return make;
    }
    set
    {
        make = value;
    }
}

属性的内部逻辑不能引用自身。实际上有些东西必须储存价值。

编辑:,除非有任何特殊的理由使用这样的属性(比如在getter/setters中需要更多的逻辑),您可以使用自动生成的属性来简化代码。所以,不是这样的:

代码语言:javascript
复制
private string make;
public string Make  // read-write instance property
{
    get
    {
        return make;
    }
    set
    {
        make = value;
    }
}

你可以用这个:

代码语言:javascript
复制
public string Make { get; set; }

编译器会自动将后者转换为与前者非常相似的东西(可能只是一个不同的支持变量名)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25848018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档