国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C#學習筆記(八)——定義類的成員

2019-11-17 02:46:20
字體:
來源:轉載
供稿:網(wǎng)友

C#學習筆記(八)——定義類的成員

一、成員的定義

image

1、定義字段

class Myclass    {        public int MyInt;    }

可以使用readonly關鍵字,表示這個字段只能在執(zhí)行構造函數(shù)的過程中賦值,或者由初始化語句賦值。

靜態(tài)成員通過定義它的類來進行訪問(MyClass.MyInt)

2、定義方法

class Myclass    {        public int MyInt;        public string GetString()        {            return "Here is a string!";        }    }

image

與override一樣,也可以使用sealed指定在派生類中不能對這個方法作進一步的修改,。

使用extern可以在項目外部提供方法的實現(xiàn)代碼。

3、定義屬性

get和set區(qū)別:get是只讀,set是只寫。然后get塊一定要有一個返回值,下面是示例。

PRivate int myInt;
public int MyIntProp        {            get            {                return myInt;            }            set            {            }        }

這樣的話,由于myInt這個字段是私有的,外部成員時不能訪問的,但是通過這個get和set就可以在外部修改了,但是前提是屬性是共有的。

set是一個賦值的功能,但是set可以通過一系列操作來達到不同途徑來設置方法。而且還可以這里加上出錯的警告之類的。

然后就是get和set一樣也可以在前面加上一系列的限定關鍵字。例如

protected set {     myInt = value;}

4、一個demo

創(chuàng)建一個MyClass.cs

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Exercise{    class MyClass    {        public readonly string Name;        private int intVal;        public int Val        {            get            {                return intVal;            }            set            {                if(value>=0&&value<=10)                {                    intVal = value;                }                else                {                    throw (new ArgumentOutOfRangeException("Val", value, "Val must be assigned a value between 0 and 10."));                }            }        }        public override string ToString()        {            return "Name:" + Name + "/nVal:" + Val;        }        public MyClass(string newName)        {            Name = newName;            intVal=0;        }    }}

在Main.cs中添加

#region Using directivesusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;#endregionnamespace Exercise{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Creating object myObj...");            MyClass myobj = new MyClass("My Object");            Console.WriteLine("myObj created.");            for(int i=-1;i<=0;i++)            {                try                {                    Console.WriteLine("/nAttemp to assign {0} to myObj.val...", i);                    myobj.Val = i;                    Console.WriteLine("Value {0} assigned to myObj.Val.", myobj.Val);                }                catch(Exception e)                {                    Console.WriteLine("Exception {0} thrown.", e.GetType().FullName);                    Console.WriteLine("Message:/n/"{0}/"", e.Message);                }            }            Console.WriteLine("/nOutputting myobj.Tostring()...");            Console.WriteLine(myobj.ToString());            Console.WriteLine("myobj.ToString() Output.");            Console.ReadKey();                    }    }}

二、類的高級議題

1、隱藏基類方法

(1)當從基類中繼承一個非抽象的成員的時候,也就繼承了其實現(xiàn)代碼。

如果繼承的成員時虛擬的,就可以用override重寫這段代碼。

無論繼承的成員是不是虛擬的,都可以隱藏基類的代碼。

public class MyBaseClass    {        public void DoSometing()        {            //Base implementation        }    }    public class MyDeriveClass:MyBaseClass    {        public void DoSometing()        {            //Derived class implementation, hides base implementation        }    }

盡管這段代碼正常運行,但是還是會有一個waring,可以提醒我們是否有意隱藏這個成員。如果確實要隱藏這個成員,我們可以使用關鍵字new顯式地表明意圖。

public class MyDeriveClass:MyBaseClass    {        new public void DoSometing()        {            //Derived class implementation, hides base implementation        }    }

(2)如果重寫基類中的方法,派生類中的基類方法會被取代,即使是通過基類類型進行的,情況也是相同的。

public class MyBaseClass    {        public virtual void DoSometing()        {            Console.WriteLine("FUCK");            //Base implementation        }    }    public class MyDeriveClass:MyBaseClass    {        public override void DoSometing()        {            Console.WriteLine("FUCK you!");            //Derived class implementation, hides base implementation        }    }
MyDeriveClass myObj = new MyDerivedClass();MyBaseClass myBaSEObj;myBaseObj = myObj;myBaseObj.DoSomething();

明顯,運算結果是FUCK you!

(3)還有就是可以使用隱藏基類的方法實現(xiàn)復寫所能實現(xiàn)的功能。

2、調用重寫或隱藏的基類方法

(1)重要性:

a、要對派生類的用戶隱藏繼承的公共成員,但仍能在類中訪問其功能。

b、要給繼承的虛擬成員添加實現(xiàn)代碼,而不是地重寫的新執(zhí)行代碼替換它。

(2)base關鍵字

使用base關鍵字可以使用基類中的隱藏的相應的方法。

base.DoSomething();

(3)this關鍵字

a、可以在類內(nèi)部使用,引用對象的實例。

b、把當前對象實例的引用傳遞給一個方法。

c、限定本地類型的成員。

return this.someData

3、嵌套的類型定義

class MyClass    {        public class myNestClass        {            public int nestedFlassField;        }    }

如果內(nèi)部定義的類是共有的,那么就可以在外部使用,但是要加上限定名稱,例如

MyClass.myNestedClass myobj = new MyClass.myNestedClass();

如果嵌套的類聲明為私有,或者聲明為其他與執(zhí)行該實例化的代碼不兼容的訪問級別就不能在外部訪問。

三、接口的實現(xiàn)

1、定義方法

interface IMyInterface    {    }

接口成員的定義與類相似,但是有一些重要的區(qū)別:

image

但是如果要隱藏繼承了基接口的成員,可以用關鍵字new來定義他們。

在接口中定義的屬性可以定義訪問塊get和set中的哪一個能夠用于該屬性~~~~~~~~~~~~

interface IMyInterface    {        int MyInt(get;set;)    }

2、在類中實現(xiàn)接口

實現(xiàn)接口的的類必須包含該接口所有成員的實現(xiàn)代碼,且必須匹配指定的簽名(包括匹配指定的get和set快),并且必須是公共的。

interface IMyInterface    {        void DoSomething();        void DoSomethingElse();    }    public class Myclass:IMyInterface    {        public void DoSomething()        {        }        public void DoSomethingElse()        {        }    }

可以使用virtual 或者abstract來實現(xiàn)接口成員。還可以在基類上實現(xiàn)接口。

interface IMyInterface    {        void DoSomething();        void DoSomethingElse();    }    public class Myclass:IMyInterface    {        public void DoSomething()        {        }    }    public class MyDerivedClass:Myclass,IMyInterface    {        public void DoSomethingElse()        {        }    }

繼承一個實現(xiàn)給定的基類,就意味著派生類隱式地支持這個接口。

當然如果基類中的方法定義為虛擬,那么派生類就可以替代這個方法,而不是隱藏它們。

(1)顯示實現(xiàn)接口成員。

如果顯示地實現(xiàn)接口成員,那么這個成員就是能通過接口來訪問,不能通過該類來訪問。

通過類訪問:

Myclass myobj = new Myclass();myobj.DoSomething();

通過接口訪問:

Myclass myobj = new Myclass();IMyinterface myInt = myobj;myInt.DoSomething();

(2)用非公共的可訪問性添加屬性存取器<

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 龙口市| 新巴尔虎右旗| 闻喜县| 深州市| 灯塔市| 探索| 秦安县| 黄陵县| 铜陵市| 平江县| 得荣县| 宝鸡市| 台东县| 桓台县| 海阳市| 旅游| 马尔康县| 会宁县| 唐山市| 兴仁县| 蒲江县| 老河口市| 焉耆| 全州县| 松江区| 宜君县| 隆化县| 江山市| 云阳县| 灵武市| 宁河县| 崇州市| 长岛县| 巴彦淖尔市| 洪雅县| 重庆市| 上饶市| 马公市| 怀集县| 长丰县| 措美县|