using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    class Program
    {
        public class Person    //定义人的属性,包括 年龄和身高
        {
            private int age;
            private float hight;
            public int Age
            {
                get { return Age; }
                set { Age = value; }
            }
            public float Hight
            {
                get { return Hight; }
                set { Hight = value; }
            }        }
        public class Team   //定义小组树形,每个小组包含5个人
        { 
            private Person [] person = new Person[5];
            public Person this[int index]
            {
                get { return person[index]; }
            }
        }        private Team[] team=new Team[3];   //定义3个小组        static void Main(string[] args)
        {
//           想在主程序中对任何一个小组的任何一个人的任何一种属性读取。
//           ***************************** //  第i个小组的第j个人的年龄是i+j+1;
//           ***************************** //  如何写代码?
        }
    }
}

解决方案 »

  1.   

    先实例化对象然后用对象 "."出属性.索引器也就是直接用对象后面加中括号...比如
    Class cl = new Class();
    person a = cl[1];这个样子的.
      

  2.   

    private Person [] person = new Person[5]; 
    只是准备了一个能放5个Person的数组,并没有创建Person本身。所有Perseon this[index]返回的都是null。比如你可以改成这样:
    private Person [] person = new Person[]{new Person(), new Person(), new Person(), new Person(), new Person()};同样道理(换成static,因为要从static Main中读取):
    static private Team[] team=new Team[]{new Team(),new Team(),new Team()};  然后就简单了:
    static void Main(string[] args)
    {
         team[2][3].Age = 2 + 3;
    }
      

  3.   

            private static Team[] team = new Team[3];  //定义3个小组         static void Main(string[] args)
            {
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 5; j++)
                    {
                        team[i][j].Age = i + j + 1;
                        team[i][j].Hight = 170;
                    }
            }
      

  4.   

    using System;namespace ConsoleApplication1
    {
        class Program
        {
            public class Person    
            {
                private int age;
                private float hight;
                public int Age
                {
                    get { return Age; }
                    set { Age = value; }
                }
                public float Hight
                {
                    get { return Hight; }
                    set { Hight = value; }
                }            public Person()
                { 
                
                }            public Person(int age, float hight)
                {
                    this.age = age;
                    this.hight = hight;
                }
            }
            public class Team  //定义小组树形,每个小组包含5个人 
            {
                private Person[] person;            public Team(Person [] person)
                {
                    this.person = person;
                }            public Person this[int index]
                {
                    get { return person[index]; }
                }
            }        static void Main(string[] args)
            {
                Person[] persons = new Person[2];
                persons[0] = new Person(9, 1.2f);
                persons[1] = new Person(10, 1.3f);
                Team[] team = new Team[3];
                team[0] = new Team(persons);
                
            }
        }
      

  5.   

    属性很就直接用".属性名"就OK了撒,还有你person这个类的2个属性似乎没有什么必要,你这样写完全和把字段改成public没区别,并不是所有字段都一定要用private,也不是所有字段都要用属性的,有些没必要的,就不用麻烦了.关于索引器,其实就像是把对象数组化了一样,和用数组没撒区别,就把一个person的对象看成一个数组元素来用就OK拉
      

  6.   

    如果使用的是VS2005的话,可以使用List<T>。
    比如让Team继承List:
    public class Team:List<Person>  // 

       ....        

    这样,直接使用Team[index].Age了。