zoukankan      html  css  js  c++  java
  • C# 索引 就是使对象可以像数组一样访问,并可以做出特殊的处理

        public class Point
        {
            private double x, y;
            public Point(double X, double Y)
            {
                x = X;
                y = Y;
            }
           
            public override string ToString()
            {
                return String.Format("X: {0} , Y: {1}", x, y);
            }
        }
        public class Points
        {
            Point[] points;
            public Points(Point[] Points)
            {
                points = Points;
            }
            public int PointNumber
            {
                get
                {
                    return points.Length;
                }
            }
            
            public Point this[int Index]
            {
                get
                {
                    return points[Index];
                }
            }
        }
    
        class IndexClass
        {
            public string this[int index]
            {
                get
                {
                    switch (index)
                    {
                        case 1:
                            return "";
                        case 2:
                            return "";
                        case 3:
                            return "";
                        case 4:
                            return "";
                        default:
                            return "unkonw number";
                    }
                }
            }
    
            public string this[string str]
            {
                get
                {
                    return str;
                }
            }
    
            public object this[object o]
            {
                get
                {
                    return new object();//这个地方有点意思
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                IndexClass ic = new IndexClass();
                Console.WriteLine(ic["字符串索引"]);
    
                Console.WriteLine(ic[4]);
    
                Console.WriteLine(ic[new object()].GetType());
    
                Point[] tmpPoints = new Point[10];
                for (int i = 0; i < tmpPoints.Length; i++)
                {
                    tmpPoints[i] = new Point(i, Math.Sin(i));
                }
    
                Points tmpObj = new Points(tmpPoints);
                for (int i = 0; i < tmpObj.PointNumber; i++)
                {
                    Console.WriteLine(tmpObj[i]);
                }
    
                Console.ReadKey();
            }
        }
  • 相关阅读:
    冒泡排序
    三种for循环遍历
    打印一年中的月历
    基于主主复制的mysql双机热备+keepalived实现高可用性
    docker实现apache+php容器和mysql容器独立运行
    XML和JSON
    PHP表单
    【翻译-Docker】Post-installation steps for Linux
    【翻译】docker install
    小计划
  • 原文地址:https://www.cnblogs.com/potoofly/p/2964012.html
Copyright © 2011-2022 走看看