[C#] Object-oriented polymorphism 01--method hidden concept and composition

polymorphism

1. What is polymorphism?

Answer: It is an object, a variety of forms of behavior, that is, there are many ways (functions) to write, and there are many ways to call objects, such as rewriting, hiding, overloading, and interface

Second, what is the use of polymorphism?

Answer: Provide more choices, have more ways to reuse code, and improve maintainability

Polymorphism --- method hiding

1. What is method hiding?

Answer: It is one of the implementation methods of polymorphism, one of the writing methods (functions) in the class, and a technical concept

Second, what is the use of method hiding?

Answer: Selective reuse, to solve the problem that the method of the parent class is not suitable for the subclass

3. How to implement method hiding?

Answer: Define a method in the parent class, and then define a method with the same name and parameters in the subclass (this method can add "new")

Fourth, implement method hiding with C#

a : Write a method named "Action" in the parent class

b: Write a method with the same name in the subclass, but add new to distinguish

Five, the hidden characteristics of the method

There are two methods for subclasses to call the parent class, and inheritance is directly used for modification and later use

6. Disadvantages of the method

It is impossible to implement the parent class to call the subclass method, but the rewriting technology can

7. Summary:

Polymorphism provides more methods and object writing methods to improve code flexibility. One of the method hiding methods is to create a new method with the same name and parameters as the parent class, and then modify it to the desired content in the method body.

Guess you like

Origin blog.csdn.net/ICD_kfg/article/details/129189680