In-depth understanding of polymorphism

Source: Internet
Author: User

The principle of the Richter replacement

We know that the subclass object can be assigned to the parent class object, or it can be said that the subclass object can completely replace the parent class object and appear anywhere the parent class object can appear, and the behavior of the program will not change, but the parent class object cannot replace the child class Object! For example, a car object can not replace a truck class object (although the truck is also a car), because the car contains a larger range than the truck, it could also be a private car!

This feature is known as the "Richter substitution principle (Liskor substiution Principle)".

Application of the principle of the Richter scale substitution

The Richter replacement principle is one of the important principles of software design, and with the change of the Richter scale, it is possible for inheritance to be reused only if the subclass can replace the parent class without affecting the functionality of the software, the parent class can be really reused, and the subclass will be able to add new behavior on the basis of the parent class.

Use of the IS and as operators

IS is used to check whether the object and the specified type are compatible. For example, to determine whether an element in an employee collection is an SE object, you can use the following code

if  is SE) {//......}

As is primarily used for type conversions between two objects. As shown below

Se se=empls[1 as SE;

And if the type conversion fails with the AS operator, NULL is returned, not an exception!

But that doesn't mean you don't need exception handling.

PM pm=empls[1 as PM;
Pm. Sayhi ();

If the Empls "1" conversion to PM failed, although this sentence is not an exception, but the next sentence Sayhi () method may be error!

Parent class type as parameter

In the Richter substitution principle, it is pointed out that the subclass object can be substituted for the parent class object, so we can write a method that takes the parent class type as the formal parameter when we develop the program, and then pass the subclass object in the actual call to realize polymorphism.

When the program is running, it automatically determines the actual parameter belongs to that subclass, calls the method of the corresponding subclass, and completes the polymorphism.

Summary: The basic steps of using virtual method to realize polymorphism

    1. The subclass overrides the virtual method of the parent class, which has two ways
      1. Create the parent class object and instantiate the parent object with the subclass object
      2. Takes the parent class type as the formal parameter, its subclass object as the actual incoming
    2. At run time, the method is executed according to the type of object actually created.
Why abstract classes and abstract methods use abstract classes and abstract methods

Demo

 Public class traffictool{publicvirtualvoid  Run () {    Console.WriteLine ("  car on the road ");}    }

From this demo we are not difficult to see: if we instantiate a Traffictool object to invoke its run () method, the actual significance is not, because the vehicle itself is a macroscopic, abstract concept, not a specific means of transport. If we do not want this parent class to be instantiated, and only provide the definition of the method, we do not implement it, and let the subclass implement these methods, you can use abstract classes and abstract methods to solve!

An abstract method is a method that is not implemented by declaring an abstract method by adding the abstract keyword in the definition, with the following syntax:

Access modifier abstract return value type method name ();

Note that the abstract method does not have a closed {} parenthesis, but is followed by a semicolon, which means that the abstract method cannot have a method body.

Classes that contain abstract methods are necessarily abstract classes, and we use the abstract keyword to define abstraction classes with the following syntax:

Access modifier abstract class class name {}

Application of abstract classes and abstract methods

When a subclass is derived from an abstract parent class, the subclass inherits all the characteristics of the parent class, including the abstract method it does not implement, and the abstract method must be implemented in its subclasses, unless its subclasses are also an abstract class. As with subclasses overriding a virtual method of a parent class, the Override keyword can also be overridden in a subclass to override the abstract method of the parent class. Its syntax is as follows:

Access modifier override return value type method name ()

The difference between a virtual method and an abstract method

Virtual method Abstract methods
Modify with Virtual To modify with an abstract
To have a method body, even one; Method body is not allowed
You can override the quilt class You must override the quilt class
In addition to sealing the class outside the definition can only be defined in abstract classes

In-depth understanding of 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.