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.重载==和!=运算符