using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {

        static void Main(string[] args)
        {
            ClassInfo c = new ClassInfo();

            c.students = new Students();
            c.students.init();
            Console.WriteLine("我的名字叫{0},今年{1}岁了", c.students[0].name, c.students[0].age);
            Console.WriteLine("我的名字叫{0},今年{1}岁了", c.students["张三"].name, c.students["张三"].age);
        }
    }
    public class Students
    {
        Student[] students = new Student[3];

        public void init()
        {
            students[0] = new Student("张三", 24);
            students[1] = new Student("李四", 35);
            students[2] = new Student("王五", 42);
        }
        public Student this[string name]
        {
            get
            {

                for (int i = 0; i < students.Length; i++)
                {
                    if (students[i].name == name)
                    {
                        return students[i];

                    }

                }
                return null;
            }
        }

        public Student this[int index]
        {
            get { return students[index]; }
        }
    }
    public class ClassInfo
    {
        public Students students;
    }
    public class Student
    {
        public Student(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
        public string name;
        public int age;
    }
}

posted on 2010-04-23 18:30  aparche  阅读(194)  评论(0编辑  收藏  举报