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

Textbook learning content summary

  • Study Chapter 9 Inheritance this week.
  • Create a subclass:
  • Inheritance is the process of deriving a new class from an existing class.
  • One of the purposes of creating subclasses is to reuse existing software, avoiding code duplication and modification that can cause errors.
  • The original class from which the new class is derived is called the parent class, and the derived class is called the child class. Java uses the reserved word extend to indicate that a new class is derived from an existing class. Inheritance creates a "yes" relationship between parent and child classes.
  • protected modifier: protected visibility provides the greatest possible encapsulation allowing inheritance. All methods and variables will be inherited by subclasses, and memory space will be reserved for variables. However, constructors are not inherited.
  • super reference: super reference can call the constructor of the parent class. Can also be used to call superclass versions of overloaded methods in subclasses
  • Method overriding: When the subclass and the superclass have the same method name and signature, the subclass method will override the superclass method. A constructor is a special method that a subclass cannot override a constructor in its parent class. final methods cannot be overridden either.
  • Class Hierarchy: A subclass of a class can also be the parent class of one or more other classes, thus establishing a class hierarchy.
  • Inheritance is transitive and should reasonably keep the common features of a class as high as possible in the class hierarchy.
  • All Java classes are directly or indirectly derived from the object class.
  • Abstract class: Use the abstract modifier to see a class declared as abstract. Abstract classes cannot be instantiated. An abstract class represents a concept upon which subclasses will define methods. A subclass derived from an abstract class must override all abstract methods of the superclass, otherwise the subclass remains abstract.
  • Interface Hierarchy: The concept of inheritance can be applied to interfaces so that one interface is derived from another.
  • visibility
  • Restrictions on Inheritance: The final modifier can be used to restrict inheritance.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: The role of the protected modifier
  • Solution to Problem 1: A variable or method declared with protected visibility can be referenced by subclasses by name, while still maintaining some degree of encapsulation. protected visibility gives access to any class in the same package.
  • Question 2: Why does a subclass have to override the toString method in its parent class?
  • Solution to problem 2: Even if the method is not called by the subclass in the current program, the toString method of the parent class should be rewritten in the subclass to avoid problems in the future. Because in the future, when someone directly uses the subclass or inherits the subclass, he will think that the subclass already has a valid toString method.

Problems and solutions in code debugging

  • Question 1: In PP9.1, first of all, I have some confusion about "declaring a variable to store the denomination of a coin", how is the denomination of a coin defined, and how does it count as saving the denomination of a coin in a variable.
  • Solution to problem 1: After re-understanding the definition of Coin, I think the face value of the coin referred to here should refer to the current state of the coin--"When the coin is heads, use 0, and when the coin is tails, use 1 Express". Saving the denomination of a coin should mean declaring a variable, so that flipthe denomination of the coin obtained by the parent class through the method is facestored in the variable, so that the newly declared variable can be used in the subclass to refer to the value of the parent class. So my final definition is
  • Question 2: Calculate the sum of several MonetaryCoin objects required in the PP9.1 question?
  • Solution to problem 2: In the previous analysis, I have defaulted the coin value to represent the current state of the coin, and the sum does not know what it means. But only do it, the result is as shown in the figure
  • Question 3: As shown in the picture
  • Solution to problem 3: The declaration and definition statement of a variable cannot be run in the same sentence and should be modified toint sum = 0; sum += coins[x].getFacevalue();
  • Question 4: The solution that IDEA mentioned last week could not be uploaded. Blog link
  • Problem 4 solution:

code hosting

Summary of last week's exam mistakes


  • Mistake 1: An array index cannot be a float, double, boolean or String.
    A . true
    B . false
  • Resolution: Correct. Array indices must be of type int, or expandable to a value of type int (thus, char, byte, and short are also allowed).

  • Mistake 2: It is possible to sort an array of int, float, double or String, but not an array of an Object class such as a CD class. But you can't sort object classes (like CD classes.)
    A . true
    B . false
  • Parsing: An array of any type can be sorted as long as the type has some mechanism to compare two elements and determine their correct ordering (less than, equal, greater than). So if the CD class had a compareTo method, it would be possible to sort them.

  • Mistake 3: Although methods may be declared with a variable length parameter list, class constructors cannot.
  • Resolution: Constructors follow the same rules as regular methods, how their parameter lists are declared, so they can also support variable-length parameter lists.

other

The content of this week's textbooks is not particularly large, and the assignments are relatively easy. But there are actually quite a few definitions in this chapter, and for someone like me who is not at all sensitive to definitions, I actually need to be vigilant.

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 117/117 1/1 19/19
the second week 289/406 1/2 21/40
The third week 403/809 2/4 22/62
the fourth week 1783/2592 1/5 35/97
fifth week 770/3362 1/6 25/122
Week 6 734/4096 1/7 25/147
Week 7 687 / 4783 1/8 25/172

Guess you like

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