- inheritance :
A class can inherit all operations, attributes, features, events, and implementation methods from the base class. However, it cannot inherit the instance constructor, destructor, and static constructor, nor inherit the members that display Access denied.
extends the base class and is passed. In addition, a derived class can declare new operations with the same name or signature to hide inherited members.
-
polymorphism : the object can be processed in different ways based on the data class or data type. Or, provide different implementation methods for methods or attributes, but have the same signature. three types of polymorphism: interface polymorphism, inheritance polymorphism, and abstract class polymorphism.
- Interface Polymorphism: Multiple classes can implement the same interface, and one class can also implement multiple interfaces.
- Inheritance Polymorphism: Use the virtual keyword in the base class, and then rewrite the function in the subclass. The inheritance class inherits all methods, attributes, features, and events of the base class. When you do not want to inherit certain features of the base class Central Europe, you can make slight changes.
- Abstract class Polymorphism: Class marked with abstract keywords. Abstract Classes usually have abstract methods. They only define methods that are not implemented and cannot be instantiated. The sub-classes are used to implement functions. In fact, the implementation of polymorphism in abstract classes is a bit like inheriting class polymorphism, but the keywords are different. At the same time, it is similar to the method defined in the interface, that is, only the declaration is provided, indicating that the method will be implemented in the subclass.
For example, using inheritance PolymorphismCodeAs follows:
Inheritance Polymorphism
1 Class Duotai
2
3 {
4 Public Void Test ()
5 {
6 Animal animal = New Animal ();
7 Dog dog = New Dog ();
8 Speak (animal );
9 Speak (DOG );
10 Console. Readline ();
11 }
12
13 Protected Static Void Speak (animal)
14 {
15 Animal. Bark ();
16 }
17
18 }
19
20 Public Class Animal
21
22 {
23 Public Virtual Void Bark ()
24
25 {
26 Console. writeline ( " Animal bark " );
27 }
28 }
29
30 Public Class Dog: Animal
31 {
32 Public Override Void Bark ()
33 {
34 Console. writeline ( " Dog bark " );
35 }
36 }
The running result is animal bark and dog bark.
For example, the use of abstract class polymorphism is as follows:
Abstract Polymorphism
Class Chouxiangduotai
{
Public VoidTest ()
{< br> dog = New dog ();
CAT = New CAT ();
speak (DOG);
speak (CAT);
console. readline ();
}
protected void speak (animal)
{< br>. bark ();
}< BR >}
Public abstract class animal
{< br> Public abstract void bark ();
}
Public ClassDog: Animal
{
Public Override VoidBark ()
{
Console. writeline ("Dog bark");
}
}
Public ClassCat: Animal
{
Public Override VoidBark ()
{
Console. writeline ("Cat bark");
}
}
}