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: