在C#中實現OOP概念

來源:互聯網
上載者:User

1.C#中的對象和類

  類是對一組具有相同屬性和行為的對象的描述

  編碼慣例:

    給公用成員變數、受保護的成員變數或內部成員變數命名應使用帕斯卡命名法,如Score、Name和Staus

    給私人成員變數命名應使用駱駝命名法,並以底線開頭,如_age、_length 和_radius

class Employee{  private string _name;  private char _gender;  private string _qualification;  private uint _salary;}

2.存取修飾詞

   public 可被所屬類的成員以及不屬於類的成員訪問

   internal 可被當前程式集訪問

   protected 可被所屬類或派生自所屬類的類型訪問

   private 僅所屬類的成員才可以訪問

   如果對類不指定存取修飾詞,則類的預設存取修飾詞為internal ,但類成員的預設存取修飾詞為private

3.建構函式和解構函式

  C#提供了一個名為建構函式的結構來自動初始化成員變數,但建構函式是類中的一種特殊方法,每次建立類的執行個體都會調用此方法。建構函式與類同名,它不返回任何值。

using System;namespace BaseConsole{    class Employee    {        private string _name;        private char _gender;        private string _qualification;        private uint _salary=0;        //預設建構函式        private Employee()        {            _qualification = "大學畢業生";        }        //參數化建構函式        private Employee(string strQualification,string strName,char gender,uint empSalary)        {            _qualification = strQualification;            _name = strName;            _gender = gender;            _salary = empSalary;        }        [STAThread]        static void Main(string[] args)        {            Employee objEmployee = new Employee();            //調用參數化建構函式            Employee objMBA = new Employee("工商管理學碩士","tom",'M',4000);            Console.WriteLine("資格="+objEmployee._qualification);            Console.WriteLine("薪水="+objEmployee._salary);            Console.WriteLine("資格="+objMBA._qualification);            Console.ReadKey();        }    }}

解構函式

~ Employee(){  //實現解構函式}

 解構函式不接受任何參數,也不帶任何存取修飾詞。解構函式的主體包括了一些代碼,通常用於關閉由執行個體開啟的資料庫、檔案或網路連接等
  一個類只能有一個解構函式

  解構函式不能重載

  解構函式不能顯式或手動調用,只能由記憶體回收行程自動調用

4.方法重載

  多個方法共用一個名稱但對不同資料執行相似的功能,這種概念稱為方法重載

using System;namespace BaseConsole{    class OverloadParameters    {        int greatest(int num1,int num2)        {            Console.WriteLine("{0}和{1}中較大的數字是:",num1,num2);            if(num1>num2)            {                return num1;            }else            {                return num2;            }        }        int greatest(int num1,int num2,int num3)        {            Console.WriteLine("{0}、{1}和{2}中最大的數字是:",num1,num2,num3);            if (num1 > num2 && num1 > num3)            {                return num1;            }            else if (num2 > num1 && num2 > num3)            {                return num2;            }            else            {                return num3;            }                    }        [STAThread]        static void Main(string[] args)        {            OverloadParameters obj = new OverloadParameters();            Console.WriteLine(obj.greatest(22,88));            Console.WriteLine(obj.greatest(300,200,100));            Console.ReadKey();        }    }}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.