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

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

Textbook learning content summary

- create subclasses:


1. Writing a class is to define a group of similar objects; a class establishes the characteristics and behavior of the object, but does not reserve memory space for declared variables (unless the declared variables are static); a class is a design, and Objects are concrete implementations of the design.

2. Inheritance is the process of deriving a new class from an existing class. The purpose is to reuse the existing software and establish a "yes" relationship between the "subclass" and the "parent class".

3. The instantiation of the subclass does not depend on the instantiation of the parent class. Inheritance is monomistic, the parent class cannot reference the variables and methods declared inside the child class.

4. The public methods or variables of the parent class can be accessed by name in the subclass, or through the subclass object; while the parent's private methods or variables cannot be accessed in the subclass, or through the subclass object.

5. When a variable or method is declared as protected visibility, the subclass can refer to it, and the parent class maintains a certain degree of encapsulation.

6. Visibility: protected > public
Encapsulation: protected <font color="#dd0000"><private

7. Constructors cannot be inherited!

8. One of the uses of super reference is to call the parent class method. In general, the first line of the constructor should call the superclass constructor with a super reference. If no such call exists, Java automatically produces a line of super() calls at the beginning of the constructor. This approach ensures that the parent class constructor will initialize its own variables before the child class constructor executes. The operation of calling the constructor of the super class with a super reference can only be performed in the subclass, and must be performed on the first line .


9. Java's inheritance method is called single inheritance, that is, a subclass can only have a single parent class. Some object-oriented languages ​​allow subclasses to have multiple superclasses, an approach called multiple inheritance, which is useful for objects that need two class descriptions. Relying on interfaces in Java provides the best features of multiple inheritance without increasing ambiguity.

- Override method:


1. When the subclass and the superclass have the same method and signature, the subclass method will override the superclass method, and the subclass method will take precedence. Overriding requirements often occur in the context of inheritance.

2. A method can be defined with the final modifier, and subclasses will not be able to override the final method.

3. A subclass can define a variable with the same name as the parent class. Attention should be paid to the difference between redeclaring a variable and assigning a value to an inherited variable. If a variable of the same name is declared in a subclass, the variable is called a shadow variable .

- Class Hierarchy:

1. A subclass derived from a parent class can also be the parent class of its own subclass, and multiple subclasses can be derived from a parent class. Therefore, the inheritance relationship often develops into a class hierarchy.

2. Two subclasses of the same parent class become siblings. Although sibling classes share characteristics inherited from their common parent class, there is no inheritance relationship between the two because one class is not derived from the other.

3. The common features of the class should be kept at the highest possible class level reasonably.

4. In Java, all classes are derived from the Object class in the final analysis. If the definition of a class does not use the extends clause to explicitly derive itself from another class, it will be treated as the default, automatically derived from the Object class.

5.Object class is defined in the java.lang package of the Java standard class library.

6. Abstract classes usually contain one or more abstract methods that are not yet defined (no implementation code) and cannot be instantiated. A class that contains one or more abstract methods must be declared abstract. Abstract classes act as placeholders in a class hierarchy.

7. A subclass derived from an abstract class must override all abstract methods of the parent class, otherwise the subclass is still an abstract class.

8. 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:

1. 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:

1. The staggered structure of the inheritance relationship between classes must be carefully studied and designed in software design.

2. 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: What are the permission modifiers and what are their functions?
  • Problem 1 solution:


1: What is a permission modifier, what does it do, and why is it needed;

a Java application has many classes, but some classes do not want to be used by other classes. There are data members and method members in every class, but not every data and method are allowed to be called in other classes. How to do access control? You need to use the access modifier.

1. The permission modifier is used to control the visible scope of the modified variable, method, and class. That is, its scope of action;


2: There are four kinds of permission modifiers in java:

1. The public type is public;

2. Public can modify classes, member variables, constructors, and method members.

3. Members modified by public can be called in any class, regardless of the same package or different packages,

4. It is the most privileged modifier

1. The private type is private;
2. Member variables, constructors, and member methods can be modified, but classes cannot be modified (here refers to external classes, ignoring internal classes).
3. Members modified by private can only be used in the class in which they are defined, and cannot be called in other classes.
1. The default type is default;
2. It can be used to modify classes, member variables, constructors, and methods, all of which can use default permissions, that is, do not write any keywords.
3. The default permission is the same package permission. Elements with the same package permission can only be called in the class that defines them and the class in the same package.
1. Protection type protect;
2. Data members, constructors, method members can be modified, but classes cannot be modified (here refers to external classes, ignoring internal classes).
3. Members modified by protected can be called in the class in which they are defined and in the same package.
4. If there are classes from different packages that want to call them, then this class must be a subclass of the class that defines them.


3: There are several issues to note about permission modifiers;

1. Not every modifier can modify a class (referring to external classes), only public and default can.

2. All modifiers can modify data members, method members, and constructors.

3. For the sake of code security, the modifier should not be used as much as possible, but applicable. For example, data members, if there is no special need, use private as much as possible. Strengthen encapsulation;

4. Modifiers modify the "accessed" authority.

  • Question 2: What is the difference between this and super reference:
  • Problem 2 solution:
this

this is an object of itself, representing the object itself, which can be understood as: a pointer to the object itself. The usage of this can be roughly divided into three types in java:
1. There is no need to talk about ordinary direct references
. This is equivalent to pointing to the current object itself.
2. Form participation members have the same name, and use this to distinguish them.
3. Refer to the constructor this (parameter):
call another form of the constructor in this class (should be the first statement in the constructor).
super

super can be understood as a pointer to its own super (parent) class object, and this super class refers to a parent class closest to itself. There are also three uses of super:
1. Ordinary direct reference
is similar to this, super is equivalent to pointing to the parent class of the current object, so you can use super.xxx to refer to the members of the parent class.
2. The member variable or method in the subclass has the same name as the member variable or method in the parent class
3. Reference constructor
super (parameter): call a constructor in the parent class (should be the first statement in the constructor ).
super和this的异同:

super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句)
this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)
super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名 super.成员函数据名(实参)
this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名)
调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
super()和this()类似,区别是,super()从子类中调用父类的构造方法,this()在同一类内调用其它方法。
super()和this()均需放在构造方法内第一行。
尽管可以用this调用一个构造器,但却不能调用两个。
this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
this()和super()都指的是对象,所以,均不可以在static环境中使用。包括:static变量,static方法,static语句块。
从本质上讲,this是一个指向本对象的指针, 然而super是一个Java关键字。
  • 问题3:Java语言的设计者为啥明确决定不支持多继承?
  • 问题3解决方案:


James Gosling published a Java white paper titled "An Overview of Java" in February 1995, which explains why Java does not support multiple inheritance.

Java removes some rarely used, and often misunderstood, and misused features of C++, such as operator overloading (although Java still retains method overloading), multiple inheritance, and a wide range of The automatic forced isotype (extensive automatic coercions).

I would like to share James Gosling's definition of Java here.
Java: A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, and portable , high-performance, multi-threaded, dynamic languages.


Just look at the beauty of the definition. Modern programming languages ​​should have such a feature. We see, what is the definition of the first feature? is simple.


To reinforce the simplicity, this is why we removed multiple inheritance. Let's look at an example, the diamond inheritance problem of multiple inheritance.


There are two classes B and C that inherit from A. Suppose B and C both inherit A's methods and override them, writing their own implementations. Suppose D inherits B and C through multiple inheritance, then D should inherit the overloaded methods of B and C, then which should it inherit? Is it B's or C's?


This trap is often fallen into in C++, although it also suggests alternative ways to solve the problem. We don't have this problem in Java. Even if two interfaces have the same method, the implementing class will have only one method, which is written by the implementing class. Loading classes dynamically can make implementing multiple inheritance difficult.

Problems and solutions in code debugging

  • Question 1: When working on pp9.1, I encountered the following problem

  • Solution to problem 1: This is what I did not change to the Coin class at all. In the Coin class, we should change the modifier of face to protected.
  • Question 2: When doing pp9.1, I chose to create an array with a size of 5 in the test class, but when I defined it, only three were defined, so the sum value could not be calculated in the final run.

  • Solution to problem 2: Just change the order for the first question, and for the latter question, I set up a variable b to separate the calculation process.

Summary of last week's exam mistakes

  • 第一题: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

  • 分析:在书的第245页,有说在Java中,数组是必须实例化的对象。
  • 第二题: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
  • 分析:索引值必须大于等于0并且小于数组的元素个数。如果数组中有X个元素,则索引值是0~X-1.

  • 第三题: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?
    A .(int[ ])
    B .(int a[ ])
    C .(int[ ] a)
    D .(int a)
    E .(a[ ])
  • 分析:参数被定义为变量最初被声明为类型参数名称。这里,类型为int[],参数为a。

  • 第四题: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];
    A .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
    C .a single BankAccount entry
    D .1000 BankAccount entries
    E .1000 reference variables and 1000 BankAccount entries
  • 分析:第一次银行帐户;为firstentity银行保留内存空间,它本身是指向BankAccount[]对象的引用变量。第一次银行账户=新银行账户[1000];实例化BankAccount[]对象为1000个BankAccount对象。

  • 第五题: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
    uD .a, b and d
    E .a, b, c and d
  • 分析:第一个声明声明a和b都是int数组。第二个声明声明c和d是ints,但对于d,一个int数组。a b和d都是int数组。

  • 第六题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
  • 分析:这样使得两个数组是相同的,所以a和b所引用的数组是同一个,所以一个称为另一个的别名。

  • 第七题: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
  • 分析:在java命令之后,在命令行输入的任何内容都将被接受为命令行参数。如果是由空格分隔的几个单词,那么每个单词都作为一个单独的字符串数组元素存储。

  • 第八题:An array index cannot be a float, double, boolean or String.
    A .true
    B .false
  • 分析:数组索引必须是int类型,或者可以扩展为int类型的值(因此,char、byte和short也是允许的)。

  • 第九题:An array, when instantiated, is fixed in size, but an ArrayList can dynamically change in size when new elements are added to it.
    A .true
    B .false
  • 分析:数组的一个缺点是它的固定大小。一旦实例化,它的大小是固定的。ArrayList是一个使用数组的类,它会自动创建一个更大的数组,将旧数组复制到新数组中,这样ArrayList就可以根据需要更大。虽然这可以解决抛出异常的问题,但它确实会导致性能较差,因为每次增加ArrayList大小时,必须从一个数组复制到另一个数组。

  • 第十题:Just as arrays can only have a fixed number of elements, set at the time the array is declared, a parameter list also can only have a fixed number of elements, set at the time the method is declared.
    A .true
    B .false
  • 分析:Java为可变长度参数列表提供了特殊的表示法。省略号(…)用于指定可变长度参数列表。

  • 第十一题: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
  • 分析:对ArrayList的前部分进行插入或删除时,就会发生大量的元素复制,从而降低其效率。

代码托管


结对及互评

点评模板:

  • 博客中值得学习的或问题:

    • xxx
    • ...
  • 基于评分标准,我给本博客打分:XX分。得分情况如下:xxx
  • 代码中值得学习的或问题:
    • xxx
    • xxx
    • ...
  • 基于评分标准,我给本博客打分:XX分。得分情况如下:xxx

  • 参考示例

点评过的同学博客和代码

  • 本周结对学习情况
    • 20172317
    • 结对照片
    • 结对学习内容
      • XXXX
      • XXXX
      • ...

其他(感悟、思考等,可选)

概念很多,嘚仔细看,有很多细节,和前面的章节联系紧密。

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 95/95 1/1 18/18
第二周 515/620 1/2 22/40
第三周 290/910 1/3 20/60
第四周 1741/2651 1/4 30/84
第五周 579/3230 1/5 20/104
第六周 599/3829 1/6 18/122
第七周 732/4561 1/6 24/146

参考:软件工程软件的估计为什么这么难软件工程 估计方法

  • 计划学习时间:22小时

  • 实际学习时间:24小时

  • 改进情况:稍有点

(有空多看看现代软件工程 课件
软件工程师能力自我评价表
)

参考资料

Guess you like

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