Three major characteristics of object-oriented: Polymorphism and three polymorphism of object-oriented

Source: Internet
Author: User

Three major characteristics of object-oriented: Polymorphism and three polymorphism of object-oriented
What is polymorphism?

In a word, polymorphism means that different interpretations can be made when the same operation (method) Acts on different objects to produce different execution results. This sentence seems simple

In fact, it is quite profound. Next we will have a deep understanding of the significance of multi-state and how to use polymorphism.

 

Benefits of using polymorphism Programming

When using inheritance programming, we generally write the methods of different sub-classes in different sub-classes, and use the is a keyword to determine the object type.

And then perform forced conversion to use different object methods.

However, if there are multiple child classes, do I have to write multiple if statements to determine which type of object is used? This will produce a lot of redundant code, and it will be very complex

.

Using polymorphism programming can solve this problem well. When the parent class writes a virtual or abstract method, the Child class overrides the method of the parent class.

The type of the runtime determines which method to call. Is this much more concise?

 

Small Example

Suppose we have a "book" class, and many sub-classes inherit the "book" class. For example, the "Computer composition principle" class inherits the "books" class, and the "in-depth Java Object-Oriented" class inherits the "books" class...

These subclasses have similar methods, that is, they are used by different people.

Next we will use the code to implement it.

1 /// <summary> 2 /// Demo class 3 /// </summary> 4 class Demo 5 {6 List <Book> books = new List <Book> (); 7 8 /// <summary> 9 /// Test 10 /// </summary> 11 public void Test () 12 {13 books. add (new ComputerBook (); 14 books. add (new javatek (); 15 16 foreach (Book item in books) 17 {18 if (item is ComputerBook) 19 {20 (ComputerBook) item ). byReading (); 21} 22 if (item is javatek) 23 {24 (javatek) item ). byReading (); 25} 26} 27} 28} 29 30 // <summary> 31 // book class 32 // </summary> 33 class Book34 {35 36} 37 38 /// <summary> 39 // computer books 40 /// </summary> 41 class ComputerBook: book42 {43 // <summary> 44 // read 45 // </summary> 46 public void ByReading () 47 {48 Console. writeLine ("I am a computer book and used by college students. "); 49} 50} 51 52 // <summary> 53 // Java book 54 /// </summary> 55 class javatek: book56 {57 // <summary> 58 // read 59 // </summary> 60 public void ByReading () 61 {62 Console. writeLine ("I Am a Java book and used by industry Java programmers. "); 63} 64}

 

Through this code, we can find that when using a subclass object, we need to use is a to determine the type of the subclass object. That is to say, if a parent class has 10

It is too cumbersome to use 10 if statements to determine the type of the subclass object.

At this time, the virtual method will be available.

 

Virtual Method

Concept:

The virtual method has a method body, which can be overwritten and modified using virtual.

The virtual method in the parent class does not have to be overwritten by the quilt class. The default Implementation of the virtual method can be provided in the parent class. If the subclass does not override the virtual method of the parent class, it will still execute

Default Implementation of the parent class. If the subclass overrides the virtual method of the parent class, the method after the subclass override is executed.

In fact, method Overloading is also a form of implementation of polymorphism, but the methods of overloading are all in the same class, and the methods of using virtual methods to achieve polymorphism are scattered in inheritance

.

Usage:

Use the virtual keyword in the parent class to define the virtual method, and use the override keyword in the subclass to override the virtual method in the parent class.

Override Keyword:

Methods Modified by the override keyword are called method rewriting.

 

Below we use the virtual method to transform the above example

1 /// <summary> 2 /// Demo class 3 /// </summary> 4 class Demo 5 {6 List <Book> books = new List <Book> (); 7 8 /// <summary> 9 /// Test 10 /// </summary> 11 public void Test () 12 {13 books. add (new ComputerBook (); 14 books. add (new javatek (); 15 // the time duration does not need to be judged 16 foreach (Book item in books) 17 {18 item. byReading (); 19} 20} 21} 22 23 // <summary> 24 // book class 25 /// </summary> 26 class Book27 {28 public virtual void ByReadi Ng () 29 {30 31} 32} 33 34 // <summary> 35 // computer books 36 /// </summary> 37 class ComputerBook: book38 {39 // <summary> 40 // read 41 // </summary> 42 public override void ByReading () 43 {44 Console. writeLine ("I am a computer book and used by college students. "); 45} 46} 47 48 // <summary> 49 // Java books 50 /// </summary> 51 class javatek: book52 {53 // <summary> 54 // read 55 // </summary> 56 public override void ByReading () 57 {58 Console. writeLine ("I Am a Java book and used by industry Java programmers. "); 59} 60}

    

 

It can be seen that it is much easier to use virtual methods to write code. But the problem comes again. The parent class of the subclass and the virtual methods in the parent class are "macro", "abstract", and I

We don't want this parent class to be instantiated and only provide the definition of the method. What should we do if this method is implemented by the subclass?

How can this problem be solved? We can use abstract classes and abstract methods.

 

Abstract classes and abstract methods

Abstract keywords:

Abstract keywords are used to modify abstract classes and abstract methods.

Abstract class:

Abstract classes modified with abstract keywords cannot be instantiated.

Abstract classes can have non-abstract methods.

Abstract classes cannot be sealed or static.

Abstract method:

The abstract method modified by the abstract keyword is an unimplemented method, which is implemented by rewriting the abstract method by the subclass.

The abstract method does not have braces and ends with a semicolon ";" directly after the parentheses.

Classes containing abstract methods must be abstract classes.

An abstract method must be implemented in its subclass unless its subclass is also an abstract class.

    

Next we will rebuild the above example using abstract classes and abstract methods.

1 /// <summary> 2 /// Demo class 3 /// </summary> 4 class Demo 5 {6 List <Book> books = new List <Book> (); 7 8 /// <summary> 9 /// Test 10 /// </summary> 11 public void Test () 12 {13 books. add (new ComputerBook (); 14 books. add (new javatek (); 15 // the time duration does not need to be judged 16 foreach (Book item in books) 17 {18 item. byReading (); 19} 20} 21} 22 23 // <summary> 24 // book class 25 /// </summary> 26 abstract class Book27 {28 public abstract vo Id ByReading (); 29} 30 31 /// <summary> 32 // computer book 33 /// </summary> 34 class ComputerBook: book35 {36 // <summary> 37 // read 38 // </summary> 39 public override void ByReading () 40 {41 Console. writeLine ("I am a computer book and used by college students. "); 42} 43} 44 45 // <summary> 46 // Java book 47 /// </summary> 48 class javatek: book49 {50 // <summary> 51 // read 52 // </summary> 53 public override void ByReading () 54 {55 Console. writeLine ("I Am a Java book and used by industry Java programmers. "); 56} 57}

    

Here, we can find that the use of virtual and abstract methods for programming (that is, polymorphism) is more in line with the idea of object-oriented programming, and can greatly improve the Code

Readable and redundant code.

It is nice to use less code to implement the same implementation.

 

 

 

    

Terminator: What does it feel like falling in love with someone? I think it may be the feeling of falling in love with programming. If you don't read a book a day, you will be itchy and panic if you don't write code.

 

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.