定義類的修飾符:
關于類的修飾符需要注意的是:
public class MyClass : MyBase {} // MyBase is a internal class
,不過internal可以繼承自public類public class MyClass : MyBase, IMyInterface {}
定義接口的修飾符:
需要注意的是:
當類沒有繼承對象時,默認繼承System.Object,那么就有必要看看該類中到底有什么。
Method | Description |
---|---|
Object() | 構造函數 |
~Object() | 析構函數 |
virtual bool Equals(object) | this和other對象進行比較 |
static bool Equals(object,object) | 比較輸入的兩個對象,內部調用上一個函數,true all null |
static bool ReferenceEquals(object,object) | 比較輸入的兩個對象是不是同一個對象的引用 |
virtual string ToString() | 默認返回類類型的名字 |
object MemberwiseClone() | 構建新的object實例,并對成員變量進行copy,引用類型的成員變量仍然引用同一個變量 |
System.Type GetType() | 返回對象的類型 |
virtual int GetHashCode() | 返回對象的hash值 |
使用GetType的一個示例:
if (myObj.GetType() == typeof(MyComplexClass)){ // myObj is an instance of the class MyComplexClass}
修飾關鍵字:
只針對成員變量的修飾符:
只針對成員函數的修飾符:
private int myInt;public int MyIntProp{ get { return myInt; } set { if (value >= 0 && value <= 10) myInt = value; else throw(...); }}
很容易想到的實現方式如下:
public class MyBaseClass{ public void DoSomething() { // base implementation }}public class MyDerivedClass : MyBaseClass{ public void DoSomething() { // Derived class implementation, hides base implementation }}
但是上面的代碼在編譯的過程中會出現警告,說MyDerivedClass中的DoSomething函數隱藏了基類中的DoSomething函數,請使用new關鍵字,即public new void DoSomething() {}
。
可以使用現有的System.Collections.ArrayList實現。這里要講的是自己定義Collections。可以采用如下的方式來實現,如:
public class Animals : CollectionBase{ // Add功能 public void Add(Animal newAnimal) { List.Add(newAnimal); } // Remove功能 public void Remove(Animal oldAnimal) { List.Remove(oldAnimal); } // 實現MyAnimal[0]的訪問方式 public Animal this[int animalIndex] { get { return (Animal)List[animalIndex]; } set { List[animalIndex] = value; } } public Animals() { }}
重載操作符的成員函數是公有靜態(tài)的。需要注意的是在進行變量轉換的成員函數的定義過程中,有兩個額外的關鍵字explicit和implicit。
// 二元操作符+public class Class1{ public int val; // 二元操作符 public static Class1 Operator +(Class1 op1, Class1 op2) { Class1 returnVal = new Class1(); returnVal.val = op1.val + op2.val; return returnVal; } // 一元操作符,- public static Class1 operator -(Class1 op1) { Class1 returnVal = new Class1(); returnVal.val = -op1.val; return returnVal; }}
is操作符:檢測一個未知的變量能夠轉化成為已知的類型的變量。如果可以則返回true,否則返回false。但是它并不能判斷兩個類型是否相同。
它的語法結構為:<operand> is <type>
該表達式的可能的結果如下:
<operand>
也是那個類型,或是那個類的子類,或是可以轉換成那個類型時,返回的結果為trueas操作符,將變量轉換成特殊的引用類型。<operand> as <type>
。
適用于以下環(huán)境,
如果不能夠轉換,則返回的結果為null。
BaseClass obj1 = new BaseClass();DerivedClass obj2 = obj1 as DerivedClass; // return null 不會拋出異常,如果使用 ... = (DerivedClass)obj1; 則會拋出異常// 如果以下面的方式進行轉換則是可以的DerivedClass derivedClass = new DerivedClass();BaseClass baseClass = derivedClass;DerivedClass newDerivedClass = baseClass as DerivedClass; // not null
直接采用System.Object對象內的MemberwiseClone()當遇到類含有引用變量的時候,就不能夠實現深層的拷貝,如:
public class Content{ public int Val;}public class Cloner{ public Content MyContent = new Content(); public Cloner(int newVal) { MyContent.Val = newVal; } public object GetCopy() { return MemberwiseClone(); }}// 具體調用Cloner mySource = new Cloner(5);Cloner myTarget = (Cloner)mySource.GetCopy();mySource.MyContent.Val = 2; // 這將導致myTarget.MyTarget.Val也相應的改變成了2
要實現深度的拷貝,可以實現ICloneable接口。
public class Cloner : ICloneable{ public Content MyContent = new Content(); public Cloner(int newVal) { MyContent.Val = newVal; } public object Clone() { Cloner clonedCloner = new Cloner(MyContent.Val); return clonedCloner; // 或是 // Cloner clonedCloner = new Cloner(); // clonedCloner.MyContent = MyContent.Clone(); // return clonedCloner; }}
自定義的過程中,只要繼承Expection
類并進行實現即可。如:
public class AgeBelowZeroException : Exception{ public AgeBelowZeroException() : base("The age must >= 0") { }}
新聞熱點
疑難解答