20172308 "Program Design and Data Structure" Eighth Week Learning Summary

20172308 2017-2018-2 "Java Programming" Eighth Week Learning Summary

Textbook learning content summary

1. Polymorphism
a. Embodiment:
The parent class reference points to its own subclass object;
the parent class reference accepts its own subclass object. (eg: 父类 f = new 子类() ;)
b. Benefits: greatly improve the scalability of the program.
c. Premise: There is a relationship between classes (inheritance, implementation)
d. Disadvantages: Improve extensibility, but only use the reference of the parent class to access the members of the parent class (methods that the child class has but the parent class does not have) , cannot be called directly)
Solution: In fact, this declaration 父类 f = new 子类() ;is a process of type promotion (upcasting). You can cast the parent class reference to the subclass type by 子类 z =(子类)f ;forcing, and then call the subclass's specific method (this is a downcasting process). However, what can be converted is that the parent class reference points to its own subclass object, and the reference can be promoted or cast; and if it is a superclass type object, it cannot be converted to a subclass object (eg, 父类 f = new 父类() ; 子类 z =(子类)f ;)
e .Features: In polymorphism, the subclass object is changing from beginning to end.
f. Type: polymorphism is achieved by inheritance; polymorphism is achieved by using interface

2. Sorting
a. Sorting by selection method: Scan the entire sequence backwards from the first element, swap the minimum value with the first element, then scan backwards from the second element, and compare the minimum value with the second element Elements swap positions until the end.
b. Insertion sorting: start from the second element and compare with the first element, and the second element is exchanged with it, otherwise it is not exchanged; then start from the third element, start from the first element, and compare until If it is smaller than an element, it is inserted before the element until the last element.
Comparison: The actual sorting efficiency of the two methods is the same, and the algorithm performs the same number of comparison operations, but the selection method performs fewer exchanges, so the selection method is better than the insertion method.

3. Search
a. Linear search: starting from one endpoint, scan (compared with all elements) the entire search pool in a linear fashion
b. Binary search: provided that the array elements are ordered (ascending or descending), note: excluded pending The search data includes midpoint elements; if the number of elements to be searched is an even number, two midpoint values ​​are generated, and the first one is selected as the new midpoint.
Comparison: The choice of algorithm depends on specific conditions (for example, binary search is more efficient if the elements of the array to be searched are in order; linear search is relatively simple if the elements are stored in array form)

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: How to understand the drawbacks of polymorphism, that is, the specific methods of subclasses cannot be called directly:

  • Problem 1 Solving Process: Refer to the blue ink cloud video; for example, 父类 f = new 子类() ;, f.子类方法(). At this time, whether these two sentences can be passed is divided into two processes: one is whether it can pass the compilation, but whether it can be run. When compiling, it will refer to the class to which the reference variable belongs (ie, the parent class), and whether there is a method to be called (ie. Subclass method () ). If there is, it will pass the compilation, if not, it will not pass the compilation; if the compilation is passed, then in the running phase, see whether there is a calling method in the class to which the object belongs (ie, the subclass). There is another point to note here, that is, the above are all for non-static member functions in polymorphism, they have overridable functions, so they reflect polymorphism; and if they are static members (functions and variables) , whether it is compiling or running, it refers to the class to which the reference type belongs (that is, the parent class), and whether there is a method to be called.

  • Question 2: Example 10.6 of the textbook, there is a sentence in front of it that says "Executive (subclass) constructor passes its own information to the Employee constructor", that is:

    I don't understand the meaning of this sentence very well. According to my understanding, it should be "subclass". The class overrides the constructor of the parent class".

  • Problem 2 solution process: Baidu. This is the case in C++, I don't know if it is suitable or not: the original parent class needs parameters when it is constructed, and the subclass inherits the parent class, and the constructor of the parent class without parameters cannot be executed. The passed parameters are used for the execution of the constructor of the parent class (in layman's terms, it is generally used to initialize the member variables of the parent class, etc.). Parameters are passed to the constructor of the parent class. Generally, the running order of the program is from the parent class to the subclass. If you want to pass parameters to the parent class when the subclass is defined, you must explicitly call the constructor of the parent class. I also understand the previous problem here, that is why the super() method is not written in the subclass constructor, the compiler will automatically call the super() method, that is, the constructor of the parent class is called: the reason why it is necessary to call the constructor of the parent class method because in the parent class, there may be private properties that need to be initialized within its constructor.

[Reference]:
Baidu knows
chrisfxs's column
with the same style of code farmer's blog

  • Question 3: The book says: "If objects can be naturally related by inheritance, polymorphism is achieved through inheritance; if the main problems that objects coexist need to be handled in different ways, then polymorphism is established through interfaces. sex". This sentence is understandable, but I feel that each should have its own advantages.

  • Problem 3 solution process: Baidu took a look: "Interface is conducive to the expansion of code, while inheritance is conducive to the modification of code functions", suddenly realized.

[Reference]:
The Shepherd's World

Problems and solutions in code debugging

  • Question 1: After-school exercise PP10.1. At the beginning of this topic, I felt that I had no idea where to start, because this example involves many classes, and there is an inheritance relationship between the classes, so I want to use an interface to implement it, and I don't know where to start.

  • Problem 1 Solving process: Refer to the UML class diagram in front of the example in the textbook, and the video resources of Lan Moyun. The first is the first question, why is this interface called Payable. Guessed a bit, the reason why it is called this should be as long as the payment method is implemented (by the way, toString is also written). Then it is to write this interface and write the methods in the other classes mentioned above. The interface code is shown

    in the figure: I wrote two payment methods in the code, that is because the payment methods in the original class are called these two names respectively, which leads to a problem: the class that refers to the interface cannot implement this function at the same time. Two methods, so make the class that references the interface into an abstract class. Although it is enough to add abstract, the subsequent declaration of the main program will also be affected, as shown in the figure:

    The object personnel is declared with the interface class Payable and points to staff2, but since there is a pay() method in the Volunteer class that must be implemented, So here you need to rewrite this method as in the picture above. The same is true for other classes, as shown in the figure:

    Another problem here is that if it is abstract, it cannot be instantiated, as shown in the figure:

    This requires rewriting each payday method when instantiating it, as shown in the figure:

    So, You can call all payment methods pay from the beginning, and then rewrite it is no problem, and the referenced class does not need to be abstract.

  • Question 2: After-school exercise PP10.5. The original code of Movies is shown in the figure:

    This program achieves the purpose of adding DVD information by writing the addDVD method in another class, and then stores the information in the DVD-type array. The initial approach was to print the array directly in Movies, but found that the memory address was printed (and the address name was the same), so I checked whether toString was wrongly written, but it was not.

  • Problem 2 Solution: Later, I had no choice but to declare a DVD array directly in Movies to save the original data. Later, I found that there is also an advantage in doing so, that is, you can directly use the traversal method to print all the data. The picture is a screenshot of the code written in the process:

    as shown in the picture above, the modification is mainly reflected in the red area:
    the inheritance at the top is the original idea, which is to inherit the variables in the DVDCollection class through inheritance. The variables and methods are set to public, and the array object is also set to static. But didn't solve the problem.
    Later, the second idea was adopted, which is the red area in the middle. In this way, you can print it directly, then write a sorting method, print it again, and compare the effect of sorting, that is, the result of the last modification. The comparison effect is shown in the figure:

    So the most important operation is to write a name Sorted class. It is the selectionsort2 method called in the third red area of ​​the written code. Comparing the Contact method of the example, write down the two objects to be compared in the DVD class, as shown in the figure:

    In the Sorting2 class, write down the comparison sequence of calling the above method, the code is shown in the figure: The

    final code is shown in the figure:

code hosting

Summary of last week's exam mistakes

  • Error 1:

  • Analysis of wrong question 1: This question actually examines what is multiple inheritance, but in practical examples, it is difficult to distinguish. Multiple inheritance means that a given class inherits from multiple parent classes. The PC in B is either desktop or notebook; the notebook in C inherits the characteristics of both PC and portable.

  • Error 2:

  • Analysis of wrong question 2: I think there is nothing wrong with choosing E for this question. Isn't the Object class the parent class of all classes, does it also have a parent class?

  • Error 3:

  • Analysis of wrong question 3: I really don't know this question. Baidu has not found the answer, nor can I find it in the book. Relationships between two subclasses of the same parent class are called siblings. clone is a copy of the same object, aliases are the same object

  • Error 4:

  • Analysis of wrong question 4: This question is really nothing to say, the bottom paragraph of the 198th page of the textbook. I thought it was has-a, and I thought it would never be wrong. . .

  • Mistake 5:

  • Analysis of wrong question 5: The reason for the error in this question is that option A cannot be translated. Well, in fact, it is my fault for relying too much on Youdao translation.

    If you directly translate the super class (super class), you will not be wrong.

  • Mistake 6:

  • Error 6 Analysis: . . . Nothing to say, the answer is a combination of A and B, so neither answer is correct, choose E. When I did the question, I didn't dare to choose E, so I chose the one that was absolutely right (the book has the original words), but I didn't expect it to be a routine.

  • Error 7:

  • Analysis of wrong question 7:
    According to Youdao's translation, answer B is wrong, choose E.

  • Error 8:

  • Error 8 Analysis: Any class can be extended, whether it is an interface, an implementation interface, or neither. unless declared final.

  • Error 9:

  • Analysis of wrong question 9: The error of this question lies in the wrong understanding of what "access" is. The subclass will inherit all the variables and methods (including private) of the parent class and reserve space for variables, but the subclass cannot be named by name. Refers to the parent class private member.

  • Error 10:

  • Error 10 Analysis: Subclasses can redefine any instance data or methods of the parent class through super.x.

  • Error 11:

  • Analysis of wrong question 11: The above question is wrong, and this question must be followed by a mistake, after all, it is the same question. . .

Pairing and mutual evaluation

  • Worth learning or questions from the blog:
    • Hou Zeyang's blog has neat layout and beautiful interface
    • The problem summary is very comprehensive
    • For the doubts in the book, I will always find a way to solve it. This spirit of exploration is worth learning.
  • Worth learning or problems in the code:
    • For programming, you can always find an angle to solve it
  • Pair study this week
    • 20172302
    • Pair learning content
      • Chapter 10 Content: Polymorphism

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 30 articles 400 hours
the first week 309/309 1/1 20/20
the second week 269/578 1/2 18/38
The third week 236/776 1/3 22/60
the fourth week 507/1283 2/5 30/90
fifth week 631/1914 1/6 30/120
Week 6 529/2443 1/7 25/145
Week 7 515/2958 1/8 25/170
eighth week 1128/4086 2/10 50/220

References

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325160191&siteId=291194637