using System;
    using System.Collections.Generic;
    using System.Text;    namespace 大学生
    {
        public class gpas
        {            //public int avg;
            public gpas(int a)
            {
                this.avg = a;
            }
            public int avg
            {
                set
                {
                   
                    avg = value;
                   
                }
                get
                {
                    return avg;
                }
            }
                    }
        public class arrylist {
            gpas[] gpa;
            public arrylist(int count)
            {
                gpa = new gpas[count];
            }
            public gpas this[int year]
            {
                get {
                    if(year<=0||year>4){
                        Console.WriteLine("年纪无效");
                        return null;
                    }
                    int index = year - 1;
                    return gpa[index];
                }
                set
                {
                    if (year <= 0 || year > 4)
                    {
                        Console.WriteLine("年纪无效");
                        return ;
                    }
                    int index = year - 1;
                     gpa[index]=value;
                }            }
        }       public class Program
        {
            static void Main(string[] args)
            {
                arrylist alist = new arrylist(4);
                gpas one = new gpas(88);
                gpas two = new gpas( 78);
                gpas three = new gpas( 89);
                gpas four = new gpas( 88);
                alist[0] = one;
                alist[1] = two;
                alist[2] = three;
                alist[3] = four;
                Console.WriteLine("请输入有效的年级");
                int count = int.Parse(Console.ReadLine());
                gpas gp = alist[count];
                Console.WriteLine(gp.avg);
            }
        }
    }
一调试,他就说在avg的读写属性哪报错 好像是递归?请高手指点,本人在线等,急急!!!!

解决方案 »

  1.   

    public int avg  属性名改一下,否则就是死循环了
      

  2.   

    或者把类改成下面的
    public class gpas 

        private int _avg; 
        public gpas(int a) 
        { 
             this._avg= a; 
        } 
        public int avg 
        { 
             set 
             { 
                _avg = value; 
             } 
             get 
             { 
                 return _avg; 
             } 
         } 
      

  3.   

    问题出在那个avg属性上,它没给变量赋值,倒是给自己赋值了。
    建议你给属性名称更改一下,同时给变量赋值
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace Test
    {
        public class People
        {
            private int _intAge;
            public People(int intAge)
            {
                this._intAge = intAge;
            }
            public int Age
            {
                set { _intAge = value; }
                get { return _intAge; }
            }
        }    public class ArrList
        {
            People[] people;
            public ArrList(int count)
            {
                people = new People[count];
            }
            public People this[int year]
            {
                get
                {
                    if (year <= 0 && year > 4)
                    {
                        Console.WriteLine("年纪无效");
                    }
                    return people[year>0?year-1:1];
                }
                set
                {
                    if (year <= 0 && year > 4)
                    {
                        Console.WriteLine("年纪无效");
                    }
                    people[year > 0 ? year - 1 : 1] = value;
                }
            }
        }
        public class Program
        {
            static void Main(string[] args)
            {
                ArrList list = new ArrList(4);
                People p1 = new People(88);
                People p2 = new People(78);
                People p3 = new People(89);
                People p4 = new People(88);
                list[0] = p1;
                list[1] = p2;
                list[2] = p3;
                list[3] = p4;
                Console.WriteLine("请输入有效的年级");
                People people = list[int.Parse(Console.ReadLine())];
                Console.WriteLine(people.Age);
                Console.ReadKey();
            }
        }
    }