Student ID 20172321 2017-2018-2 "Program Design and Data Structure" Week 7 Learning Summary

Student ID 20172321 2017-2018-2 "Program Design and Data Structure" Week 7 Learning Summary

Textbook learning content summary

  • create subclass
  • Inheritance establishes a "yes" relationship (reserved word extends) between the parent class and the child class, and inheritance is unidirectional.
  • protected visibility provides the greatest possible encapsulation that allows inheritance, but constructors do not.
  • Use super reference to call the constructor of the parent class. The operation of calling the constructor of the parent class by super reference can only be performed in the subclass, and it must be performed in the first line.
  • Java's inheritance method is single inheritance.
  • Override method
  • Subclass methods take precedence (a method defined by final modification cannot be overridden by subclasses)
  • Shadow Variables - A subclass can define a variable with the same name as the parent class, and redeclaration can change the variable type, but should generally be avoided.
  • class hierarchy
  • object class: All Java classes are directly or indirectly derived from the object class. (Every class in Java inherits the toString and equals methods)
  • Abstract class: An abstract class cannot be instantiated (a class containing one or more abstract methods must be declared abstract), and each abstract method must use the abstract modifier.
  • Interface Hierarchy: The concept of inheritance can be applied to interfaces so that one interface is derived from another.
  • The inheritance of classes and the inheritance of interfaces cannot overlap, that is, interfaces cannot be used to derive new classes, and classes cannot be used to derive interfaces.
  • Only when a class is designed to implement an interface, there is interaction between the implementing class and the interface.
  • visibility
  • The private members of the parent class are also inherited by the subclass. Although these private members cannot be directly accessed by member names, they can be accessed indirectly.
  • Design of inheritance relationship between classes
  • In software design, the staggered structure of inheritance relationship between classes must be carefully studied and designed.
  • Final methods are often used to ensure that the method can be used in all subclasses.
  • The final modifier can also be applied to the entire class. A final class can no longer be used to derive new classes.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: The meaning and usage of super reference
  • Problem 1 solution:
  • super appears in subclasses that inherit the parent class, and there are three ways of existence:

The first method
super.xxx(xxx is the variable name or object name)
means to obtain the variable or method reference named xxx in the parent class.
Using this method, you can directly access variables or objects in the parent class, and perform operations such as modification and assignment.

The second
super.xxx() (xxx is the method name)
means that the method in the parent class is directly accessed and called.

The third
super()meaning of this method is that calling the initialization method of the parent class is actually calling the public xxx()method in the parent class;

  • Question 2: Abstract Classes and Interfaces
  • Problem 2 solution:
  • If a class does not have enough information to describe a concrete object and needs other concrete classes to support it, then such a class is called abstract class. For example, new Animal(), we all know that this is to generate an animal Animal object, but we don’t know what this Animal looks like. It does not have a concept of a specific animal, so it is an abstract class and needs a specific animal. , such as dogs and cats to describe it in a specific way, we only know what it looks like.
  • An interface is a more abstract "class" than an abstract class. I can't find a better word to put "class" in quotes here, but let's be clear that an interface is not a class itself, as we can see from our inability to instantiate an interface. Such as new Runnable(); is definitely wrong, we can only new its implementation class.

  • Question 3: Differences between several visibility modifiers (public, default, private, protected)
  • Problem 3 solution:

Problems and solutions in code debugging

  • Question 1: The sum of the coin toss is 0000, which is very strange, why it happens to be all 0, and this is not a sum.

  • Solution to problem 1: There is a problem when setting the coin tossing in the parent class, resulting in a 100% probability of tossing to 0, and then changing System.out.println("四个硬币的和为" + myCoin1.getF() + myCoin2.getF() + myCoin3.getF() + myCoin4.getF())to It System.out.println("四个硬币的和为" + (myCoin1.getF() + myCoin2.getF() + myCoin3.getF() + myCoin4.getF()))turns the concatenation of strings into addition.
  • Question 2: When making pp8.3, the first few subclasses have no problem, but compared with the parent class, they are all increased String variables. The last one I want to add an int variable, and then there is a problem super(xxx)when there is a problem .

  • Solution to problem 2: After finding several errors, there is still a problem. Finally, I changed super()the order of several in it and it was successful.

code hosting

Summary of last week's exam mistakes

  • Wrong question 1 and reasons, understand the situation
    In Java, arrays are
    A . primitive data types
    B . objects
    C . interfaces
    D . primitive data types if the type stored in the array is a primitive data type and objects if the type stored in the array is an object
    E. Strings
    Correct answer: B Your answer: D
    In java, arrays are implemented as objects. A variable is a reference variable to a block of memory that stores the entire array. However, arrays are accessed using the symbolic name [index], not via message passing.

  • Error question 2 and reasons, understand the situation
    The "off-by-one" error associated with arrays arises because
    A . the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far
    B . the last array index is at length + 1 and loops may only iterate to length, missing one
    C . the last array element ends at length - 1 and loops may go one too far
    D . programmers write a loop that goes from 0 to length - 1 whereas the array actually goes from 1 to length
    E . none of the above, the "off-by-one" error has nothing to do with arrays
    Correct answer: A Your answer: B
    The array is initialized to [x], where X is the size of the array. However, the legal indices for arrays are 0 to X-1, so programmers often off-by-one because programmers would write code to try to access indices 1 to X.

  • Wrong question 3 and reasons, understand the situation
    If a and b are both int arrays, then a = b; will
    A . create an alias
    B . copy all elements of b into a
    C . copy the 0th element of b into the 0th element of a
    D . return true if each corresponding element of b is equal to each corresponding element of a (that is, a[0] is equal to b[0], a[1] is equal to b[1] and so forth) and return false otherwise
    E . return true if a and b are aliases and return false otherwise
    Correct Answer: A Your Answer: B
    "=" is an assignment operator. If the two variables are primitives, you get a copy of the right variable than the left variable (so if a and B are INT values ​​and B=5, then a becomes 5). However, since a and B are arrays, the reference variable a is set to reference the variable B, causing both a and B to reference the same array in memory at the same time, or they are now aliases to each other.

  • Wrong question 4 and reasons, understand the situation
    A Java main method uses the parameter (String[ ] variable) so that a user can run the program and supply "command-line" parameters. Since the parameter is a String array, however, the user does not have to supply any parameters.
    A . true
    B . false
    Correct Answer: A Your answer: B
    When the programmer wants to allow the user to supply command line parameters, the main method requires parameters. Anything entered on the command line after the Java command will be accepted as a command line argument. If it's several words separated by spaces, then each word will be stored as a separate string array element. For example, "Java foo.class Hi there" stores "Hi" in variable[0] and "there" in variable[1] for possible use by the program.

  • Wrong question 5 and why, understand the situation
    Although methods may be declared with a variable length parameter list, class constructors cannot.
    A . true
    B . false
    Correct answer: B Your answer: A
    constructors follow the same rules as usual methods, they How the parameter lists are declared, so they can also support variable-length parameter lists.

Pairing and mutual evaluation

Review Template:

  • Worth learning or questions from the blog:
    • xxx
    • xxx
    • ...
  • Worth learning or problems in the code:
    • xxx
    • xxx
    • ...
  • Based on the scoring criteria, I give this blog a score: XX points. The scores are as follows: xxx

  • Reference example

Reviewed classmates blog and code

Others (perception, thinking, etc., optional)

Difficult, really difficult, life is really hard, but learning Java is a long way to go. I like learning Java the most, I like Java the most, I like it the most, I like it the most.

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 189/189 1/1 18/18
the second week 250/439 2/3 21/39
The third week 437/876 2/5 25/64
the fourth week 459/1335 2/7 21/85
fifth week 547/1882 1/8 20/115
Week 6 592/2474 2/10 25/150
Week 7 782/3256 1/11 29/179

References

Guess you like

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