********2019-2020-1 《数据结构与面向对象程序设计》第六周学习总结

********2019-2020-1 《数据结构与面向对象程序设计》第六周学习总结

教材学习内容总结

多态的优点:

  1. 消除类型之间的耦合关系
  2. 可替换性
  3. 可扩充性
  4. 接口性
  5. 灵活性
  6. 简化性

重写(Override):

是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都无法改变。
备注:

  1. 父类的成员方法只能被它的子类重写。
  2. 声明为 final 的方法不能被重写。
  3. 声明为 static 的方法不能被重写,但是能够被再次声明。
  4. 构造方法不能被重写。

多态存在的三个必要条件

  1. 继承
  2. 重写
  3. 父类引用指向子类对象
    E.G:
Staff personal = new StaffMember();

备注:当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误;如果有,再去调用子类的同名方法。

上周考试错题总结

1.Which of the following lists of numbers would accurately show the array {9,4,12,2,6,8,18} after the first pass through the Selection Sort algorithm?


A .9, 4, 12, 2, 6, 8, 18

B .4, 9, 12, 2, 6, 8, 18

C .2, 4, 12, 9, 6, 8, 18

D .2, 4, 6, 8, 9, 12, 18

E .2, 4, 9, 12, 6, 8, 18

  • 解析:在“选择排序”的每个连续遍历中,找到最小的未排序值并与当前数组索引交换(当前索引从0开始,一直到数组的第二个位置)。在第一遍中,最小的元素2与索引0交换,因此2和9交换位置。

2.We compare sorting algorithms by examining


A .the number of instructions executed by the sorting algorithm

B .the number of instructions in the algorithm itself (its length)
C .the types of loops used in the sorting algorithm

D .the amount of memory space required by the algorithm

E .whether the resulting array is completely sorted or only partially sorted


  • 解析:不同的排序算法在执行时需要不同数量的指令。例如,选择排序通常需要比插入排序更多的指令。因此,我们将排序算法按执行排序数组所需的指令数进行比较。我们可以计算排序算法在最坏情况下执行的指令的最大数量,或者最佳情况下的最小数量,或者平均执行指令的数量。如果两个排序算法需要大致相同数量的指令来对数组进行排序,那么我们还可以检查所需的内存空间量。

3.Both the Insertion Sort and the Selection Sort algorithms have efficiencies on the order of ________ where n is the number of values in the array being sorted.


A .n

B .n * log n

C .n^2

D .n^3

E .Insertion sort has an efficiency of n and Selection Sort has an efficiency of n^2

  • 解析:两种排序算法都使用两个嵌套循环,每个循环都执行大约n次,这给两者都带来了N*N或N ^ 2的复杂性。

4.Polymorphism is achieved by


A .overloading

B .overriding

C .embedding

D .abstraction

E .encapsulation

  • 解析:重载只是为具有不同参数列表的方法提供了替代方法。重写提供了多态性,因为根据当前被引用的对象调用相应的方法。嵌入是类中类的封闭。抽象与多态性无关。使用可见性修饰符(public、private、protected)实现封装。

5.Comparing the amount of memory required by selection sort and insertion sort, what can one say?


A .Selection sort requires more additional memory than insertion sort

B .Insertion sort requires more additional memory than selection sort

C .Both methods require about as much additional memory as the data they are sorting

D .Neither method requires additional memory

E .None of the above

  • 解析:选择排序和插入排序都可以“就地”实现。这意味着不需要额外的内存,只要在两种排序过程中在数据数组中重新排列要排序的数据。

6.Java allows one to create polymorphic references using inheritance and using interfaces.


A .true

B .false

  • 解析:继承允许使用一个基变量来引用不同的子代成员在执行期间将使用正确的子代成员。这就是多态性的作用。接口提供了类似的机制,但使用的是抽象方法,而不是继承中使用的具体方法。但效果是一样的。

7.The type of the reference, not the type of the object, is use to determine which version of a method is invoked in a polymorphic reference.

A .true

B .false

  • 解析:这题没看懂题目= =

8.(An) interface reference can refer to any object of any class that implements the interface.


A .true

B .false

  • 解析:这是使用接口名声明引用变量的多态函数之一。

9.A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?


A .ignore the exception

B .handle the exception where it arose using try and catch statements

C .propagate the exception to another method where it can be handled

D .throw the exception to a pre-defined Exception class to be handled

E .all of the above are ways that a Java program could handle an exception

  • 解析:如果代码包含在try语句中并实现了相应的catch语句,则抛出的异常要么被当前代码捕获,要么被传播到调用导致异常的方法并在相应的catch语句中捕获的方法,或者它继续通过方法传播,传播顺序与调用这些方法的顺序相反。但是,一旦达到主方法,此过程就会停止。如果未在那里捕获,则异常会导致程序终止。但是,不会向异常类抛出异常。

10.NullPointerException and ArithmeticException are both derived from which class?


A .Error

B .Exception

C .RuntimeException

D .IllegalAccessException

E .CheckedException

  • 解析:这两个异常都是RuntimeException的子级,RuntimeException本身是Exception的子级。error是一个可丢弃的对象,它不同于exception,而illegalaccessexception和checkedeexception是exception的子级,但不是runtimeexception。

代码调试中的问题和解决过程

  • 问题1:提示BYTE型未初始化:
    • 解决方案:
// 方式 1
byte allBitsOne = 0xFF;
// 方式2
byte allBitsOne = (byte)0xFF; 
// 方式3
byte allBitsOne = 0xFFFFFFFF; 

可行方案:2,3。
方案1不可行原因:在 Java 中,literal integers是采用的固定的有符号 32 位整型来表示的。而 byte 类型在 Java 中又是一个有符号的 8 位类型,表示范围是十进制 -128~127。于是 0xFF 代表的十进制 255 超过了 byte 类型的表示的范围。

  • 问题2:输出地址问题:

    • 解决方案:起初我觉得这是乱码,后来经过判断是byte数组combination的哈希值,改为toString()方法后输出正确内容。
  • 问题3:堆栈溢出错误:

    • 解决方案:删除Secret对象中的Password实例对象,删除Password对象中的Secret实例对象。

代码托管

结对及互评

点评

  • 博客中值得学习的:
    • 教材学习内容概括行强,简介明了。
    • 有自己动手打新代码,加入了很多个人的理解。
    • 内容很充实,很用心,比上次进步了很多。
    • 博客格式正确,运用了很多不同的方法,排版精美。
    • 希望能在课本内容总结以及问题&解决过程中加入自己的思考,使博客内容更加充实。
      基于评分标准,我给本博客打分12分:
      得分情况如下:
      正确使用Markdown语法(加1分)
      模板中的要素齐全(加1分)
      教材学习中的问题和解决过程(加2分)
      代码调试中的问题和解决过程(加2分)
      其他加分(加6分)
      进度条中记录学习时间与改进情况(1)
      感想,体会不假大空(1)
      有动手写新代码(1)
      错题学习深入(1)
      点评认真,能指出博客和代码中的问题(1)
      结对学习情况真实可信(1)

点评过的同学博客和代码

  • 本周结对学习情况:

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

(╯▔皿▔)╯

参考资料

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 10000行 30篇 400小时
第一周 138/138 2/2 23/23 减少了鼠标的使用次数
第二周 749/887 1/4 25/48
第三周 765/1652 1/4 25/48
第四周 694/2346 1/6 20/87
第五周 1659/4005 1/8 21/105
第六周 531/4536 1/9 23/128
posted on 2019-10-14 16:00  Mr.Stark丿  阅读(269)  评论(1编辑  收藏  举报