C # Review ⑤
June 19, 2016
22:39
Main Inheritance Inheritance
1. Inherited syntax structure
class // base class int A; Public A () {...} Public void F () {...}} class // subclass (inherits from a, extends a) int b; Public B () {...} Public void G () {...}}
The inheritance of classes in C # can only be single inheritance, only single inheritance is supported in Java, and multiple inheritance is supported in C + +. But C #, Java, C + + can implement multiple interfaces.
Single Inheritance:a class can is inherit from one base class, but it can implement multiple interfaces.
A class can inherit only one parent class and cannot inherit from a struct.
A class can only inherit from a class and not from a struct.
Structs cannot be inherited, but multiple interfaces can be implemented.
Structs cannot inherit from the another type, but they can implement multiple interfaces.
The base class for all classes in C # is the object class
A class without explicit base class inherits from Object.
2.Assignments and type checks allocations and types checking
class A {...} class b:a {...} class c:b {...}
3.Overriding Methods Rewrite method
Only methods declared as virtual in the parent class can be overridden in a subclass
Only methods that is declared as virtual can overridden in subclasses
Method signatures must be the same;
Method signatures must be identical
Same number and types of parameters (including function type!)
Same visibility (public, protected, ...).
Properties and indexers can also be overridden (corresponding to the keyword virtual and override);
Properties and indexers can also be overridden (virtual, override).
Static methods cannot be overridden.
Static methods cannot be overridden.
4.Dynamic binding Dynamic Bindings
Benefits of Dynamic Binding: You can use the following method to construct an instance object that is valid for different classes.
classA { Public Virtual voidWhoareyou () {Console.WriteLine ("I am an A"); }}classb:a { Public Override voidWhoareyou () {Console.WriteLine ("I am a B"); }} Call Example: a=NewB (); a.whoareyou (); //"I am a B"examples of dynamic bindings:voidUse (A x) {x.whoareyou ();} Use (NewA ());//"I am an A"Use (NewB ());//"I am a B"
5.Hiding coverage
member functions in subclasses can be modified by the new keyword;
Use the new keyword modifier to hide those member functions that have the same function name and signature as the parent class;
To illustrate:
classA { Public intx; Public voidF () {...} Public Virtual voidG () {...}}classb:a { Public New intx; Public New voidF () {...} Public New voidG () {...}} b b=NewB (); b.x= ...;//accesses b.x call B's xB.f ();.. b.g (); //calls F function and g function called by B.F and B.G((A) b). x= ...;//accesses a.x call A's X((A) b). F (); ... ((A) b). G (); //calls A.F and A.G (although the dynamic type of (A) b is b)//call A's F function and G function, although the type of (a) B is B
6.Dynamic binding (with hiding) dynamic bind (with overwrite as new keyword)
To illustrate:
The first simple example:
A slightly more complex example:
7. Constructors in subclasses
8.Visibility protected and internal visibility protection and internal
Protected:
Visible in the current class and in subclasses
Visible in the declaring class and its subclasses (more restrictive than in Java)
Internal:
Visible at current assembly
Visible in the declaring assembly (see later)
protected internal:
Visible in the current class, in the subclass, in the current assembly
Visible in declaring class, its subclasses and the declaring assembly
9. Abstract classes and abstract methods
Abstract classStream { Public Abstract voidWrite (Charch); Public voidWriteString (strings) {foreach(CharChinchs) Write (CH); }}classFile:stream { Public Override voidWrite (Charch) {... write ch to disk ...}}
Comments:
Abstract methods cannot be implemented;
The Abstract methods do not has an implementation.
The abstract method hides the virtual keyword;
Abstract methods is implicitly virtual.
If there is an abstract method in a class, then this class is also declared abstract class;
If A class has an abstract methods (declared or inherited) it must be abstract itself.
An abstract class cannot instantiate an object
One cannot create objects of an abstract class.
10.Abstract properties and indexers abstract attributes and abstract indexers
Abstract classSequence { Public Abstract voidADD (Objectx);//Method Public Abstract stringName {Get; }// Property Public Abstract Object This[intI] {Get;Set; }//Indexer}classList:sequence { Public Override voidADD (Objectx) {...} Public Override stringName {Get {...} } Public Override Object This[intI] {Get{...}Set {...} }}
The overridden indexer and property must have the same get and set methods as the base class
Overriding indexers and properties must has the same get and set methods as in the base class
11.Sealed Classes Sealing Class
Sealed class Account:asset {long balance; Public void Deposit (long x) {...} Public void Withdraw (long x) {...} ...}
Comments:
Sealed classes cannot be extended or inherited (corresponding to the keyword final in Java), but can be inherited from other classes;
Sealed classes cannot is extended (same as final classes in Java),
But the they can inherit from the other classes.
The override method can be declared as a separate seal
Override methods can declared as sealed individually
12.Class System.Object Object Class
classObject {protected ObjectMemberwiseClone () {...} PublicType GetType () {...} Public Virtual BOOLEquals (Objecto) {...} Public Virtual stringToString () {...} Public Virtual intGetHashCode () {...}}//Directly Usable:Type T= X.gettype ();//returns a type descriptor (for reflection)Objectcopy = X.memberwiseclone ();//shallow copy does a shallow copy (this method is protected)//Overridable in subclasses:x.equals (y)//should compare the values of x and yx.tostring ()//should return a string representation of xintCode = X.gethashcode ();//should return a hash code for x
Example
13. Reload = = and! = operator
C # Review ⑤