C # review ⑤,

Source: Internet
Author: User

C # review ⑤,

C # review ⑤

June 19, 2016

22: 39

Main Inheritance

1. inherited syntax structure

class A { // base classint a;public A() {...}public void F() {...}}class B : A { // subclass (inherits from A, extends A)int b;public B() {...}public void G() {...}}

In C #, class inheritance can only be single inheritance. in Java, only single inheritance is supported. In C ++, multiple inheritance is supported. However, C #, Java, and C ++ can all implement multiple interfaces.

Single inheritance: a class can only inherit from one base class, but it can implement multiple interfaces.

A class can inherit only one parent class and cannot inherit from struct.

A class can only inherit from a class, not from a struct.

Struct cannot be inherited, but multiple interfaces can be implemented.

Structs cannot inherit from another type, but they can implement multiple interfaces.

C # The base class of all classes is the Object class

A class without explicit base class inherits from Object.

2. Assignments and Type Checks allocation and Type check

class A {...}class B : A {...}class C: B {...}

 

3. Overriding Methods rewrite Method

Only methods declared as Virtual in the parent class can be rewritten in the subclass.

Only methods that are declared as virtual can be overridden in subclasses

4. Dynamic Binding of Dynamic Binding

Benefits of dynamic binding: You can use the following method to create instance objects for different classes.

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") ;}} call example: A a = new B ();. whoAreYou (); // "I am a B" dynamic binding example: void Use (A x) {x. whoAreYou ();} Use (new A (); // "I am an A" Use (new B (); // "I am a B"

 

5. Hiding Overwrite

The member function in the subclass can be modified by the new keyword;

Use the new Keyword to hide member functions with the same function name and signature as the parent class;

Example:

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 calls xb of B. F ();... b. G (); // callb. F and B .G call F and G functions (A) B ). x = ...; // accesses. x calls x (A) B) of ). F ();... (A) B ). G (); // calla.f and. G (although the dynamic type of (A) B is B) // call the F and G functions of A, although (A) B is of the B type

6. Dynamic Binding (with Hiding) (with overwrite Is The new Keyword)

Example:

7. constructor in subclass

8. Visibility protected and internal Visibility protection and internal

Protected:

Visible in current class and in subclass

Visible in the declaring class and its subclasses (more restrictive than in Java)

Internal:

Visible in current Assembly

Visible in the declaring assembly (see later)

Protected internal:

Visible in current class, subclass, and current Assembly

Visible in declaring class, its subclasses and the declaring assembly

9. abstract classes and abstract methods

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 ...}}

Note:

Abstract methods cannot be implemented;

Abstract methods do not have an implementation.

Abstract METHODS hide virtual keywords;

Abstract methods are implicitly virtual.

If an abstract method exists in a class, the class must also be declared as an abstract class;

If a class has abstract methods (declared or inherited) it must be abstract itself.

Abstract classes cannot instantiate objects

One cannot create objects of an abstract class ..

10. Abstract Properties and Indexers Abstract attributes and Abstract Indexer

abstract class Sequence {public abstract void Add(object x);         // methodpublic abstract string Name { get; }         // propertypublic 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 {...} }}

The rewritable indexer and attribute must have the same get and set methods as the base class.

Overriding indexers and properties must have 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) { ... }...}

Note:

The sealed class cannot be extended or inherited (the corresponding keyword final in Java), but it can be inherited from other classes;

Sealed classes cannot be extended (same as final classes in Java ),
But they can inherit from other classes.

The override method can be declared as a separate seal.

Override methods can be declared as sealed individually

12. Class System. Object Class

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 (); // shallow copy (this method is protected) // Overridable in subclasses: x. equals (y) // shocould compare the values of x and yx. toString () // shocould return a string representation of xint code = x. getHashCode (); // shocould return a hash code for x

 Example

13. reload = and! = Operator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.