zoukankan      html  css  js  c++  java
  • C#中的索引器

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

  • 相关阅读:
    day12:crontab任务调度
    day11:组管理和权限管理
    day10:实用指令
    day09:用户管理
    day08:开机、重启和用户登录注销
    day07:vi和vim编辑器
    做一个简单的新闻客户端的一点准备
    Android学习笔记一之客户端连接服务器
    Struts2学习笔记二之Action
    Struts2学习笔记一之工作原理和struts.xml解析
  • 原文地址:https://www.cnblogs.com/tianguook/p/1718860.html
Copyright © 2011-2022 走看看