繼承、Overriding重寫、動態綁定、Sealed密封類、Object類、重載==和!= ...
C#複習⑤
2016年6月19日
22:39
Main Inheritance 繼承
1.繼承的語法結構
class A { // base class int a; public A() {...} public void F() {...} } class B : A { // subclass (inherits from A, extends A) int b; public B() {...} public void G() {...} }
C#中類的繼承只能是單繼承,在Java中也只支持單繼承,C++中支持多繼承。但是C#、Java、C++均可以實現多個介面。
Single inheritance: a class can only inherit from one base class, but it can implement multiple interfaces.
某個類只能繼承一個父類不能繼承自結構體。
A class can only inherit from a class, not from a struct.
結構體不能被繼承,但是可以實現多個介面。
Structs cannot inherit from another type, but they can implement multiple interfaces.
C#中所有的類的基類為Object類
A class without explicit base class inherits from Object.
2.Assignments and Type Checks分配和類型檢查
class A {...} class B : A {...} class C: B {...}
3.Overriding Methods重寫方法
只有在父類中聲明為Virtual的方法才可以在子類中重寫
Only methods that are declared as virtual can be overridden in subclasses
方法簽名必須相同;
Method signatures must be identical
same number and types of parameters (including function type!)
same visibility (public, protected, ...).
屬性和索引器同樣可以被重寫(對應關鍵字virtual 和 override);
Properties and indexers can also be overridden (virtual, override).
靜態方法不可以被重寫。
Static methods cannot be overridden.
4.Dynamic Binding 動態綁定
動態綁定的好處:可以使用下麵的方法針對不同類構造出的實例對象均有效。
class A { public virtual void WhoAreYou() { Console.WriteLine("I am an A"); } } class B : A { public override void WhoAreYou() { Console.WriteLine("I am a B"); } } 調用舉例: A a = new B(); a.WhoAreYou(); // "I am a B" 動態綁定舉例: void Use (A x) { x.WhoAreYou(); } Use(new A()); // "I am an A" Use(new B()); // "I am a B"
5.Hiding覆蓋
在子類中成員函數可以被new關鍵字修飾;
使用new關鍵字修飾可以將那些和父類有相同函數名和簽名的成員函數隱藏;
舉例說明:
class A { public int x; public void F() {...} public virtual void G() {...} } class B : A { public new int x; public new void F() {...} public new void G() {...} } B b = new B(); b.x = ...; // accesses B.x調用 b的x b.F(); ... b.G(); // calls B.F and B.G調用的F函數和G函數 ((A)b).x = ...; // accesses A.x 調用A的x ((A)b).F(); ... ((A)b).G(); // calls A.F and A.G(although the dynamic type of (A)b is B) //調用A的F函數和G函數,儘管(A)b的類型是B
6.Dynamic Binding (with Hiding)動態綁定(帶覆蓋即new關鍵字)
舉例說明:
第一個簡單的例子:
稍複雜點的例子:
7.子類中的構造函數
8.Visibility protected and internal可見性保護和internal
Protected:
在當前類中可見以及子類中可見
Visible in the declaring class and its subclasses(more restrictive than in Java)
Internal:
在當前Assembly可見
Visible in the declaring assembly (see later)
protected internal:
在當前類中、子類中、當前Assembly中可見
Visible in declaring class, its subclasses and the declaring assembly
9.抽象類和抽象方法
abstract class Stream { public abstract void Write(char ch); public void WriteString(string s) { foreach (char ch in s) Write(ch); } } class File : Stream { public override void Write(char ch) {... write ch to disk ...} }
註釋:
抽象方法不能有實現;
Abstract methods do not have an implementation.
抽象方法隱藏著virtual關鍵字;
Abstract methods are implicitly virtual.
如果一個類中有抽象方法那麼這個類也要聲明為抽象類;
If a class has abstract methods (declared or inherited) it must be abstract itself.
抽象類不能實例化對象
One cannot create objects of an abstract class..
10.Abstract Properties and Indexers抽象屬性和抽象索引器
abstract class Sequence { public abstract void Add(object x); // method public abstract string Name { get; } // property public abstract object this [int i] { get; set; } // indexer } class List : Sequence { public override void Add(object x) {...} public override string Name { get {...} } public override object this [int i] { get {...} set {...} } }
重寫的索引器和屬性必須有和基類相同的get和set方法
Overriding indexers and properties must have the same get and set methods as in the base class
11.Sealed Classes 密封類
sealed class Account : Asset { long balance; public void Deposit (long x) { ... } public void Withdraw (long x) { ... } ... }
註釋:
密封類不能擴展即繼承(在Java中對應關鍵字final),但是可以繼承自其他類;
sealed classes cannot be extended (same as final classes in Java),
but they can inherit from other classes.
重寫方法可以被聲明為單獨的密封
override methods can be declared as sealed individually
12.Class System.Object Object類
class Object { protected object MemberwiseClone() {...} public Type GetType() {...} public virtual bool Equals (object o) {...} public virtual string ToString() {...} public virtual int GetHashCode() {...} } //Directly usable: Type t = x.GetType();//returns a type descriptor (for reflection) object copy = x.MemberwiseClone();//淺拷貝does a shallow copy (this method is protected) //Overridable in subclasses: x.Equals(y) //should compare the values of x and y x.ToString() //should return a string representation of x int code = x.GetHashCode(); //should return a hash code for x
Example
13.重載==和!=運算符