20172328 "Program Design and Data Structure" Week 7 Learning Summary

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

Chapter 9 Summary of Learning Content of Textbooks

  • 1. The concept of inheritance:
    • Inheritance is the establishment of a "yes" relationship between a parent class and a child class.
    • The original class from which a new class is derived is called a parent, superclass, or base class, and the derived class is called a subclass or subclass.
  • 2. Inheritance is unidirectional. Extends is used in java to indicate that a new class is derived from an existing class.
  • 3. protected modifier: When a variable is declared with protected visibility, subclasses can reference it, and the parent class maintains a certain degree of encapsulation.
    (In UML diagrams, protected visibility is represented by #.)
  • 4.Super reference, use super reference to access the members of the parent class. The subclass constructor is responsible for calling the superclass constructor, and should call the superclass constructor with a super() reference in the first line of the subclass constructor.
  • 5.Java's inheritance method is single inheritance, that is, a subclass can only have a single parent class. In java, you can rely on interfaces to achieve multiple inheritance,
  • 6. Overriding method: that is, when the subclass and the superclass have the same method and signature, the subclass method can override the superclass method, and the subclass method takes precedence. The object calling the method determines which version of the method will actually be executed.
  • 7. Shadow variable: If a variable with the same name is declared in a subclass, the variable is called a shadow variable.
  • 8. Class Hierarchy: Inheritance relationships usually develop into a class hierarchy. In a class hierarchy, care should be taken to keep the common features of the class at the highest possible level reasonably.
  • 9. All classes are directly or indirectly derived from the object class. The object class is defined in the java.lang package of the Java Standard Class Library.
  • 10. The final modifier can limit the ability of inheritance. Declare a method with the final modifier so that the method cannot be overridden in any derived class.

    Problems and Solving Processes in Teaching Materials Learning

  • Question 1: The encapsulation of variables declared as protected visibility is not as strict as private visibility, but stronger than public visibility.
  • 1.(〃'▽'〃) Answers to this question:

    • public: has the greatest access rights, and can access any class, interface, exception, etc. under the classpath. It is often used in external situations, that is, in the form of an external interface of an object or class.

    • protected: The main function is to protect subclasses. Its meaning is that subclasses can modify members with it, others cannot, it is equivalent to an inherited thing passed to subclasses

    • default: sometimes called friendly, it is designed for access to this package. Any class, interface, exception, etc. under this package can access each other, even if the parent class is not decorated with protected members.

    • Private: The access authority is limited to the inside of the class, which is a manifestation of encapsulation. For example, most member variables are decorated with private, and they do not want to be accessed by any other external classes.
  • Question 2: What does the derivation of a class represent?
  • 2. (〃'▽'〃) Answer to this question: Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can expand new capabilities. The definition of the existing class is used as the basis for the technology to create a new class. The definition of a new class can add new data or new functions, and can also use the functions of the parent class, but cannot selectively inherit the parent class.
  • Question 3: Difference between abstract class and interface?
  • 3. (〃'▽'〃) Answer to this question: abstract class and interface are two mechanisms for supporting abstract class definitions in the Java language. It is precisely because of the existence of these two mechanisms that Java has powerful object-oriented capabilities. . Abstract class and interface have great similarities in the support of abstract class definitions, and can even replace each other.

Neither abstract classes nor interfaces can be directly instantiated. To instantiate them, the abstract class variable must point to a subclass object that implements all abstract methods, and the interface variable must point to a class object that implements all interface methods. Abstract classes are inherited by subclasses, and interfaces are implemented by classes. Interfaces can only do method declarations, and abstract classes can do method declarations and method implementations. The variables defined in the interface can only be public static constants, and the variables in the abstract class are ordinary variables. All abstract methods in an abstract class must be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. Similarly, when a class implements an interface, if it cannot implement all the interface methods, then the class can only be an abstract class. Abstract methods can only be declared, not implemented. abstract void abc(); cannot be written as abstract void abc(){}. An abstract class can have no abstract methods. If a class has abstract methods, then the class can only be abstract. Abstract methods are to be implemented, so they cannot be static or private. Interfaces can inherit interfaces and can inherit multiple interfaces, but classes can only inherit unilaterally.

Problems and solutions in code debugging

  • Question 1: There is an error message when doing pp9.1, indicating that the int type cannot be applied, but in the parent class MonetaryCoin I set the face value type to be the int class, and after adding it up, it should be the int type, so it is very unreasonable. untie.
      
      
  • 1.(〃'▽'〃) Answer to this question: After discussing with my partner Guo Kai, he told me that there was an error in the initialization of faceValue in the parent class, and the object one array of CoinMonetary I created could not be directly added to equal the int variable Sum. Therefore, the error lies in not carefully distinguishing between objects and methods.

  • Question 2: When I was doing pp9.1, I wanted to use the random number coin tossing method filp in the parent class Coin and let it output sequentially. I thought of using for-each to do it, but it seemed that the idea was incorrect and I couldn't do it. come out.
  • 2. (〃'▽'〃) Answers to this question: The pairing group is super easy to use, so ask the boss for help. The problem is easily solved, and I also learned a little about the difference between for loop and for-each loop. for-each loop
    We discuss (I'm actually listening), used two methods to output the filp method of the Coin class, and output all of them.

code hosting

(screenshot of the running result of the statistics.sh script)

Summary of last week's exam mistakes

  • Question 1: 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
  • Problem 1 Analysis and comprehension: Option B, in Java, arrays are implemented as objects. A variable is a reference variable to a block of memory that stores the entire array.
  • Question 2: If an int array is passed as a parameter to a method, which of the following would adequately define the parameter list for the method header? parameter list?)
    A . (int[ ])
    B . (int a[ ])
    C . (int[ ] a)
    D . (int a)
    E . (a[ ])
  • Problem 2 Analysis and Insight: Choose C, the parameter is defined as the variable was originally declared as the type parameter name. Here, the type is int[] and the parameter is a. At that time, I thought that both B and C could declare arrays, and I directly excluded them without careful consideration.
  • Question 3: Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for
    firstEmpireBank = new BankAccount[1000]; (Assume that BankAccount is a predefined class, And the statement BankAccount[] firstrebank; has been executed. Then, the following instructions store the memory space.)
    A. a reference variable to the memory that stores all 1000 BankAccount entries (a reference variable to the memory that stores all 1000 BankAccount entries.) B . 1000
    reference variables, each of which point to a single BankAccount
    entry
    BankAccount entries)
    E. 1000 reference variables and 1000 BankAccount entries (1000 reference variables and 1000 BankAccount entries)
  • Problem 3 Analysis and comprehension: the first bank account; reserve memory space for the firstentity bank, which itself is a reference variable pointing to the BankAccount[] object. Instantiate the BankAccount[] object as 1000 BankAccount objects. The statement reserves memory space for 1000 reference variables. Note that there is not a single object in these 1000 banks.
  • Question 4: Given the following declarations, which of the following variables are arrays?
    int[]a, b;
    int c,d[];
    A . a
    B . a and b
    C . a and d
    D . a, b and d
    E . a, b, c and d
  • Question 4 Analysis and comprehension: choose D, the first statement declares that a and b are both int arrays. The second declaration declares that c and d are of type int, but d is an array of ints and c is a variable. ab and d are both int arrays.
  • Question 5: If a and b are both int arrays, then a = b; will (if a and b are both int arrays, then a = b; will)
    A. create an alias
    B. copy all elements of b into a (copy all elements of b into a)
    C . copy the 0th element of b into the 0th element of a (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 (if each corresponding element of b is equal to the corresponding element of a (i.e. a[0] = b[0], a[1] = b[1], etc.), return true, otherwise return false)
    E . return true if a and b are aliases and return false otherwise (return true if A and B are arrays, otherwise return false)
  • Problem 5 Analysis and comprehension: Choose A, "=" is an assignment operator. If both variables are primitives, the variable on the left is a copy of the variable on the right (if a and b are int values, and b = 5, then it will become 5). However, because a and b are array references the variable is set to reference variable b, causing both a and b to reference the same array in memory, or they are now aliases of each other.
  • Question 6: 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. (The Java main method uses parameters (String[] variable) so that the user can run the program and provide "command line" parameters. Since the parameter is an array of strings, the user does not have to provide any parameters.)
    A.True
    B. Flase
  • Problem 6 Analysis and Insight: The main method requires parameters in case the programmer wants to allow the user to provide command line parameters. Anything entered on the command line after the java command will be accepted as a command line argument. If it is several words separated by spaces, then each word is stored as a separate string array element.
  • Question 7: In Java, an array can only store one type of data. For instance, you cannot create an array that stores both double and String values. (In Java, an array can only store one type of data. For example For example, you can't combine float and string.)
  • Question 7 Analysis and comprehension: choose true, arrays are called isomorphic types. This means that the type of the value stored in the array must be the same as each element. The type is determined by the declaration. So int[] x just makes x an array of int values. Therefore, no array can store double and String at the same time.
  • Question 8: So long as one is only accessing the elements of an ArrayList, its efficiency is about the same as that of an array. It's only when one begins to insert or remove elements towards the front portion of an ArrayList that its efficiency deteriorates. (As long as you only access elements of the ArrayList, it's about as efficient as an array. It only gets less efficient when one starts inserting or removing elements in the front part of the ArrayList)
  • Question 8 Analysis and comprehension: Choose Ture, ArrayList is implemented as an array, as long as one of them only accesses the elements of ArrayList, the efficiency is the same as that of an array. However, when insertions or deletions are made to the front part of the ArrayList, a lot of element copying occurs, reducing its efficiency.

Pairing and mutual evaluation

-20172301
-20172303

Review Template:

Others (perception, thinking, etc., optional)

Inheritance is the content that appeared loomingly when we were studying Chapter 7 (7.4). At that time, we didn't think about it, feel it, and only understood the calling and aggregation relationship between classes at a simple level; in fact, it was forgotten quite quickly during the learning process. Yes, this week's experience is to look back when moving forward, not to let yourself lose too much, and to let yourself integrate new knowledge and old knowledge. In conclusion: I have nothing to offer but blood , toil, tears and sweat.

learning progress bar

        | 代码行数(新增/累积)| 博客量(新增/累积)|学习时间(新增/累积)|

| -------- | :----------------:|:----------------:|:- --------------: |
| Goals | 5000 lines | 30 articles | 400 hours |
| First week | 100/100 | 1/1 | 15/15 |
| Second week | 377/477 | 1/2 | 20/35 |
| Week 3 | 280/757 | 2/4 | 22/57 |
| Week 4 | 973/1730 | 1/5 | 35/92 |
| Week 5 | 1000/2730 | 1/6 | 40/132 |
| Week 6 | 729/3459 | 1/7 | 40/172 |
| Week 7 | |2/9|30/192|

References

Guess you like

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