Deep understanding of polymorphism ., Deep understanding of Polymorphism

Source: Internet
Author: User

Deep understanding of polymorphism ., Deep understanding of Polymorphism

1. Inheritance: if Class A can obtain public information from Class B, this mechanism is called inheritance.

2. About the base keyword
Base. parent class attributes
Base. Parent Class Method
Base ()

3. protected is protected. It can be accessed in the current class and subclass. (Locked/restricted objects: variables in the class)

4. Secret of subclass Construction

5. Two inherited features: Single-stick and pass-through (the subclass can obtain the information of the parent class)

6. Polymorphism:
Multiple objects, which have different responses for the same operation. This mechanism is called polymorphism.
7. polymorphism Conditions
The parent class must have a virtual method, and the subclass must use the Override keyword to Override the virtual method.

7. multi-state advanced Java ++
1. Polymorphism
Lee's replacement principle:
In addition to non-private members and methods of the parent class, subclass can also
It has its own unique member variables and methods.

2. is and
Is used to determine the type
As performs type conversion and () conversion. This line of code does not have errors, if the conversion fails. Returns null.

3. The virtual method of the parent class, which does not need to be forcibly rewritten in the subclass
Abstract method of the parent class. The subclass must be forcibly rewritten .?
 
4. Implementation of polymorphism in abstract methods
The parent class must be an abstract class and define an abstract method.
The subclass must override the abstract method.

5. Notes for abstract classes and Abstract METHODS: handwriting ()
1. Abstract classes are modified with Abstract keywords.
2. abstract methods can only be in abstract classes.
3. the abstract class cannot be instantiated.
4. the abstract method cannot have a method body or even {}.
5. abstract classes cannot be static or Sealed Classes
6. The subclass must override all abstract methods in the abstract class, unless the subclass itself is also an abstract class
7. abstract classes can contain common methods.
8. abstract classes can have Constructors
9. The shortcut for rewriting all abstract methods is Ctrl +.
10. abstract methods in abstract classes are used to constrain the subclass method form (signature ).


Principle: abstract classes can be instantiated, but their instantiation method is not to create an object through the new method,
Instead, the parent class is indirectly instantiated by pointing to the sub-class instance through the reference of the parent class.
(Because the child class will first instantiate its parent class before it is instantiated. In this way, an object that inherits the subclass of the abstract class is created,
The parent class (abstract class) is instantiated ).

Conclusion: abstract classes can be instantiated. They cannot be instantiated through new, but can be automatically scheduled by subclass construction.
6. Interface
The business in the project must use interfaces.
Later: the Interface is just a stuff modified with Interface. ++ Interfaces in development

1. The interface is also a data type
2. Case study: Both planes and birds can fly. They all implement the IFly interface. But they fly in different ways. This is also an implementation scheme of polymorphism.
A class implements 100 methods for an interface. Only one method must be implemented.

. Method public hidebysig newslot abstract virtual instance void Fly () cel managed
{
}

3. interface contamination

4. A class implements N interfaces, that is, the disguised multi-inheritance we mentioned earlier.
Class and interface: class implementation Interface


Interfaces and interfaces: Inherit


5. The parent class variable points to the subclass object.
Typer and Coder
Typist programmer

Boss: dinggang: Typer

Related Article

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.