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

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

Textbook learning content summary

Post binding:

  • Polymorphic references are reference variables that can point to different types of objects at different times.
  • At some point in the execution of the program, a request is generated to execute a certain piece of code to complete a method call, which is called the binding of a method call to a method definition.
    For polymorphic references, the binding is delayed until the program runs, and the method definition to be bound depends on the object referenced by the reference variable at that time. This delayed request event is called late binding or dynamic binding.

Implement polymorphism:

Polymorphic references can be established in two ways: inheritance and interfaces.
Use inheritance to achieve polymorphism:
  • A reference variable can point to any object of any class that has an inheritance relationship.
  • Any class is a descendant of the Object class, and its reference can point to any object.
  • The actual version of the method that will be called depends on the type of the object and not the type of the reference variable. (I came across it in the exam)
  • Types that the parent class can use as a method parameter

    Polymorphism with interfaces
  • Interface names can be used to declare object reference variables
  • An interface reference variable can point to any object of any class that implements the interface.
  • The relationship between an implementing class and its implementing interface is the same as the relationship between a subclass and a parent class. A "is" relationship that lays the foundation for polymorphism.
  • When using the interface reference variable, only the methods defined in the interface can be called, even if the object pointed to by the interface reference variable has other available methods, it cannot be called.
  • Interface names can also be used as types for method parameters.

sort

  • Sorting by selection method: In a list, start from the first number, scan from left to right, encounter the smallest number, swap it with the first number,
    and then start from the next number, continue to repeat the above Operation, continue to find the smallest, until the last number is sorted, forming an orderly arrangement.
  • Insertion Sort: Continuously insert a new element into a sorted subset of the sequence. In each round of sorting, a
    new unsorted element is inserted into the sorted subset at the appropriate position until the entire sequence is sorted. Start with a "sorted" array with only one element, sort the first two values ​​of the array,
    swap the positions of the two values ​​if necessary, and insert the third value relative to the first two values ​​(sorted value) appropriate location. Whenever an insert
    operation , the number of values ​​in the sorted subset is incremented by 1 and the process is repeated until all the values ​​are inserted into their proper positions, and other values ​​in the array will be moved to free up the position space for insertion Position element,
    then the sorting of the entire sequence is completed.

search

  • Linear search: From an endpoint, the entire search pool is linearly traced, so it is called a linear search.
  • Binary search: If the elements in an array are ordered (ascending or descending), binary search is much more efficient than linear search algorithms.
    Binary search takes advantage of the fact that the search pool is ordered, reducing a large number of comparison operations.

polymorphic design

  • Polymorphism allows inconsistent behavior to be achieved in a consistent way.
  • You should train your software design sensitivity and be good at identifying potential problems that can be solved using polymorphism.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: How does inheritance support polymorphism?
  • Solution to Problem 1: Observe the textbook examples and find that polymorphism is actually a process of accomplishing a specified goal in different ways, but with the same behavior or purpose.
    Then, in Java, a reference variable declared with the parent class can point to a child class object. If two classes have two methods with the same signature, the parent class reference is
    polymorphic (because there are different versions of the same method name).
    Just like the example in the textbook, the Exployee class has a pay() method, and the subclasses that inherit it also have a pay() method, so that polymorphism occurs when the object is instantiated in the staff class.

  • Question 2: There is a sentence in the textbook: "If the pay() method is not overloaded in the Volunteer class, the StaffMember should be designed as an abstract class that cannot be instantiated."
    So what is the relationship between overloading, overriding and polymorphism? ?
  • Solution to Problem 2: When a subclass overrides the definition of its parent class method, both versions of the method actually exist. Overriding provides polymorphism because the appropriate method is invoked based on the currently referenced object, and if the method is invoked with a polymorphic reference, the version of the invoked method is determined by the type of the object that executes the method invocation.
    For example, there is a bird A and a bird B, A taught B to eat and fly.
    For flying, A understands that it is nothing more than vibrating wings, while B has a deeper understanding, vibrating wings and making good use of the wind. This is an overload. B adds a parameter to flying.
    For eating, A likes to eat bugs, and B likes to eat grass. This is rewriting. B rewrites the method of eating.
    And A and B are a good example of polymorphism.

Two references for you:

The difference between overloading and rewriting
Difference analysis of several concepts of rewriting, overriding, overloading, and polymorphism

Problems and solutions in code debugging

  • Question 1: When I completed the after-class exercise of PP10.1, I created an interface without modifying the staff class, and then the

  • Problem 1 solution: how to implement polymorphism in interface implementation?
    Before, I wanted to implement the interface of all the related classes containing the payday method to achieve the purpose, but the object instantiated in the Firm was only the Staff class, so I directly rewrote the staff,

It was found that polymorphism can be achieved in this way. (mainly because there are no examples in the textbook, so the understanding is not very clear)

code hosting

Summary of last week's exam mistakes

  • Error 1 and the reason, understand the situation

What is polymorphism achieved by?
Analysis: In fact, I have some conclusions above on this topic. Overloading just provides an alternative for methods with different parameter lists.
Overriding provides polymorphism because the appropriate method is called based on the currently referenced object. Embedding is a class contained within a class. Abstraction has nothing to do with polymorphism
. Encapsulation is implemented using visibility modifiers (public, private, protected).

  • Wrong question 2 and why, understanding the situation

    Upcasting is completely safe, it is a product of the single inheritance structure supported by Java. In contrast, downcasting must be done explicitly by the programmer.
    Java only automatically converts in one direction. The rules for upcasting and downcasting do not depend in any way on the visibility modifiers used.

So what exactly does upcast mean?

1) Upcasting
(1) Definition: The practice of treating a reference to an object as a reference to its base class is called "upcasting".
This is mainly because the object of the subclass can be regarded as the object of the base class, that is, it has an is-a relationship.
For example:
Useful useful = new MoreUseful();//The right side is an object of a subclass, and the left is a
variable of the parent type //, pointing to the subclass object on the right.
(2) The base class can receive any message sent to the derived class, because the two have exactly the same interface, we only need
to cast from derived class up, and never need to know the exact type of the object being processed, which is polymorphism Sexually determined
. Using polymorphism, methods with the same method name and method characteristics
can generate different actions according to the type of the object that calls the method, which greatly increases the expressiveness of the programmer.

This is a reference
to upcast and downcast in java

  • Error 3 and reasons, understanding the situation

While inheritance and interfaces support polymorphism, this is only done if there is late binding. However, overloading is a form of polymorphism one (method) name, multiple subjects,
and polymorphism is in use whenever a program uses overloading.

  • Error 4 and reasons, understanding the situation

  • Analysis:
    Previously because the println() method was thought to be able to handle such a wide range of objects and print them correctly, this is an overload of the println() method, and overloading is not
    a manifestation of polymorphism. In fact, this question is wrong for the same reason as the previous question. While inheritance and interfaces support polymorphism, this is only done if there is late binding. However,
    overloading is a form of polymorphism-one (method) name, multiple subjects, and polymorphism is in use as long as the program uses overloading

Pairing and mutual evaluation

Grading

  1. Correct use of Markdown syntax (plus 1 point):
    • No points for not using Markdown
    • No extra points for grammatical errors (links can't be opened, tables are incorrect, lists are incorrect...)
    • No points for messy typography
  2. The elements in the template are complete (plus 1 point)
    • No points for lack of "Problems and Solving Processes in Textbook Learning"
    • No bonus points for missing "Issues in Code Debugging and Resolution Processes"
    • No bonus points for code hosting that cannot be opened
    • No points will be added for those that cannot be opened without "pairing and mutual evaluation"
    • No extra points for missing "summary of last week's exam mistakes"
    • Missing "progress bar" can not add points
    • No extra points for missing "references"
  3. Problems and solving process in teaching material learning, one point is added for each problem

  4. Problems and solutions in code debugging, plus 1 point for each problem

  5. This week's valid code exceeds 300 branches (plus 2 points)
    • No extra points for submissions less than 20 times a week
  6. Other bonus points:
    • 1 point for blogging before Friday
    • Feelings, experience is not fake plus 1 point
    • Nice typography plus a point
    • Add 1 point for recording the learning time and improvement in the progress bar
    • 1 point for writing new code by hand
    • After-class multiple-choice questions with verification plus 1 point
    • 1 point for code Commit Message specification
    • Add 1 point for in-depth study of wrong questions
    • Serious comments, plus 1 point for pointing out problems in the blog and code
    • 1 point for true and credible paired learning
  7. Deductions:
    • 0 points for plagiarism
    • Code cheating will be deducted to 0 points
    • 0 points for late work

Comments:

  • Worth learning or questions from the blog:

  • Worth learning or problems in the code:

Reviewed classmates blog and code

  • Pair study this week
    • 20172310
    • Pair learning content
      • Textbook Chapter 10
      • learning four operations
      • Discuss stack usage and programming
  • Last week's blog comments

Others (perception, thinking, etc., optional)

To be honest, I feel that this week’s tasks are a bit heavy, and it coincides with the May Day holiday. I have already planned a trip before, so when I learned that there are two blogs this week, I felt a little
irritable. When I encounter a problem, I feel a little anxious. When the teacher took the course of Chapter 10, my independent study was not completed. Fortunately, my teammates and I stepped up the pace later and completed
our task. Learning Java takes time, I want to better organize my time and study efficiently.

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 127/127 1/1 25/25
the second week 278/405 1/2 20/45
The third week 442/847 1/3 20/65
the fourth week 1063/1910 2/5 30/95
fifth week 840/2750 1/6 27/122
Week 6 631/3381 1/7 20/142
Week 7 914/4295 1/8 20/162
eighth week 2534/6829 2/10 30/192

References

Guess you like

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