20172305 "Program Design and Data Structure" Week 8 Learning Summary

20172305 2017-2018-2 "Program Design and Data Structure" Week 8 Learning Summary

Textbook learning content summary

The content of this week is mainly the content of 10.1-10.6 of the book:

  • chapter Ten

1. Post binding (dynamic binding)

2. Polymorphism of inheritance and interface implementation

3. Sorting (selection sort and insertion sort)

4. Search (Linear Search and Binary Search)

Problems and Solving Processes in Teaching Materials Learning

  • Problem 1: Post Binding (Dynamic Binding)
  • Solution to problem 1: When I didn't see this, I always thought that the reference variable was of what type, so I could only call the relevant method. So every time I write a class, I always try my best to improve the relevant methods. The execution efficiency of dynamic binding is lower than that of early binding, but the flexibility embodied by dynamic binding cannot be compared.

    • The type of the reference variable and the object pointed to by the reference variable must be compatible, but not necessarily identical, and the relationship between the two can be very flexible.
  • Problem 2: Polymorphism - one of the three characteristics of object-oriented
  • Solution to problem 2: The basis of Java's implementation of runtime polymorphism is dynamic method dispatch, which is a mechanism for calling overloaded methods at runtime rather than at compile time. The existence of polymorphism can replace and expand the original code, and can perform flexible and diverse operations, which greatly improves the efficiency of use. At the same time, through the explanation of the video content on the blue ink cloud, the types in the database can be modified Or replace it in a database, we can't always call all the data, and then add it, so in order to solve this problem, we can perform a polymorphic application between the two, which can greatly The shortening resolves incidents and resolves issues that arise.

    • Polymorphism means that a reference variable can point to different types of objects at different times. Methods called in this way will be bound to different methods at different times. The bound method code depends on the type of the referenced object.
  • Problem 3: Sorting (selection sort and insertion sort)
  • Solution to Problem 3: The problem of sorting is one of the problems solved with polymorphism. This book introduces two relatively simple sorting methods. I also found different types of sorting methods such as bubble sort, Hill sort, quick sort, and heap sort on the Internet (which shows how powerful the data structure principle is. How crazy)

    • Similarities: the same sorting effect, the same efficiency, similar double-layer loops, all perform about n*n operations
    • Differences: The sorting method is different. The selection sorting method can be replaced by two numbers to achieve a certain order, and the insertion sorting method is to perform a sort similar to a clockwise cycle. The selection sort method performs relatively few swap operations, and the selection method is better than the insertion method.
  • Problem 4: Search (Linear Search and Binary Search)
  • Solution to Problem 4: A linear search is an "one-by-one", "ordered" search starting at an endpoint. The method of binary search is similar to the "dichotomy" that was learned in high school mathematics, constantly taking the midpoint and screening half and half. Compared with linear search, binary search is more efficient, but binary search has a drawback, that is, the elements in the array must be in a certain order (ascending or descending). For the choice of two search methods, the search volume of data is large. The preferred binary search, the efficiency of the search is not an important issue, we can choose linear search.

    • The set of elements being searched is called the search pool
    • For binary search, if it is an even number of elements, we can see from the code when we first select it mid = (min + max) / 2. In the case of inseparability, the middle two numbers will be selected as the middle number, and the binary number will be selected.

Problems and solutions in code debugging

  • Issue 1: PP10.1
  • Solution to problem 1: The writing of 10.1 feels very simple. The conversion from inheritance to interface implementation polymorphism is not a change of method. Alas, during the actual operation, I suddenly found that I had no way to start. I tried to implement the interface in StaffMember, so that all subclasses can use this method, but the Staff is pay()always in the red line, I thought about it in each The interfaces are implemented in the subclasses respectively. Although they pay()are not marked with red lines, when the reference variables are declared and pointed to the subclasses, the red lines are started again. After thinking about it, I did not implement the interface in the StaffMember class, but if the interface is implemented, this is not a kind of polymorphism. Two very confusing polymorphisms implemented through the interface are considered to be on the road of trying. The thorns, just implement the interface in the StaffMemberen class, and finally declare the reference variables of the interface in the production code, so that different objects and methods of different types can be called in StaffMemberen to pay()implement the methods in payday()the interface.
    • interface:
    • Declare a reference variable for the interface:
  • Issue 2: PP10.4
  • Solution to problem 2: For this programming project, it took a lot of effort. The descending order was thought of at the beginning and put the content in the front and put it in the back, but it is not very good to change a symbol after thinking about it. Possibly, so I kept changing it, but I still couldn't achieve the required purpose. By asking my classmates how to solve it, it turned out that my original idea was correct. I feel that the title is deceiving, so I don't bring out this kind of rewriting, just modify a symbol.

  • Question 3: PP10.5
  • Problem 3 Solution: When writing this project, the idea is very simple. With the Sorting class, the names in movies can be arranged in order, but after I use it, I will be reminded that the types are incompatible, and I found that these two It is necessary to build a bridge between them to communicate. Thinking about the essential reason, it is because I did not think of imitating the connection between the three codes of 10.8--10.10. Just build a CampareTo method in CollectionDVD1 and implement the interface Comparable, that's it. It took a day and a half to write this project, and it was only through the guidance of my classmates that I truly understood this question. Therefore, it is still caused by the lack of meticulousness in my reading.
    • Issues are not compatible:
    • Implemented "bridges":

code hosting

Summary of last week's exam mistakes

  • Mistake 1: Inheritance through an extended (derived) class supports which of the following concepts?
    • A.interfaces
    • B.modulary
    • C.information hiding
    • D.code reuse (code reuse)
    • E.correctness
  • Error resolution: By extending a class and inheriting it, the new class does not need to reimplement any inherited methods or instance data, saving the programmer's workload. So code reuse is the ability to reuse other people's code, by extending it to suit your needs. By inheriting the parent class, the duplication of code can be greatly reduced. Regarding the problem of choosing an interface class, I said that I was very confused at the time, and I chose the interface without knowing what was going on.

  • Mistake 2: Which of the following is true regarding Java classes?
    • A.All classes must have 1 parent but may have any number of children (derived or extended) classes
    • B.All classes must have 1 child (derived or extended) class but may have any number of parent classes
    • C.All classes must have 1 parent class and may have a single child (derived or extended) class
    • D.All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes
    • E.All classes can have either 0 or 1 parent class and any number of children (derived or extended) classes
  • Error resolution: Java supports inheritance, but not multiple inheritance, so a Java class can have any number of child elements, but only one parent class. Also, since all Java classes inherit directly or indirectly from object classes, all Java classes have only one parent class. When doing this question, I considered the ultimate parent class, the Object class. Because it has no parent class, I chose the E option. However, the teacher said that the Object class can be regarded as its own parent class. also expressed helplessness. I feel sorry for this...  

  • Mistake 3: A variable declared to be of one class can later reference an extended class of that class. This variable is known as (A variable declared as a class can later reference an extended class of that class. This variable is called. )
    • A.protected
    • B.derivable
    • C.cloneable
    • D. polymorphic (polymorphic)
    • E.none of the above, a variable declared to be of one class can never reference any other type of class, even an extended class
  • Error parsing: The term polymorphism means that a variable can have multiple forms. In general, Java is strongly defined that a variable, once declared of one type, can never be changed to another type. The exception is that a polymorphic variable can be a derived class of any type (although not at the same time, a variable can be converted from one type to another). The problem of polymorphism. After reading Chapter 10, I understand the answer to this question. By declaring an object of the parent class, you can refer to the method of inheriting the subclass of the parent class. This flexible operation is achieved through inheritance. Polymorphism.

  • Mistake 4: In order to determine the type that a polymorphic variable refers to, the decision is made
    • A.by the programmer at the time the program is written
    • B.by the compiler at compile time
    • C.by the operating system when the program is loaded into memory
    • D.by the Java run-time environment at run time
    • E.by the user at run time
  • Error parsing: A polymorphic variable can contain many different types, but it doesn't know which type it is until the program executes. When referencing variables, decisions must be made. This decision is made by the runtime environment based on the latest assignment of variables. This question is also about polymorphism, so after reading Chapter 10, it is a question of dynamic binding, which is less efficient than early binding, but dynamic binding reflects the flexibility of polymorphism.

  • Mistake 5: Using the reserved word, super, one can
    • A.access a parent class'constructor(s)
    • B.access a parent class'methods and instance data
    • C.access a child class'constructor(s)
    • D.access a child class'methods and instance data
    • E.none of the above
  • Error resolution: The reserved word super provides a mechanism for accessing the methods and instance data of the superclass (whether they are hidden or not). Also, super can be used to access the constructor of the parent class. The correct answer is a combination of A and B.

  • Mistake 6: If you instantiate an Abstract class, the class or object you wind up with
    • A.is also an Abstract class
    • B.is a normal class
    • C.is an Interface
    • D.is a reference to an Object
    • E.can't exist you cannot instantiate an Abstract class
  • Error parsing: You can only instantiate concrete classes not abstract classes. But you can extend abstract classes and interfaces.

    • The abstract class can actually be instantiated , but its instantiation method is not to create an object through the new method, but to indirectly realize the instantiation of the parent class by pointing to the instance of the child class through the reference of the parent class (because the child class needs to Before instantiation, its parent class must be instantiated first. In this way, an object that inherits the subclass of the abstract class is created, and its parent class (abstract class) is instantiated). However: the interface cannot be instantiated (the interface has no constructor at all).

  • Mistake 7: A derived class has access to all of the methods of the parent class, but only the protected or public instance data of the parent class. protect or public instance data.)
    • A.true
    • B.false
  • Error Resolution: Since methods can also be declared private, derived classes cannot access any private methods. Therefore, the derived class can only access the protected and public methods and instance data of the parent class. I didn't see access to all methods, so I thought it was right through the second half of the sentence.

  • Mistake 8: 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. (If class AParentClass has a protected instance data x x, and AChildClass is an AParentClass of a derived class, then AChildClass can access x, but cannot redefine x to another type.)
    • A.true
    • B.false
  • Error resolution: A derived class can redefine any instance data or methods of the parent class. The version of the parent class is now hidden, but can be accessed by using super.x. Any instance data and methods of the parent class can be overridden or defined as a different type in the child class.

  • The question of inheritance in the exam is one of the three major characteristics of the object. There are many knowledge points. How to use it flexibly requires continuous learning in practice. Before the exam, I felt that I did not learn this chapter very well. The 26 questions are almost one-third wrong, just like studying for the exams in Chapters 5 and 6, so you have to study hard and constantly reflect on your own learning. Strive to shorten the distance with those outstanding students in the future learning process.

comprehension

  学习第十章的时候,通过老师给的书上例题代码,可以省去不少时间去理解多态性有关的知识点,但是,本周发布了一个感觉很好做的(实则并不是的)一个小组项目-四则运算的编写,但是细细思考了一下,突然发现,本周好忙啊~~关于多态性的问题还没有深入思考的同时还要编写项目的UML类图。好在还有一个五一小假可以忙里偷闲!

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 136/136 1/1 15/15
the second week 570/706 1/2 20/35
The third week 613/1319 1/3 23/58
the fourth week 1249/2568 1/5 30/88
fifth week 904/3472 1/6 30/118
Week 6 540/4012 1/7 30/118
Week 7 826/4838 1/7 30/178
eighth week 925/5763 2/9 45/223

References

Guess you like

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