Abstract thinking common_role of abstract classes _ Role of interfaces _ differences between abstract classes and interfaces (1)

Source: Internet
Author: User

Today, teacher Liang fangming explained the importance of abstract thinking, the role of abstract classes, the role of interfaces, and the role of abstract classes and interfaces. Next, I will review what I learned today. If there are any mistakes or shortcomings, please point out them.

First, let's start with an example. The implementation process of the bubble sort method is as follows:

The first implementation process:

Public void BubleSort (int [] array)
{
For (int I = 0; I <array. Length; I ++)
{
For (int j = 0; j <array. Length-i-1; j ++)
{
If (array [j]> array [j + 1])
{
Int temp = array [j];
Array [j] = array [j + 1];
Array [j + 1] = temp;
}
}

}
}

This Bubble sorting method can only compare arrays with integer elements. If you want to compare arrays of the double, float, and long * types, you must rewrite or reload this method (the overload cannot be performed in a class ). This Code cannot be reused, which is the biggest drawback of this method. Is there a way to reuse Bubble sorting? So what are the commonalities of various types of Bubble sorting?

I. Importance of abstract thinking

What is abstract thinking? In my opinion, abstract thinking is to find the same in different ways and remain unchanged in changes. Consider these identical and unchanged things as commonalities. While common things can be shared. The ability to improve abstract thinking is to improve the ability to extract the commonalities of different things. Different things are not just specific things in reality. Here, things are a broad concept and include common parts of various methods.

Let's discuss the above example of Bubble sorting. Which of the following sorting methods are the same for different data types? What are the differences? Through thinking, we find that the difference is only the comparison part, that is, array [j]> array [j + 1]. If we want to sort the person object by the age field in the person class, our comparison is array [j]. age> array [j + 1]. age. For different parts and the same parts in this method, we should extract them separately. Since the comparison methods for various data types are different, we can define the comparison methods as abstract methods. The class to be compared inherits this abstract class and overwrites this abstract method. In this way, the common part of Bubble Sorting can be reused.

Therefore, the Bubble sorting method should be rewritten:

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.