20172316 2017-2018-2 "Program Design and Data Structure" Week 8 Learning Summary
Textbook learning content summary
- The core word of this chapter is polymorphism .
- The beginning introduces binding, especially post-binding. Binding is to associate the method name with the executed object, thereby distinguishing the application of the same method on different objects. The front (static) binding is already determined during programming, and the binding efficiency is high. Post (dynamic) binding is inefficient, but it is more flexible. The binding method used in this chapter is post binding.
- Polymorphism, as I understand it, is the property that the same method can act on different classes.
- There are two ways to achieve polymorphism: inheritance and interface.
We have already seen the polymorphism caused by inheritance. ArrayList can store any type of object before specifying the element type, because all classes are subclasses of the Object class. The example in the bookmove()
is a method. After the reference variableAnimal creature = new Horse
pointsHorse
to (Horse is a subclass of Animal), the method increature.move()
it is called . It is also very convenient to implement polymorphism using interfaces, which is very similar to implementing polymorphism in abstract classes in inheritance. An interface can inherit an interface, and a class can inherit an interface, but an interface cannot inherit a class.Horse
move()
- Sorting, there are two sorts in the book: selection sorting and insertion sorting. The basis of sorting is the implementation of the Comparable interface. Arrange objects in an orderly manner by comparing "size". The number of exchange operations performed by the visa is less, which is better than the insertion method.
- Search, the book provides two searches: linear search, binary search, binary search is more efficient, but the premise is that the search pool has been sorted.
Problems and Solving Processes in Teaching Materials Learning
There are no major problems in the study of the textbook this week, and the teachers and teaching assistants have answered them in detail.
Problems and solutions in code debugging
- Question 1: I want to try to implement polymorphism by inheritance. The basic idea is that in the Demo class
i = 4
and the SuperDemo classi = 7
, the final output should be in the SuperDemo class, but the7
result isSuperDemo = 0
. - Problem 1 Solution: Find similar questions online and get a little inspiration. When executing
new SuperDemo()
, the parent class is called firstDemo()
, and the method of the subclass is referenced in the constructionshow
, but at that time the subclass construction has not been executed, and thei
variable is only allocated memory and given the default value0
, so it is printed0
. should be.
code hosting
(screenshot of the running result of the statistics.sh script)
Summary of last week's exam mistakes
Wrong question 1: Which of the following is true regarding Java classes?
Correct answer: A .All classes must have 1 parent but may have any number of children (derived or extended) classes
Reason: Every class is directly or indirectly an Object Subclass of class.Wrong question 2: A variable declared to be of one class can later reference an extended class of that class. This variable is known as
Correct answer: D .polymorphic
Reason: I don't have a good understanding of the meaning of the title, thinking it is "in a class Variables can be referenced by their own subclasses" and so on, so they were mistakenly selected as protected.Wrong question 3: In order to determine the type that a polymorphic variable refers to, the decision is made
Correct answer: D .by the Java run-time environment at run time
Reason: The type judgment of polymorphic variable refers to the program runtime , which is dynamic binding.Wrong question 4: Using the reserved word, super, one can
Correct answer: E .none of the above
Reason: super reserved word can refer not only to constructors, but also to methods and data.False Question 5: If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but can not redefine x to be a different type.
Correct Answer: B .false
Reason: This is OK This is achieved through the use of the reserved word super.
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 | |
fifth week | 260/1549 | 1/9 | 15/110 | Understand the importance of preview |
Week 6 | 358/1907 | 2/11 | 20/130 | Significantly improved typing speed |
Week 7 | 780/2687 | 2/13 | 15/145 | Learn to use JUnit |
eighth week | 2124/4811 | 2/10 | 15/160 | without |
References
- "Java Programming Tutorial (Eighth Edition)" Electronic Industry Press
- "Learn Simple Tutorial Using Code Cloud and Blog Garden"
- "Using Open Source China (Code Cloud) Managed Code"