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

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

Textbook learning content summary

The content of this week is mainly the content of 9.1-9.5 of the book

  • Chapter nine

1. The relationship between subclasses and parent classes, abstract classes

2. Reserved word super and visibility modifier protected

3. Class hierarchy and inheritance

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: reserved word super and visibility modifier protected
  • Solution to problem 1: Regarding the role of super, I have not understood what it means. What is the function of calling the constructor of the parent class? The parameters contained in the constructor of the parent class are not created in the constructor of the subclass. Is it possible? However, through the example of playing PP9.3, I found myself through the process of writing five subclasses such as ReadMaterial parent class and novel, and found that super is convenient in the actual writing process. For the same parameters as the parent class construction method, you can save To write a large part of the related methods, you can use inheritance, and calling the constructor in this way saves a lot of effort. To a large extent, the parent class method is refined, so that the subclass has a stronger pertinence. ==It is like the classification of the biological kingdom, which divides various organisms into domains, kingdoms, phyla, classes, orders, families, genus, and species, and refines them layer by layer. By inheriting the methods of refining the parent class and expanding the methods of the parent class, it becomes a subclass that inherits one by one. == The visibility modifier protected is a unique one from the public and private visibility modifiers.

    • Use super reference to call the constructor of the parent class.
    • When a variable or method is declared with protected visibility, subclasses can reference it, and the superclass maintains a certain degree of encapsulation. The encapsulation of protected visibility is not as strict as private visibility, but stronger than public visibility.
  • Question 2: Parent and child classes
  • Solution to problem 2: The relationship between parent class and child class is a more refined subclass implemented by inheritance, a kind of "extended" based on the parent class implementation, as I said in the previous question In that way, the subclasses are continuously "expanded" by means of inheritance one by one, so that the methods are continuously refined and kept close to the specific goals. By omitting common methods for parent and child classes, duplication can be avoided.

  • Question 3: Abstract Classes and Interfaces
  • Solution to Problem 3: The problem of abstract classes and interfaces is not well understood.

    • In object-oriented concepts, we know that all objects are represented by classes, but the reverse is not the case. Not all classes are used to describe objects, if a class does not contain enough information to describe a concrete object, such a class is an abstract class. Abstract classes are often used to represent the abstract concepts we get in the analysis and design of the problem domain. They are abstractions of a series of concrete concepts that look different but are essentially the same.
    • Difference: Abstract classes can contain non-abstract methods and constants and data.
    • Same thing: neither can be instantiated.

Problem 4: Class Hierarchy and Inheritance
Solution to Problem 4: The so-called class hierarchy is the continuous inheritance and development of classes, but the inheritance of classes is one-way, so the parent class is a simple, identical method a collection of . Continue to achieve concreteness through one subclass after another (finally realize the simplicity of object-oriented). The inheritance method of classes, in popular terms, is to extendsconnect the parent class and the subclass for the inheritance of the subclass (a simple word can write the content with common characteristics only once.)

Problems and solutions in code debugging

  • Question 1: PP9.1
  • Solution to Problem 1: The requirement of this programming project is to derive from the Coin class in Chapter 5, but for the understanding of the Coin class, it just returns the head or tail of the coin, but the requirement of this problem is to return the coin face value, so what exactly does "face value" mean? Refers to the heads and tails of the coin, or the value the coin represents. In the Q&A in the Blue Moyun class, Zhao Ganchen's question was judged to be positive and negative, but I prefer the face value returned, so after I inherited the parent class method, I modified the parent class's flip method and re-implemented it. Write a method to rewrite the original method in the parent class. At the same time as rewriting, I added the NumberFormat class to make its output face value with RMB symbol. (It may be somewhat contrary to the teacher's requirements, but it is still consistent in the general direction.)

  • Question 2: PP9.3
  • Solution to problem 2: For the writing of 9.3, I feel that there is no big problem, that is, to write a bunch of classes, and achieve the meaning of the question through inheritance. Therefore, it is necessary to write pseudo-code before writing:
    the common attributes of reading materials: the number of pages, the author (editor-in-chief), the type of the book, etc. Through UML modeling, a simple composition is carried out for representation. At the same time, I have carried out different method expansions and added parameters in the construction method for different subclasses, and made a more specific statement. Through the writing of these subclasses, I feel more about what object-oriented development means. Just like my junior high school math teacher said, "Math comes from life, and it is higher than life." Java language is a high-level language that serves human beings. , the meaning of existence is to facilitate the tedious things in life. Just like this topic, it feels like a part of the modification and rewriting on this basis can form an information management of the library series, and the series can be carried out through input and output. Organize and plan.

  • Check whether there are pictures or pinyin in the books subclass, and judge the suitable people to read this book
  • Add the parameter of the number of articles in the magazine in the magazine subclass
  • Add the publication scope and academic disciplines of academic journals to the definitions sub-category, and judge the influence, credibility, and value of the academic journal by whether the publication scope is national or global.
  • The novel subclass adds the author's nationality
  • The textbook subclass is used to count the units, study subjects suitable for the series, such as Chinese, mathematics, and nature.
  • PP9.3 in UML
  • Fiction
  • books
  • Magazine
  • Academic publications
  • textbook

code hosting

Summary of last week's exam mistakes

  • Mistake 1: 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
  • Error analysis: The array is initialized as = new type[x], where x is the size of the array The length stored in the array. However, the array is indexed from 0 to x - 1, so the off-by-one problem for the programmer is because the programmer will write code to try to access the index 1 to x, the so called bounds check is the length of the array versus the index The difference between the values ​​is checked by 1.

  • Wrong question 2: if a and b are both int arrays, then a = b; will (if a and b are both int arrays, a = b;then

    • A.create an alias (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
  • Error analysis: Regarding this question, I was a little confused when I was doing it, so I searched on Baidu and found the content of a blog for this question . Through a quick understanding of the blog, I think I should choose B, but But wrong. By parsing: "=" 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. It is the knowledge point of aliases in Chapter 3. Just like the legend shown in the book, they become aliases to each other, pointing to the same address and the same object.

  • Mistake 3: 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 supply "command line" parameters. Since the parameter is an array of strings, the user does not have to supply any parameters.)
    • A.true

    • B.false
  • Error parsing: The main method requires arguments in case the programmer wishes to allow the user to provide command line arguments. 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. For example, "java foo.class hi there" will store "hi" in variable[0] and "there" in variable[1] for use by the program.

  • 错题4: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.
    • A.true

    • B.false
  • Error analysis: ArrayList is implemented as an array, as long as one of them just accesses the elements of the 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.

  • The content of this chapter is relatively easy to understand, and the understanding of arrays is also relatively good. It is obviously easier than the last exam, but there are also a few questions that are wrong. Regarding the problem of error 4, I thought that the ArrayList class is stored in the form of an array, but I did not realize the problem of "efficiency", or I was only limited to the surface and did not explore the content in detail.

comprehension

第九章的内容是基于第四章和第七章继续讲解如何自创类、更简便的使用类,通过继承的方式,可以把相同特征的有关方法进行集中作为父类,但是通过本周的编写也发现自己在类的编写方面有很大不足,对于简单的可以很清晰编写成功,但是对于有些复杂的就不得不写写伪代码、照着书上的例子进行敲码。

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 136/136 1/1 15/15
the second week 570/706 1/2 20/35
The third week 613/1319 1/3 23/58
the fourth week 1249/2568 1/5 30/88
fifth week 904/3472 1/6 30/118
Week 6 904/3472 1/7 30/118
Week 7 1366/4838 1/8 32/150

References

Guess you like

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