Student number 20172328 "Program Design and Data Structure" Week 8 Learning Summary

Student number 20172328 "Program Design and Data Structure" eighth week study summary

Textbook learning content summary

  • 1. Binding: A request event may be generated at a certain moment of program execution, requiring the execution of a certain piece of code to complete a method call. This request event is called the binding of a method call and a method definition.
  • Late binding: For polymorphic references, this binding is delayed until the program runs, and the method to be bound depends on the compilation phase. For polymorphic references, this binding is delayed until the program runs, and the method to be bound depends on the object referenced by the reference variable at that time. This delayed request event is called post binding or dynamic binding.
  • 2. Polymorphic references can point to different types of objects over time. For polymorphic references, the binding of the method call to the method definition code is performed at runtime.
  • In Java, polymorphic references can be established in two ways: 继承方式and 接口方式.
  • 3. Use inheritance to achieve polymorphism:
    • When a reference variable is declared with a class name, the variable can point to any object of that class, or it can refer to an object of any class related to its declared type by inheritance.
    • And this relationship is valid for the entire class hierarchy. For example: Animal creature = new Horse(Animal is a parent class of Mammel, Horse is a subclass of Mammel)
  • 4. Use interfaces to achieve polymorphism:
  • Class names can be used to declare object reference variables, and interface names can also be used to declare the type of object reference variables.
  • An interface reference variable can point to any object that implements the interface class.
  • Please Remember: When using an interface reference variable, you can only call the method defined in the interface, even if the object pointed to by the interface reference variable has some other available methods, the call cannot be achieved.
  • Interface names can be used as method parameters, so that any class object that implements the same interface can be passed as a parameter to the method. Method parameters can be made polymorphic, allowing flexibility in the parameters the method accepts.
  • 5. Sorting: Sorting is the process of adjusting a set of elements into an ordered arrangement.
  • Selection sort method: first retrieve all elements, find the smallest and the first and change the position, continue to find the second and the second element to change the position~~~ Then it is arranged in an ascending order sequence from small to large. In the same way, you can also discharge the descending sequence from large to small.

    int min;     
    Comparable temp;
    
     for(int index = 0;index  < list.length-1 ; index ++)
     {
        min = index;
        for(int scan = index + 1; scan  < list.length ; scan ++)
        if(list[scan].compareTo(list[min])<0)
          min = scan ;
    
        temp = list[min];
        list[min] = list[index];
        list[index] = temp;
        }   
  • Insertion sorting: sort the first two elements of the sequence, then insert the third element and then sort... Then it is arranged in an ascending ordered sequence from small to large. Similarly, it can also be arranged in descending sequence from large to small.

      for(int index = 1;index < list.length;index++)
      {
          Comparable key = list[index];
          int  position = index;
    
          while(position > 0 && key.compareTo(list[position-1]) < 0)
          {
          list[position] = list[position -1];
          position--;
          }
    
         list[position] = key;
      }
  • The comparison of the efficiency of the two sorting algorithms
    is the basic criterion for comparing sorting algorithms. Select insertion and insertion sort have practically the same efficiency. Both algorithms perform roughly n² comparison operations (where n is the number of values ​​in the sequence). The selection sort method is easy to understand, and in most cases, it can move to its own position accurately at one time, so the selection sort method is better than the insertion sort method.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: Object? reference variable? ? Object reference variable? ? ?
  • 1.(〃'▽'〃) Answers to this question: I couldn't figure out what this was during the process of reading the book at that time. Confused and confused, I didn't understand much after reading the book twice. Then just look for a detailed explanation.

    Vehicle veh1;

veh1 = new Vehicle();

The effect is the same. In this way, it is relatively clear that there are two entities: one is the object reference variable, and the other is the object itself.

在堆空间里创建的实体,与在数据段以及栈空间里创建的实体不同。尽管它们也是确确实实存在的实体,但是,我们看不见,也摸不着。不仅如此,

我们仔细研究一下第二句,找找刚创建的对象叫什么名字?有人说,它叫“Vehicle”。不对,“Vehicle”是类(对象的创建模板)的名字。

For details, click here

  • Question 2: Can an interface implement an interface? Can an interface inherit an interface? What is the relationship between the two?
  • 2.(〃'▽'〃) Answer to this question: I got the solution to the problem through group discussion, and I also searched online.
  • An interface can inherit an interface, and can inherit more than one interface, but cannot implement an interface. Because the member methods of the interface all have abstract properties and do not have a method body, the inherited interface cannot be implemented.

  • My own understanding: An interface is a collection of constants and abstract methods. Implementing an interface means making the abstract methods in the interface concrete, and inheriting an interface means inheriting the methods in the interface without making them concrete. Therefore, an interface cannot implement an interface while an interface can inherit an interface .
  • Question 3: The answers to SR10.5 in the textbook cannot be understood. Assuming that the MusicPlayer class is the parent class of the CDPlayer class, determine whether the following statements are legal.

    MusicPlayer mplayer = new MusicPlayer();
    CDplayer cdplayer = new CDPlayer();
    mplayer = cdplayer;
    The answer is: the third statement is illegal, a MusicPlayer is not necessarily a CDPlayer. It is illegal to assign a value directly without ++ first using cast to cast ++.
  • 3. (〃'▽'〃) Answer to this question: The inheritance of the parent class by the subclass is an extension of the service interface provided by the parent class, that is to say, the interface provided by the parent class is a subset of the interface provided by the subclass, so When it is said that a simple parent class object cannot be transformed into a subclass object.
    java object type conversion
  • Question 4: I don't understand the questions c and d of SR10.11 in the textbook. (The topic is too long to play well. If you want to know more about the 322 pages of the book, please read it ヾ(◍°∇°◍)ノ゙)
  • 4. (〃'▽'〃) Answer to this question: The reference variable of the interface can be assigned by the implementation interface object, which is equivalent to assigning the address to it.
    ❀Expansion: In inheritance, objects of two subclasses that inherit the parent class can also be assigned to each other, and the essence is also to change the object address.

    Problems and solutions in code debugging

  • Question 1: When I was working on pp10.1, I encountered a stuck place. I created a payable interface, and then I didn't know what method to put in the interface. After thinking for a long time, I wrote a payable abstract method without return value, but Can't succeed and can't output something specific and valid.
  • 1.(〃'▽'〃) Answer to this question: I still asked my classmates and changed the method in the Payable interface directly to payday(), and then I can output it. After thinking more about it, I think it is only because I have to implement it after inheriting the interface. My own problem turned out to be here. It's actually quite simple.
  • Question 2:
  • 2. (〃'▽'〃) Answers to this question:
  • Question 3:
  • 3.(〃'▽'〃) Answers to this question:

code hosting

This is the first time to use IDEA for code amount statistics
(screenshot of the running result of the statistics.sh script)

Summary of last week's exam mistakes


  • Question 1: If a programmer writes a class wanting it to be extended by another programmer, then this programmer must . change private methods and instance data to be protected (change private methods and instance data to protected type)
    B . change public methods and instance data to be protected (change public method instance data to protected type)
    C . change all methods to be protected (change all methods to protected type)
    D . change the class to be protected (change the class to protected type)
    E . none of the above, the programmer does not have to change anything (there is no correct answer to the above, this program members do not need to change anything)
  • Question 1 Analysis and comprehension: Correct answer A. I chose E at the time. Sections declared protected can be accessed by any subclass of the class in which they are defined, while sections declared private cannot be accessed by any other class. Therefore, previously defined private items must be protected. Previously defined public items should remain so that all other classes can still access these public items. The previously defined private should not be public as this would allow all classes to access them, not just subclasses.
  • Question 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 All classes must have 1 child (derived or extended) class but may have any number of parent
    classes (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 (all classes have 1 or 0 parent classes and an infinite number of children)
  • Question 2 Analysis and comprehension: The correct answer is A, and I chose E at that time. It is only when the teacher emphasizes it in class that he has a deeper understanding of this issue. The object class is the parent class of all classes, that is, all classes are derived from the object class. So the answer is A.
  • Question 3: 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 . cloneable
    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
  • Question 3 Analysis and comprehension: The correct answer is D, and I chose A at the time. , I hadn't learned about polymorphism at that time, and I decided to start when I saw the keyword protected in the chapter on inheritance. In fact, polymorphism means that variables can exist in many forms. In general, Java is mandated that a variable, once declared of a certain type, can never be changed to a different type. The only exception is that polymorphic variables can be derived classes of any type (although not at the same time, variables can also be converted from one type to another).
  • Question 4: Using the reserved word, super, one can
    A. access a parent class'constructor(s)
    B. access a parent class'methods and instance data( access parent class methods and instance data)
    C . access a child class'constructor(s) (access child class constructor)
    D. access a child class'methods and instance data (access child class methods and instance data)
    E. none of the above (no correct options)
  • Question 4 Analysis and comprehension: The correct answer is E. I chose A at the time. The key point of this question is that the answers of A and B are both correct, so the two answers should be combined. At that time, I only read one answer and finished it carelessly, so I will pay attention to it in the future.
  • Question 5: Why shouldn't an abstract method be declared final? (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. So long as the Abstract method is declared in a Class (not an Interface), there's nothing wrong with doing this (There is nothing wrong with doing this as long as the abstract method is declared in the class (not the interface))
    E . None of the above (no correct option)
  • Question 5 Analysis and comprehension: correct answer B, my choice E at the time,I don't know why I chose E. To make an abstract method a concrete method, it must be overridden. Declaring a method final makes it impossible to override it. Therefore not.
  • Question 6: Interface classes cannot be extended but classes that implement interfaces can be extended.
    A . true
    B . false
  • Question 6 Analysis and comprehension: The correct answer is B, and I chose A at that time, which is not clear about the concept. Any class can be extended, whether it is an interface, implements an interface, or neither. The only exception is if the class is explicitly modified with the word "final", in which case it cannot be extended.

This time, the translation in the correction of the wrong question is to check the translation by yourself! !

Pairing and mutual evaluation

-20172301
-20172304

Review Template:

  • Worth learning or questions from the blog:
    • 20172301
    • 20172304
  • Worth learning or problems in the code:

    • Reviewed classmates blog and code

  • Pair study this week
    • Paired student number 1
    • Pair learning content
      • Discussion on Infix to Suffix in Pair Programming
      • discussion of textbook exercises
      • Discussion on whether an interface can implement an interface

Others (perception, thinking, etc., optional)

Playing on May 1st brought me a lot of joy and leisure, but also brought me a lot of psychological pressure. When I pick up the textbook again and turn on the computer, I suddenly feel at a loss, probably without a sense of belonging.
---------------------------------
The learning progress of the course never stops, and there is no time for me to take it slow. I hope that taking a deep breath, I can start again and take it seriously. The exhaustion of the soul makes the journey difficult, and the firmness of the heart keeps me going.

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 100/100 1/1 15/15
the second week 377/477 1/2 20/35
The third week 280/757 2/4 22/57
the fourth week 973/1730 1/5 35/92
fifth week 1000/2730 1/6 40/132
Week 6 729 /3459 1/7 40/172
Week 7 660/4119 2/9 30/192
eighth week 1417/5536 3/12 30/222

References

Guess you like

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