Object-oriented implementation
1 C # classes can have more than one parent class, can implement multiple interfaces
2 briefly describes the concepts of overriding, overloading, and hiding in C #
3 Why calling virtual methods in a constructor method can cause problems
4 How to declare a class in C # cannot be inherited
Object-oriented implementation
1 C # classes can have more than one parent class, can implement multiple interfaces
There can be at most one parent class in C #, but multiple interfaces can be implemented.
2 briefly describes the concepts of overriding, overloading, and hiding in C #
Overrides are the use of the override keyword to re-implement the virtual method in the base class, and in the process of running, the actual type of method will be invoked, regardless of the type of reference.
Shadowing is the re-implementation of a method in a base class with the New keyword, which determines which type of method should be called by the type referenced during the run.
Overloading means that multiple methods share the same name and have the same return value, but can have different parameter lists.
3 Why calling virtual methods in a constructor method can cause problems
Calling a virtual method in the constructor method will result in a run-time error. This is because the constructor method of the subclass is not called when the constructor method is called, but depending on the type of the actual object, the virtual method called by the base class is the method defined in the subclass, which means that a method that has not completed the constructed type object is called, and an unpredictable error can occur.
Examples of construction methods are as follows:
classConstructor {Static voidMain (string[] args) { //constructs an underlying typec C =NewC (); Console.read (); } } Public classRef { PublicRef (strings) {Console.WriteLine (s); } } Public classBase { PublicRef basestring =NewRef ("Base Initialization Expression"); PublicBase () {Console.WriteLine ("Base Construction Method"); } } Public classA:base { PublicRef astring =NewRef ("a initialization expression"); PublicA ():Base() {Console.WriteLine ("a construction method"); } } Public classb:a { PublicRef bstring =NewRef ("b Initialization Expression"); PublicB ():Base() {Console.WriteLine ("B Construction Method"); } } Public classc:b { PublicRef CString =NewRef ("C Initialization Expressions"); PublicC ():Base() {Console.WriteLine ("C Construction Method"); } }
Output Result:
C Initialization expressions
B Initialization expression
A initialization expression
Base initialization expression
Base construction Method
A construction method
B Construction Method
C Construction Method
4 How to declare a class in C # cannot be inherited
C # through the keyword sealed can declare that a type cannot be inherited, and the design should add sealed keywords to all types that are not used as base classes to avoid the various errors that arise from inheritance.
The string class is sealed.
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
. NET Foundation (06) Object-oriented implementation