文章

C#之interface(接口)

using System;

namespace Interface2
{
    class Program
    {
        static void Main(string[] args)
        {
            var user = new PhoneUser(new Nokia());
            user.UserPhone();
        }
    }
    class PhoneUser
    {
        private IPhone phone;
        public PhoneUser(IPhone phone)
        {
            this.phone = phone;
        }
        public void UserPhone()
        {
            phone.Dail();
            phone.PickUp();
            phone.Send();
            phone.Receive();

        }
    }
    interface IPhone
    {
        void Dail();
        void PickUp();
        void Send();
        void Receive();
    }
    class Nokia : IPhone
    {
        public void Dail()
        {
            Console.WriteLine("Nokia is Calling!");
        }

        public void PickUp()
        {
            Console.WriteLine("Hi,is Nokia!");
        }

        public void Receive()
        {
            Console.WriteLine("Nokia is Receive!");
        }

        public void Send()
        {
            Console.WriteLine("**** -From Nokia...");
        }
    }
    class Motorola : IPhone
    {
        public void Dail()
        {
            Console.WriteLine("Motorola is Calling!");
        }

        public void PickUp()
        {
            Console.WriteLine("Hi,is Motorola!");
        }

        public void Receive()
        {
            Console.WriteLine("Motorola is Receive!");
        }

        public void Send()
        {
            Console.WriteLine("**** -From Motorola...");
        }
    }
}

原文来自:C#之interface(接口),尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论