C #. NET technology insider 05-object-oriented concepts

Source: Internet
Author: User
    1. 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.

    2. 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");
 
}
}

}

 

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.