class Myclass { public int MyInt; }
可以使用readonly關鍵字,表示這個字段只能在執行構造函數的過程中賦值,或者由初始化語句賦值。
靜態成員通過定義它的類來進行訪問(MyClass.MyInt)
class Myclass { public int MyInt; public string GetString() { return "Here is a string!"; } }
與override一樣,也可以使用sealed指定在派生類中不能對這個方法作進一步的修改,。
使用extern可以在項目外部提供方法的實現代碼。
get和set區別:get是只讀,set是只寫。然后get塊一定要有一個返回值,下面是示例。
PRivate int myInt;
public int MyIntProp { get { return myInt; } set { } }
這樣的話,由于myInt這個字段是私有的,外部成員時不能訪問的,但是通過這個get和set就可以在外部修改了,但是前提是屬性是共有的。
set是一個賦值的功能,但是set可以通過一系列操作來達到不同途徑來設置方法。而且還可以這里加上出錯的警告之類的。
然后就是get和set一樣也可以在前面加上一系列的限定關鍵字。例如
protected set { myInt = value;}
創建一個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)當從基類中繼承一個非抽象的成員的時候,也就繼承了其實現代碼。
如果繼承的成員時虛擬的,就可以用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)還有就是可以使用隱藏基類的方法實現復寫所能實現的功能。
(1)重要性:
a、要對派生類的用戶隱藏繼承的公共成員,但仍能在類中訪問其功能。
b、要給繼承的虛擬成員添加實現代碼,而不是地重寫的新執行代碼替換它。
(2)base關鍵字
使用base關鍵字可以使用基類中的隱藏的相應的方法。
base.DoSomething();
(3)this關鍵字
a、可以在類內部使用,引用對象的實例。
b、把當前對象實例的引用傳遞給一個方法。
c、限定本地類型的成員。
return this.someData
class MyClass { public class myNestClass { public int nestedFlassField; } }
如果內部定義的類是共有的,那么就可以在外部使用,但是要加上限定名稱,例如
MyClass.myNestedClass myobj = new MyClass.myNestedClass();
如果嵌套的類聲明為私有,或者聲明為其他與執行該實例化的代碼不兼容的訪問級別就不能在外部訪問。
interface IMyInterface { }
接口成員的定義與類相似,但是有一些重要的區別:
但是如果要隱藏繼承了基接口的成員,可以用關鍵字new來定義他們。
在接口中定義的屬性可以定義訪問塊get和set中的哪一個能夠用于該屬性~~~~~~~~~~~~
interface IMyInterface { int MyInt(get;set;) }
實現接口的的類必須包含該接口所有成員的實現代碼,且必須匹配指定的簽名(包括匹配指定的get和set快),并且必須是公共的。
interface IMyInterface { void DoSomething(); void DoSomethingElse(); } public class Myclass:IMyInterface { public void DoSomething() { } public void DoSomethingElse() { } }
可以使用virtual 或者abstract來實現接口成員。還可以在基類上實現接口。
interface IMyInterface { void DoSomething(); void DoSomethingElse(); } public class Myclass:IMyInterface { public void DoSomething() { } } public class MyDerivedClass:Myclass,IMyInterface { public void DoSomethingElse() { } }
繼承一個實現給定的基類,就意味著派生類隱式地支持這個接口。
當然如果基類中的方法定義為虛擬,那么派生類就可以替代這個方法,而不是隱藏它們。
(1)顯示實現接口成員。
如果顯示地實現接口成員,那么這個成員就是能通過接口來訪問,不能通過該類來訪問。
通過類訪問:
Myclass myobj = new Myclass();myobj.DoSomething();
通過接口訪問:
Myclass myobj = new Myclass();IMyinterface myInt = myobj;myInt.DoSomething();
(2)用非公共的可訪問性添加屬性存取器<
新聞熱點
疑難解答