20172314 2017-2018-2 "Program Design and Data Structure" Week 7 Learning Summary

20172314 2017-2018-2 "Program Design and Data Structure" Week 7 Learning Summary

Textbook learning content summary

  • Create a subclass:
    • Inheritance is the process of deriving a new class from an existing class by writing it in the declaration header of the subclass public class 子类名 extends 父类名;.
    • pay attention:
    • The instantiation of the subclass does not depend on the instantiation of the superclass.
    • Inheritance is unidirectional, the parent class cannot reference the variables and methods declared in the child class.
    • Constructors are not inherited.
  • protected modifier:
    • The public methods in the parent class can be accessed by name in the child class. If the child class wants to access its private methods, the parent class method must be declared as protected visibility, for exampleprotected int a=12;
    • Protected visibility provides the greatest possible encapsulation allowing inheritance.
  • super quote:
    • Used to call the constructor of the parent class.
    • When the subclass constructs the method, such as referring to the parent class, for examplepublic 子类名(int 父类构造方法名,int 子类构造方法名)
    • If such a call is not required, Java will automatically generate a line of super() calls at the beginning of the constructor. The operations referenced by super can only be performed in subclasses, and must be executed on the first line (the first line of the subclass constructor). ).
  • Multiple inheritance:
    • Some object-oriented languages ​​allow subclasses to have multiple superclasses. Java relies on interfaces to achieve this function.
  • Override method:
    • In inheritance, when the subclass and the superclass have the same method name and signature, the subclass method overrides the superclass method, the subclass takes precedence, and the object calling the method determines which version of the method will be actually executed (subclasses cannot write final method). For example, in the book parked.message(); dates.message();, different operations are performed, resulting in different results.
  • Shadow variable:
    • A variable with the same name as the parent class is declared in the subclass, which is called a shadow variable. Such declarations cause confusion and should be avoided.
  • Object class:
    • All classes in Java are derived from the Object class, and each class inherits the toString method and the equals method.
  • Abstract class:
    • Abstract classes usually have (not necessarily) undefined abstract methods that cannot be instantiated.
    • Every abstract method must use the abstract modifier.
    • A subclass derived from an abstract class must override all abstract methods of the superclass, otherwise the subclass remains abstract.
  • Visibility:
    • The private visibility member of the parent class will exist in the subclass, but it cannot be called directly by the member name, but can be called indirectly by calling a method that can call the private member.
  • Inherited restrictions:
    • Using the final modifier on a method declaration makes the method impossible to override in any derived class.
    • The final modifier can also be applied to an entire class, making that class unusable for deriving new classes.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: When referring to super in the textbook, it is mentioned as follows, but the Advice class in Example 9.9 is used at the endsuper.message();

    The operation of calling the superclass constructor with super reference can only be performed in the subclass, and must be performed on the first line.

  • Answer to the first question: When I saw Example 9.9, I had no doubts about the Advice class. I felt it was correct and understandable. But after seeing the concept of the super part by chance, I remembered this example, and put the super in the The first line has doubts. After I checked the reason why super put the first line of execution on the Internet, I found that my understanding was wrong. I mistakenly thought that as long as super is used, it must be put in the first line. In fact, the reason why super is placed in the first line is to ensure that the parent class object is initialized before initializing the current object. We also found related problems on the Internet . Pay special attention to this when calling the constructor of the parent class. , must be in the first line of the constructor of the subclass, and there is no such requirement for other methods of the parent class, they can be directly inherited. In Example 9.9, the constructor of the parent class is not called, just a message method, so it is not needed. Through this careless question, I have deepened my understanding of the usage of super.
  • Question 2: b, "subclasses can override the constructors of superclasses" in question SR9.9 after class, the reason for the error.
  • Answer to the second question: I think the reason for the error is that the constructor cannot be inherited at all, it can only be called, and there is no chance for modification. And the explanation of the answer is "Constructors have no return type, if you try to override the constructor of the parent class, you will get a syntax error, because all methods except constructors must have a return." Seems to be ok with my reasons.. . But at the same time, there is one more understanding. Since the understanding is a bit vague, there is a third question.
  • Question 3: The principle of the subclass calling the constructor of the superclass.
  • Answer to question three: refer to an example:

    class A{  
    public A(){} // 1:无参数构造方法。  
    public A(String s){} // 2.  
    }  
    
    class B extends A{  
    public B(String s){  
    super(s); // 3.  
    }  
    }  
    Note: If there is no parameterless constructor at 1, then 3 must actively call the constructor with parameters of the parent class. If there is 1 constructor, then 3 codes can be omitted, because Java will automatically call the parameterless constructor of the parent class by default. Remember one thing: when constructing a subclass, the constructor of the parent class must be called. So the parent class either has a default no-argument constructor so that Java will automatically call this no-argument constructor. If the parent class does not have a parameterless construction, then you need to call the parent class's construction by super() in the subclass's construction. Therefore, the subclass still cannot inherit the constructor of the parent class, but it can be called.
  • Question 4: Why can't the subclass inherit the constructor of the parent class?
  • Answer to question 4: Regarding the construction method, each class has its own construction method. Even if you do not declare it, the java virtual machine will help you build an implicit construction method, so the subclass must also have its own construction method. There is no need to inherit the constructor of the parent class. At the same time, the constructor of the parent class is used to construct the object of the parent class, and the object of the subclass needs to declare its own constructor object to create, so there is no need to inherit.
  • Question 5: What kind of existence is an abstract class?
  • Answer to Question 5: After consulting relevant materials, I found an explanation that is easiest to understand. As follows, so an abstract class can be regarded as an overall class and cannot be instantiated, so it is called an abstract class. Refer to How to vividly explain why abstract classes are used in java?

    You define an animal class that inherits a dog class and a cat class. You can new an instance of the dog class, but you cannot new an instance of the animal class, because there is no such instance of the animal class that does not belong to any subclass. So, the animal class must be abstract.

Problems and solutions in code debugging

  • Error 1: When running the code in IDEA, there is always a red wavy line indicating an error, but there is nothing wrong with the marked place.

  • Once the error is solved: This is a problem encountered when starting to use IDEA. Later, it was found that a "}" was added. In the later use, I almost know the characteristics of IDEA, and sometimes the wrong place it prompts is not. There must be something wrong there.
  • Question 2: This time I encountered two errors when I used the virtual machine to push.

  • Problem 2 Solved: The first error is because the previous push was not completed and there were residues, so when the new push was made again, two different submissions appeared. Finally, I deleted those few and it was fine. The second error found a similar problem on the Internet . The solutions mentioned are as follows. In fact, it is the same as the previous push failure, git pull origin masterbut git push origin masterthe description of the cause of the error is different.

Since
git does not submit code frequently, there will be a problem of version update
. add .git commint -m "merge with remote" and finally git push origin master




  • Question 3: When outputting a long piece of content, I want to use "\t" to separate, but the error is as shown in the figure

  • Problem 3 Solution: In the process of finding a solution, I learned that \t in java is an integer multiple of the current string length to 8, with a minimum of 1 and a maximum of 8 spaces. How much to fill depends on the length of the string before \t. For example, the length of the current string is 10, then the length after \t is 16, that is, 6 spaces are added. If the length of the current string is 12, the length after \t is 16. Fill in 4 spaces. For my problem, I didn't find a proper solution, so I replaced "\t" with spaces. The output is the same.
  • Question 4: The code is shown in the figure, I hope the output per is 120000/5, which is 24000, but the output is always 0.

  • Solution 4: I tried adding brackets, but it didn't work. The final solution is to remove per and add "pages/pictrue" directly to the println output.

code hosting
Problems and Solving Processes in Teaching Materials Learning

Summary of last week's exam mistakes

  • Error one:

  • One solution to the problem: In Java, arrays are objects. When I do this question, I first make sure that the array is an object, but I don't know if it is a primitive data type. But when he saw option D, he thought it made sense, and he was successfully persuaded. In fact, a variable is a reference variable to a block of memory that stores the entire array, and an array is not a variable.
  • Error two:

  • Analysis of wrong question 2: First, 1000 reference variables are generated, each of which is a BankAccount object, but after viewing the explanation of the entry, I still do not understand what the entry refers to. In this explanation, it should refer to variables. What is the name of the entry in the reference Java stream? emmmmm...incomprehensible.
  • Error three:

  • Analysis of wrong question 3: In the first one, a and b are both arrays. I think they can be understood as String a, b. But int a[ ] is equivalent to int [ ] a in the declaration of the array. And I chose it because I thought the most correct way to write it was a, so I chose one, and now I still don't understand the explanation, so just remember it first, there is no reason.
  • Error four:

  • Analysis of wrong question 4: "=" is an assignment operator. After a=b, since ab is an array, an alias is generated. If one of them is a definite number, it is assigned to the other.
  • Error five:

  • Analysis of wrong question 5: When I was doing this question, I thought that it was not necessary to provide parameters because I thought that there was no need to determine the parameters in the test code, but in fact the main method needs parameters, in case the programmer wants to allow users to provide command-line parameters. Anything entered on the command line after the java command will be accepted as a command line argument.
  • Error six:

  • Analysis of wrong question 6: This question was not carefully reviewed, and I didn't see it as "cannot", thinking that he said "can"...
  • Error seven:

  • Analysis of wrong question 7: When doing the question, I forgot that there is such a thing as a variable-length parameter list. The variable-length parameter list is used to specify a variable length by using an ellipsis (…).
  • Error eight:

  • Analysis of wrong question 8: When inserting or deleting the front part of ArrayList, a large number of elements are copied, which reduces its efficiency. When inserting or deleting here, not only one element is changed, but all the following ones must be changed, which should be paid special attention to.

    Pairing and mutual evaluation

    Comments:

  • Worth learning or problems in the blog: In the problem of code debugging, the process of writing PP9.3 is recorded in detail, and the UML diagram is also attached. It can be said that it is very attentive and bows to outstanding college students.

  • Worth learning or problems in the code: Tan Xin has a clear understanding of the code, has a rigorous conception, and has a good grasp of it. What I lack is a clear brain circuit.

  • Based on the scoring criteria, I rate this blog: 13 points. The scores are as follows:
    • Correct use of markdown syntax (1 point).
    • The template elements are complete (1 point).
    • Bonus points for questions (6 points).
    • Amount of code (1 point).
    • 1 point for beautiful layout
    • 1 point for code specification
    • 1 point for writing new code by hand
    • 1 point for code Commit Message specification

Reviewed classmates blog and code

  • Pair study this week
    • 20172305
    • Pair learning content
      • Experiment 3-5
      • pp9.1 and pp9.3

other

This week, I mainly learned inheritance, which involves the writing of classes. Since my grasp of writing classes in Chapter 7 is not very comprehensive, this week's study has improved my ability to write classes and has a more comprehensive understanding of a class. , and there is a little understanding of this week's experimental homework, and no detailed understanding.

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 93/93 1/1 20/20
the second week 305/398 1/2 20/38
The third week 328/651 2/4 25/60
the fourth week 1013/1689 1/5 30/90
fifth week 795/2407 1/6 30/120
Week 6 1117/2921 1/7 30/150
Week 7 703/3511 1/7 40/190

refer to:

Guess you like

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