zoukankan      html  css  js  c++  java
  • C#学习笔记七索引器

    索引器

     

    namespace 索引器

    {

        class Program

        {

            static void Main(string[] args)

            {

                Person p1 = new Person();

                p1[1] = "小明";

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

                Console.ReadKey();

            }

        }

        public class Person

        {

            private string firstName = "大毛";

            private string secondName = "二毛";

     

            //下面这个就是索引器

            public string this[int index]//索引可以有不止一个参数,setget可以只有一个

            {

                set

                {

                    if (index == 1)

                    {

                        firstName = value;

                    }

                    else if (index == 2)

                    {

                        secondName = value;

                    }

                    else

                    {

                        throw new Exception("输入的值不合适");

                    }

                }

                get

                {

                    if (index == 1)

                    {

                        return firstName;

                    }

                    else if (index == 2)

                    {

                        return secondName;

                    }

                    else

                    {

                        throw new Exception("输入的值不合适");

                    }

                }

            }

        }

    }

  • 相关阅读:
    05、汇编语言--环境搭建
    04、计算机基础--编码
    03、计算机基础--数制
    02、计算机基础--8086处理器
    01、计算机基础--计算机概述
    06、JavaEE--SpringMVC
    04、Android系统--Android10
    03、Android系统--Android4.4
    App自动化测试工具Uiautomator2
    Python yaml文件读写
  • 原文地址:https://www.cnblogs.com/tangzhengyue/p/2152397.html
Copyright © 2011-2022 走看看