C#學習日記23---多態性 之 運算子重載、方法重載、抽象類別、虛擬方法

黄舟
發布: 2017-01-21 15:26:39
原創
1644 人瀏覽過

     在C#中多態性的定義是:同一操作作用於不同類別的實例,不同的類別進行不同的解釋,最後產生不同的執行結果。換句話說也就是 一個接口,多個功能。

C# 支援2種形式的多態性: 編譯時的多態性、運行時的多態性

編譯時的多態性:

   編譯時的多態性是透過重載來實現的

   編譯時的多態性

方法重載

   您可以在同一個範圍內對相同的函數名稱有多個定義。函數的定義必須彼此不同,可以是參數清單中的參數類型不同,也可以是參數個數不同。不能重載只有返回類型不同的函數聲明。寫個例子

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{  
    class exchange  //定义一个exchange类  
    {//方法实现交换两参数数据  
        public void swap(int a, int b)  
        {  
            int temp;  
            temp = a;  
            a = b;  
            b = temp;  
            Console.WriteLine("{0},{1}",a,b);  
        }  
        public void swap(string a, string b)  
        {  
            string temp;  
            temp = a;  
            a = b;  
            b = temp;  
            Console.WriteLine("{0},{1}", a, b);  
        }  
  
    }  
    class program  
    {  
        static void Main(string[] args)  
        {  
            exchange exch = new exchange();  
            exch.swap(10, 20);  //调用 swap(int a,int b)方法  
            exch.swap("大", "小");  //调用 swap(string a,string b)方法  
          
        }  
    }  
}
登入後複製

結果:

C#學習日記23---多態性 之 運算子重載、方法重載、抽象類別、虛擬方法

操作符重載

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{  
    class student  //定义student类  
    {  
        private int Chinese;  
        private int Math;  
        public void value(int a, int b)  //定义一个赋值的方法,以后学了构造方法就不这么麻烦了  
        {  
            Chinese = a;  
            Math = b;  
        }  
        public static student operator + (student a, student b) //运算符重载,实现相加功能  
        {  
            student stu = new student();  
            stu.Chinese = a.Chinese + b.Chinese;  
            stu.Math = a.Math + b.Math;  
            return stu;  
        }  
        public int getChinese()  //获取Chinese 的方法  
        {  
            return Chinese;  
        }  
        public int getMath()  //获取Math 的方法  
        {  
            return Math;  
        }  
    }  
    class program  
    {  
        static void Main(string[] args)  
        {  
            student a = new student();  
            student b = new student();  
            a.value(70,80);  
            b.value(40, 50);  
            student stu = a + b;  //70+40, 80+50  
            Console.WriteLine("a+b Chinese = {0}\na+b Math = {1}", stu.getChinese(), stu.getMath());  
              
        }  
    }  
}
登入後複製

結果:

C#學習日記23---多態性 之 運算子重載、方法重載、抽象類別、虛擬方法

🀎?決定實現何種操作,C#中運行時的多態透過抽象類別或虛方法來實現的。

抽象類別和抽象方法

   C# 允許您使用關鍵字 abstract 建立抽象類別或抽象方法,當衍生類別繼承自該抽象類別時,實作即完成。抽象類別包含抽象方法,抽象方法可被衍生類別實作。派生類別具有更專業的功能。抽象類別不能夠被實例化,

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{//创建抽象类和抽象方法  
   abstract class score  
    {  
       public abstract int Add();  
    }  
    //创建子类  
   class student : score  
   {  
       private int Chinese = 80;  
       private int Math = 90;  
       public override int Add()  //关键字 override 实例方法  
       {  
          int sum=Chinese+Math;  
           return sum;  
       }  
         
   }  
    class program  
    {  
        static void Main(string[] args)  
        {  
            student stu = new student();  
                
            Console.WriteLine(stu.Add() );  //结果 170  
             
        }  
    }  
}
登入後複製

虛方法

  虛方法是使用關鍵字 virtual 聲明的。虛方法可以在不同的繼承類別中有不同的實作。對虛方法的呼叫是在運行時發生的。

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test  
{  
   class score  
    {  
       protected int Chinese = 80;  
       protected int Math = 90;  
       public virtual int Add()  //定义一个虚方法  
       {  
           int sum = Chinese + Math;  
           return sum;  
       }  
    }  
   //定义子类,实现方法  
   class student : score  
   {  
       public override int Add()  //关键字 override 实例方法,实现相减操作  
       {  
           int sub = Math - Chinese ;  
           return sub;  
       }  
         
   }  
    class program  
    {  
        static void Main(string[] args)  
        {  
            student stu = new student();  
                
            Console.WriteLine(stu.Add() );  //结果 10  
             
        }  
    }  
}
登入後複製
我們可以看出運行時真正呼叫的方法並不是虛方法,而是override 實例後的方法

以上就是 C#學習日記23---多態性之運算子重載、方法重載、抽象類別、虛方法的內容,更多相關內容請關注PHP中文網(www.php.cn)!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!