隨筆 - 78  文章 - 25  trackbacks - 0
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114363
          • 排名 - 515

          最新評論

          閱讀排行榜

          評論排行榜

          ■接口:使用關鍵字interface,只是定義一系列的行為類型,不提供任何實現
          interface IMovable{
              float speed{get;}
              float maxSpeed{get;set;}
              void Run();
              void Walk();
              void Fly();
          }
          ■接口的實現,通過冒號來實現

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;

          namespace useInterface
          {
              
          interface IMovable {
                  
          float Speed{get;}
                  
          float MaxSpeed { get; set; }
                  
          void Run();
                  
          void Walk();
                  
          void Fly();
              }
              
          class Dog : IMovable {
                  
          public float Speed {
                      get {
                          
          return 10.5f;
                      }
                  }
                  
          private float _MaxSpeed;
                  
          public float MaxSpeed{
                      get { 
          return this._MaxSpeed; }
                      set { 
          this._MaxSpeed = value; }
                  }
                  
          public void Run() {
                      System.Console.WriteLine(
          "Dog is running");
                  }
                  
          public void Walk()
                  {
                      System.Console.WriteLine(
          "Dog is walking");
                  }
                  
          public void Fly()
                  {
                      System.Console.WriteLine(
          "Dog can't fly");
                  }

              }
              
          class Person : IMovable
              {
                  
          public float Speed
                  {
                      get
                      {
                          
          return 20.5f;
                      }
                  }
                  
          private float _MaxSpeed;
                  
          public float MaxSpeed
                  {
                      get { 
          return this._MaxSpeed; }
                      set { 
          this._MaxSpeed = value; }
                  }
                  
          public void Run()
                  {
                      System.Console.WriteLine(
          "Person is running");
                  }
                  
          public void Walk()
                  {
                      System.Console.WriteLine(
          "Person is walking");
                  }
                  
          public void Fly()
                  {
                      System.Console.WriteLine(
          "Person flies with a plane");
                  }

              }
              
          class Program
              {
                  
          static void SthMove(IMovable mov) {
                      string type 
          = mov.GetType().Name;
                      System.Console.WriteLine(
          "This is a {0} ",type);
                      System.Console.WriteLine(
          "{0}.Speed={1}",type,mov.Speed);
                      System.Console.WriteLine(
          "{0}.MaxSpeed={1}", type, mov.MaxSpeed);
                      mov.Walk();
                      mov.Run();
                      mov.Fly();
                  }
                  
          static void Main(string[] args)
                  {
                      Person aPerson 
          = new Person();
                      aPerson.MaxSpeed 
          = 50;
                      SthMove(aPerson);
                      IMovable mov 
          = new Dog();
                      mov.MaxSpeed 
          = 20;
                      SthMove(mov);
                      System.Console.ReadLine();
                  }
              }
          }

          結果:
          This is a Person
          Person.Speed=20.5
          Person.MaxSpeed=50
          Person is walking
          Person is running
          Person flies with a plane
          This is a Dog
          Dog.Speed=10.5
          Dog.MaxSpeed=20
          Dog is walking
          Dog is running
          Dog can't fly



          一個類可以實現多個接口。如有兩個接口IMovable,ISpeakable,定義一個Person類來實現這兩個接口
          class Person:IMovable,ISpeakable
          posted on 2009-10-26 18:21 期待明天 閱讀(263) 評論(0)  編輯  收藏 所屬分類: CSharp
          主站蜘蛛池模板: 浮山县| 荣昌县| 积石山| 盘山县| 当雄县| 大同市| 志丹县| 延庆县| 吉安县| 万源市| 三台县| 永善县| 英吉沙县| 镇宁| 灌南县| 会昌县| 永吉县| 大同市| 祁连县| 闸北区| 长阳| 革吉县| 随州市| 锦屏县| 大足县| 铜鼓县| 开原市| 巴南区| 湖北省| 拜城县| 呈贡县| 石台县| 西林县| 饶阳县| 府谷县| 广河县| 青田县| 临安市| 南靖县| 兴宁市| 西贡区|