精美絕倫的樣本程式。寫文章的人應該向人家學習。

來源:互聯網
上載者:User
using System;

/*一個非常精緻的類。
 * 這是我所見過的最好的一段程式樣本。
 * 非常短小,但是卻幾乎包含了一個類所能擁有的一切。
 * 包括:委託、事件機制、介面、介面繼承,當然還有最不起眼的屬性和方法。
 * 設計這個類的作者可謂獨具匠心,令人歎服。這也就是作者不俗之處。*/

namespace ObjectType
{
    public delegate void ADelegate();
    public interface IChanged
    {
        event ADelegate AnEvent;
    }
    public interface IPoint
    {
        int X {get; set;}
        int Y {get; set;}
    }

    class Point:IPoint, IChanged
    {
        #region IPoint 成員

        private int x;
        public int X
        {
            get
            {
                // TODO:  添加 Point.X getter 實現
                return this.x;
            }
            set
            {
                // TODO:  添加 Point.X setter 實現
                this.x = value;
                AnEvent();
            }
        }

        private int y;
        public int Y
        {
            get
            {
                // TODO:  添加 Point.Y getter 實現
                return this.y;
            }
            set
            {
                // TODO:  添加 Point.Y setter 實現
                this.y = value;
                AnEvent();
            }
        }

        #endregion

        #region IChange 成員

        public event ObjectType.ADelegate AnEvent;

        #endregion
    }

    class EntryPoint
    {
        static void CallMe()
        {
            Console.WriteLine("I got called!");
        }

        [STAThread]
        static void Main(string[] args)
        {
            Point p = new Point();
            IChanged ic = p;
            IPoint ip = p;
            ic.AnEvent += new ADelegate(CallMe);
            ip.X = 42;
            ip.Y = 42;
            Console.WriteLine("X: {0} Y: {0}", p.X, p.Y);
        }
    }
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.