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

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

Textbook learning content summary

Chapter 10 Polymorphism
1. Post binding
1. The type of the reference variable and the object pointed to by the reference variable must be compatible, but not necessarily the same;
2. Polymorphic references can point to different types of objects over time;
3. For polymorphic references, post-binding cannot be executed until the program is running;

4. The efficiency of post-binding is lower than the binding efficiency of the compilation phase.

2. Polymorphism is achieved by inheritance
1. A reference variable can point to any object of any class in the inheritance relationship;
2. The version of the method that will actually be invoked depends on the type of the object rather than the type of the reference variable;

3. Implementing polymorphism using interfaces
1. Interface names can be used to declare object reference variables;
2. An interface reference variable can point to any object of any class that implements the interface;

4. Sorting
1. Selection sorting method;
2. Insertion sorting method;
3. Comparison: The efficiency of selection sorting method and insertion method is the same, but the number of exchange operations performed by selection method is less.

5. Search
1. Linear search;
2. Binary search;
3. Comparison: Binary search is more efficient than linear search, but binary search requires that the data has been sorted.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: In the binding mentioned in the first section, it is said: "The efficiency of compile-time binding is higher than that of dynamic binding", why?
  • Solution to problem 1:
    First of all, my idea at the beginning was that because the binding at compile time is called static binding and early binding, I searched for relevant information, among which, there is such a saying when it comes to static binding :

This explains where static methods are useful, and also clarified a concept for me, because static binding happens before the code is executed, so this binding does not delay the execution time of the program.

  • Question 2: Is the scope of dynamic binding only objects, or can it be class methods or the like?
  • Solution to problem 2:
    Let's use inheritance to implement polymorphism to do an experiment:

    public class Father {
    protected String name = "父亲属性";
    }
      public class Son extends Father {
    protected String name = "儿子属性";
    public static void main(String[] args) {
        Father sample = new Son();
        System.out.println("调用的属性:" + sample.name);
    }
    }

    The final conclusion is that the called member is the property of the parent.

Now what if I try to call the member variable name of the subclass?

public class Father {
    protected String name = "父亲属性";

    public String getName() {
        return name;
    }
}  
public class Son extends Father {
    protected String name = "儿子属性";
    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        Father sample = new Son();
        System.out.println("调用的属性:" + sample.getName());
    }
}

It is to encapsulate the member variable into a method getter form.

  • Question 3: What does rewriting have to do with polymorphism?
  • Solution to problem 3: Because in the discussion of a problem, I saw such a problem: Why do you say that there is a method to rewrite the premise of polymorphism? In fact, on the one hand, when you see this problem, you realize the premise It is a relationship between the two of them. At the same time, I wanted to ask this question myself, so I continued to read:
    In that discussion, many bigwigs used airplanes as an example, and one of them said:

But this is to understand the polymorphism in inheritance, but as a whole, there is still no specific detail to say why?
Another person talks about the concept and role of polymorphism:

The book says this:

When a subclass overrides the definition of a method in its husband class, both versions of the method actually exist. If the method is called with a polymorphic reference, then the version of the called method is determined by the type of the object on which the method call is performed, not by the type of the reference variable.

Problems and solutions in code debugging

  • Question 1: In the practice of pp10.1, I didn't understand how to compile it at first, so I compiled an interface first, and then put the void method in Staff into the interface, and learned the examples in the book in the main driver. Declare an object with an interface name, after instantiating it, an error is displayed?


  • Solution to problem 1: I ALT and press Enter, thinking about declaring Staff as an interface class, try it, and it will work, but it doesn't seem to meet the requirements of the title, so I feel that I am still wrong, but it can be output.

code hosting

Summary of last week's exam mistakes

错题1
Inheritance through an extended (derived) class supports which of the following concepts?
A. interfaces
B. modular
C. information hiding
D. code reuse
E. correctness

    正确答案: D 我的答案: A 

Resolution: By extending a class and inheriting it, the new class doesn't have to reimplement any of these inherited methods or instance data, saving the programmer's workload. So code reuse is the benefit of reusing other code for extending it for your needs. False 2
Aside from permitting inheritance, the visibility modifier protected is also used to
A. permit access to the protected item by any class defined in the same package
B. permit access to the protected item by any static class
C. permit access to the protected item by any parent class
D. ensure that the class cannot throw a NullPointerException
E. define abstract elements of an interface

    正确答案: A 我的答案: B 

Resolution: The protected visibility modifier is used to control access to an item in a protected manner. Protection is access limited to the current class (like a private project), a class in the same package, or an extended class of this class.
False Question 3
Which of the following is an example of multiple inheritance?
A. A computer can be a mainframe or a PC
B. A PC can be a desktop or a laptop
C. A laptop is both a PC and a portable device
D. A portable device is a lightweight device
E. Macintosh and IBM PC are both types of PCs

    正确答案: C 我的答案: E 

Resolution: Multiple inheritance means that a given class inherits from multiple parent classes. Of those listed above, laptops inherit features from PCs and portable devices. The answers in A, B, and E are all examples of single inheritance, where a class has at least two children (in A, computers have children mainframes and PCs, B, PCs have children desktops and laptops, E, PCs Has kids Macintosh and IBM PC). Answer D represents an attribute of a class.
False 4
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) or extended) classes
E. All classes can have either 0 or 1 parent class or any number of children (derived or extended) classes

    正确答案: A 我的答案: E 

Analysis: Java supports inheritance, but not multiple inheritance, so a Java class can have any number of child elements, but only one parent element. Furthermore, since all Java classes inherit directly or indirectly from the Object class, all Java classes have only one parent class.
False 5
A variable declared to be of one class can later reference an extended class of that class. This variable is known as
A. protected
B. derivable
C. closeable
D. 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

    正确答案: D 我的答案: A 

Analysis: Polymorphism means that a variable can have multiple forms. In the ordinary case, Java is strongly defined that a variable once declared of a certain type can never be changed to a different type. This is an exception, a polymorphic variable can be a derived class of any type (although not at the same time, a variable can change from one type to another).
Mistake 6
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

    正确答案: E 我的答案: B 

Resolution: The super reserved word provides a mechanism to access the superclass's methods and instance data (whether they are hidden or not). In addition, you can use super to access the superclass's constructor. So the correct answer is a combination of A and B, which is not an option, so the correct answer is E.
Wrong question 7
Why shouldn't an abstract method be declared final?
A. There's nothing wrong with doing so
B. Abstract methods cannot be overridden and they must be if a concrete class ever is to be instantiated
C. So long as the Abstract method never actually is used in by any other method, there's no problem with doing this
D. As long as the Abstract method is declared in a Class (not an Interface), there's nothing wrong with doing this
E. None of the above

    正确答案: B 我的答案: E 

Resolution: In order to make an abstract method concrete, it must be overridden. Declaring a method is final, so it is impossible to override it. This is a contradiction and is forbidden.
False Question 8
If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but cannot redefine x to be a different type.
A. true
B. false

    正确答案: B 我的答案: A 

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, just like in super.x.
False Question 9
The reserved word, extends, is used to denote a has-a relationship.
A. true
B. false

    正确答案: B 我的答案: A 

Resolution: Extends is for inheritance...inheritance is an is-a relationship, not a has-a relationship.
Mistake 10
Although classes can be inherited from one-another, even abstract classes, interfaces cannot be inherited.
A. true
B. false

    正确答案: B 我的答案: A 

Resolution: An interface has all the inherited properties that a normal class has. So you can create an Interface inheritance hierarchy just like you can create a Class inheritance hierarchy. However, what you can't do is instantiate the interfaces they must implement.

Pairing and mutual evaluation

  • Pair study this week
  • Worth learning or questions from the blog:
    • The content is detailed and appropriate;
    • The code debugging link is more detailed;
  • Based on the scoring criteria, I rate this blog: 4 points. The scores are as follows:
  1. Correct use of Markdown syntax (plus 1 point):
  2. The elements in the template are complete (plus 1 point)
  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

  • Worth learning or questions from the blog:
    • The content is detailed and appropriate;
    • The code debugging link is more detailed;
  • Based on the scoring criteria, I rate this blog: 4 points. The scores are as follows:
  1. Correct use of Markdown syntax (plus 1 point):
  2. The elements in the template are complete (plus 1 point)
  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

other

  • This week should be the most suitable week for happiness and pain. On May 1st, I always divide the time between study and rest, but sometimes I want to rest too much and always fail to devote myself to study, making myself lazy, and finally Letting ourselves play is not good enough, and we are not good at learning, so I think rational management of time will make us live more fully.
  • Time passed quickly, seeing that most of the semester was coming to an end, and the Java course became a bit difficult. After all, my foundation was not solid, and I always learned a little and forgot a little, and cheerleading training took up most of the evening. The time is a lot different from the time when others study. I feel that I haven’t stayed up late because of playing on my mobile phone. Every night I knock on my computer and the power is out. I can’t help but look at other people’s computers and have power. I can only read a book for a while and sleep. Now The competition in the class is getting stronger and stronger, I hope everyone can do their best, their body is the main thing, come on!

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated)
Target 5000 lines 30 articles 400 hours
the first week 156/156 1/1 15/15
the second week 217/371 1/2 20/35
The third week 233/604 2/4 20/55
the fourth week 1382/1986 1/5 35/90
fifth week 146/2196 1/6 25/115
Week 6 462/2658 1/7 15/130
Week 7 856/3514 1/8 20/150
eighth week 1877/5391 3/11 20/170

References

Are Java Static Binding and Dynamic Binding Method
Rewriting a Necessary Condition for Polymorphism?

Guess you like

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