Initial inheritance and polymorphism

Source: Internet
Author: User

I. Subclasses and Parent classes

1. Subclass: Parent Class

For example:

Dog (Subclass): Anomal (parent Class)

Subclass (derived class) parent class (base class and superclass)

2. Subclasses can inherit those members of the parent class

(Non-Private member, but technically, it can be considered to be all members of the parent class)

When two classes in a software system conform to is a, you can use inheritance

For example:

Student is a person

SE is a employee

Ostrich (ostrish) is a bird (wrong conclusion!!!) )

☆: Inheritance mode What happened behind the construction of the subclass?

If we want to construct a subclass object

//in the Animal parent classclassanimal{ Public stringname{Get;Set}   Public intage{Get;Set}//No reference PublicAnimal () {}//With reference PublicAnimal (stringNameintAge ) {       This. Name=name; This. Age=Age ;}}
//in the Dog classclassdog:animal{ Public stringname{Get;Set}   Public intage{Get;Set}//No reference PublicDog () {}//With reference PublicDog (stringNameintAgestringcolor) {       This. Name=name; This. Age=Age ; This. Color=Color} Public voidempty () {Console.WriteLine ("Eat Mat very happy");        Console.ReadLine (); }}
//  Program    class program    {        staticvoid Main (string[] args)        {            new  Dog ();            Dog.empty ();        }    }

Break Point for debugging

Press F11 to debug below

Cheng normal operation

(1) Enter the subclass constructor method name, but the subclass constructs the method body

(2) Jump to the parent class to construct the method name, then execute the method body constructed by the parent class

(3) The method body of the parent class executes, and the method body of the child class is executed.

(4) The subclass method executes, jumps to the main object function, creates the project on behalf of the subclass object

Conclusion:
As long as you dare to create a subclass object, then there must be a parent object in the background is automatically generated!!!!

Base (A, B): There are several parameters in parentheses, referring to the parameter constructs in the parent class. A/b parameter is named according to the reference of the subclass

Variable naming of construction parameters

Base can access properties and methods

2. Access modifiers (protected)

Public (method, class, member variable): Accessible anywhere

Internal (member variable, class, method): can only be used in the current assembly (access)

Private (method, member variable): can only be used in {} of the current class

Protected protected (member variables and methods): The current class and other subclasses, and subclasses of subclasses

3. Single-Root Sex and transitivity

Uniqueness: A child can have a few biological fathers

Only one

A subclass can have only one direct parent class
4. polymorphic

Polymorphism: Different objects react differently to the same operation, which is polymorphic

5. Call the parent class constructor from base notice point

01. Calling the parent class constructor through base can only be written after the construction of the subclass

02. Calling the parent class through base constructor parameter order is consistent with the constructor of the parent class

Initial inheritance and polymorphism

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.