zoukankan      html  css  js  c++  java
  • 泛型接口

    泛型接口指定实参

        public sealed class Point
        {
            public Int32 x;
            public Int32 y;
    
            public override string ToString()
            {
                return "x: " + x + " y: " + y;
            }
        }
    
        public class Triangle : IEnumerable<Point>
        {
            private Point[] m_triangle;
            public Triangle(Point[] pArray)
            {
                m_triangle = new Point[pArray.Length];
    
                for (Int32 i = 0; i < pArray.Length; i++)
                {
                    m_triangle[i] = pArray[i];
                }
            }
    
            public IEnumerator<Point> GetEnumerator()
            {
                return new TriangleEnum(m_triangle);
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
    
        public sealed class TriangleEnum : IEnumerator<Point>
        {
            private Point[] m_vertices;
            int position = -1;
    
            public TriangleEnum(Point[] list)
            {
                m_vertices = list;
            }
    
            public Boolean MoveNext()
            {
                position++;
                return position < m_vertices.Length;
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            public Point Current
            {
                get
                {
                    try
                    {
                        return m_vertices[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
    
            Object IEnumerator.Current
            {
                get
                {
                    return Current;
                }
            }
    
            public void Dispose()
            { }
        }

    泛型接口未指定实参

        internal sealed class ArrayEnumerable<T> : IEnumerable<T>
        {
            private T[] m_array;
            public ArrayEnumerable(T[] list)
            {
                m_array = new T[list.Length];
    
                for (Int32 i = 0; i < list.Length; i++)
                {
                    m_array[i] = list[i];
                }
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                return new ArrayEnumerator<T>(m_array);
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }
    
        internal sealed class ArrayEnumerator<T> : IEnumerator<T>
        {
            private T[] m_array;
            private Int32 m_position = -1;
    
            public ArrayEnumerator(T[] list)
            {
                m_array = list;
            }
    
            public Boolean MoveNext()
            {
                m_position++;
                return m_position < m_array.Length;
            }
    
            public void Reset()
            {
                m_position = -1;
            }
    
            public T Current
            {
                get
                {
                    try
                    { 
                        return m_array[m_position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
    
            Object IEnumerator.Current
            {
                get
                {
                    return Current;
                }
            }
    
            public void Dispose()
            { }
        }

    测试代码

        class GenericInterfaceNoAssign
        {
            public static void Go()
            {
                Point[] points = new Point[7]
                {
                    new Point(){x=110, y=220},
                    new Point(){x=111, y=221},
                    new Point(){x=112, y=222},
                    new Point(){x=113, y=223},
                    new Point(){x=114, y=224},
                    new Point(){x=115, y=225},
                    new Point(){x=116, y=226}
                };
    
                ArrayEnumerable<Point> arrayEn = new ArrayEnumerable<Point>(points);
    
                foreach (Point p in arrayEn)
                {
                    Console.WriteLine(p.ToString());
                }
            }
        }
    
        class GenericInterface
        {
            public static void Go()
            {
                Point[] points = new Point[3]
                {
                    new Point(){x=10, y=20},
                    new Point(){x=11, y=21},
                    new Point(){x=12, y=22}
                };
    
                Triangle triangle = new Triangle(points);
    
                foreach (Point p in points)
                {
                    Console.WriteLine(p.ToString());
                }
            }
        }
  • 相关阅读:
    分分钟提升命令行模式下密码输入逼格
    MySQL server has gone away 的两个最常见的可能性
    第一次遇到刷新缓冲区延时
    Mac上安装mysqlclient的报错
    python3 --- locale命名空间让程序更加安全了
    doctest --- 一个改善python代码质量的工具
    MySQL优化器 --- index_merge
    机智的MySQL优化器 --- is null
    Centos-7.x 下子网掩码的配置
    JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
  • 原文地址:https://www.cnblogs.com/hellolong/p/3829608.html
Copyright © 2011-2022 走看看