zoukankan      html  css  js  c++  java
  • 索引

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace _9.索引

    {

        class Program

        {

            static void Main(string[] args)

            {

                int[] values = { 3,5,7,9};

                int i = values[1];

                Person p1 = new Person();

                p1[1] = "小明";

                Console.WriteLine(p1[1]+p1[2]);

                Console.WriteLine(p1["tom",3,9]);//索引也可以重载。

                Console.ReadKey();

            }

        }

        class Person

        {

            private string FirstName="大毛";

            private string SecondName="二毛";

            public string this[string name, int x, int y]

            {

                get

                {

                    return name + x + y;

                }

            }

            public string this[int index]//中括号里的就是索引。

            {

                get

                {

                    if (index == 1)

                    {

                        return FirstName;

                    }

                    else if (index == 2)

                    {

                        return SecondName;

                    }

                    else

                    {

                        throw new Exception("这是个错误的序号啊亲!");

                    }

                }

                set

                {

                    if (index == 1)

                    {

                        FirstName = value;

                    }

                    else if (index == 2)

                    {

                        SecondName = value;

                    }

                    else

                    {

                        throw new Exception("这是个错误的序号啊亲!");

                    }

                }

            }

        }

    }

  • 相关阅读:
    守护进程、互斥锁、生产者消费者模型
    实现并发编程的基础理论
    udp协议
    进程与进程池
    tcp协议产生-粘包问题的解决方案
    day21面向对象_类
    day16_面向过程编程与模块导入
    day15_函数递归_匿名函数_内置函数
    三、运算符(阶段二)
    二、(续)基础语法--常量、变量和注释(阶段二)
  • 原文地址:https://www.cnblogs.com/gyt-xtt/p/3639047.html
Copyright © 2011-2022 走看看