第六周

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

教材学习内容总结

  • 相同类型的变量,调用同一个方法时呈现出的不同行为特征,这就是多态
  • 与方法不同的是,对象的实例不具备多态性。比如
BaseClass ploymophicBc = new Subclass();

ploymophicBc引用变量,程序中输出它的book实际变量时,并不是输出Subclass类定义的实际变量,而是输出BaseClass类的实际变量

  • 引用变量之间的转换只能在具有继承关系的两个类型之间进行,如果两个变量没有任何继承关系的类型,则无法进行类型转换,否则编译时就会出现错误。
  • 如果试图把一个父类实例转换成子类类型,则这个对象必须实际上是子类实例才行(即编译时类型是父类类型,而运行时类型是子类类型),否则将在运行时引发ClassCastException异常
  • 实现多态的方式也可以是接口,即定义一个借口,可以实现一个借口呈现多个不同的形态。
    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException, UnsupportedEncodingException {
        Encryptable e ;
        e = new password("hello");
        e.encrypt();
        System.out.println(e.decrypt());
        e = new Secret("hello");
        System.out.println(e.toString());
        e.encrypt();
        System.out.println(e.toString());
        e.decrypt();
        System.out.println(e.toString());

package pp94;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public interface Encryptable {
    public void encrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException;
    public String decrypt() throws BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException;
}

  • 在Java中可以使用interface定义抽象的行为和外观。接口中的方法没有操作时,一定得是公开且抽象。
  • try catch块的初始想法是if语句,类似于if else来判断出异常的情况,异常是抽象的,所以无法转换为计算机可识别的代码,在这种情形下,java提出了一种假设:如果程序可以顺利完成,那就一切正常,把系统的业务实现代码放在try块中定义,所有的异常处理逻辑放在catch块中进行处理。
try
{
    
}
catch
{
    alert输入不合法
    goto retry
}
  • 异常也可以进行继承,实现异常的层次。
  • 无论以何种方式退出try语句块,都会执行finally语句(可用来回收资源)
  • throw可以用来抛出异常
  • 接口类似于共用一套方法(利用这一特性,可以实现多态,定义一组借口对象,同一种方法可以输出不同的效果)

教材学习中的问题和解决过程

  • 问题1:为什么子类可以转换成父类?
  • 问题1解决方法:因为子类实际上是一种特殊的父类,因此java允许把一个子类对象直接赋值给一个父类引用变量,无须任何类型的转换,或者被称为向上转型,向上转型由系统自动完成。
  • 问题2:对象的实例变量是否具备多态性?
  • 问题2解决方法:对象的实例变量不具备多态性,如
    subclass是baseclass的子类,定义
baseclass p = new subclass();

输出程序中的一个实例变量时,我们输出的不是subclass里的实例变量,而是baseclass的实例变量。引用变量在编译阶段只能调用其编译时类型所具有的所有方法,但运行时则执行它运行时类型所具有的方法。因此,编写java代码时,引用变量只能调用声明该变量时所用类型里包含的方法。通过引用变量来访问其包含的实例变量时,系统总是试图访问它编译时类型所定义的成员变量,而不是它运行时类型所定义的成员变量。

  • 问题3:父类是否可以转换成子类
  • 问题3解决方法:引用变量类型之间的转换只在能具有继承关系的两个类型之间进行,如果两个没有任何继承关系的类型,则无法进行类型转换,否则编译时会出现错误。如果试图把一个父类实力转换成子类类型,则这个对象必须实际上是子类实例才可以(即编译时类
    型为父类类型,而运行时类型为子类类型),否则将在运行时发生异常。

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

  • 问题1:

  • 问题1解决方法:在stafflist对象后应该加上一个括号,这个的意思是先把对象强制转换成executive类型的参数,再调用里面的方法。

  • 问题2:

  • 问题2解决方法:我们在定义一个数组的时候用了
String[] a = new String[3]

这只是申请了三个空间而已如下面的test数组一般
image

  • 问题3:

  • 问题3解决方法:经过检查,发现是最后的e里的toString和类中的tostring大小写问题,但是为什么会不报错呢?我去掉了tostring重新试了一遍,发现输出结果是一样的

而我修改了类secret里的tostring,但是主函数里并没有tostring这个方法可以调用,只有toString,我觉得,应该是toString是父类中的一个方法,即接口里的一个方法,输出的结果就是e,只有利用方法重写,修改后才能输出完整的结果,而修改了secret里面的toString名称并无作用,因为实例对象并不具有多态性。
发现情况几乎是一样的,所以预测,一开始

代码托管

(statistics.sh脚本的运行结果截图)

上周考试错题总结

1.What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0.

for (j=0; j < list.length; j++)

if (list[j] < temp) c++;

A.It finds the smallest value and stores it in temp

B.It finds the largest value and stores it in temp

C.It counts the number of elements equal to the smallest value in list

D.It counts the number of elements in list that are less than temp
E.It sorts the values in list to be in ascending order

解析:语句if(list [j] <temp)c ++; 将列表中的每个元素与temp进行比较,并且仅在元素小于temp时才将其添加到c,因此它将对列表中小于temp的元素进行计数,并将结果存储在c中。

2.Which of the following statements is completely true?

A.If a class is declared to be abstract then every method in the class is abstract and must be overridden

B.If a class is declared to be abstract then some methods in the class may have their bodies omitted

C.If a class is declared to be abstract then all methods in the class must have their bodies omitted

D.If a class is declared to be abstract then all the instance variables must be overridden when a concrete class is derived from the abstract base class

解析:正确答案为B,如果声明了抽象类,则其内部某些方法肯定会是抽象方法。

3.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中捕获语句,否则它将继续以与调用这些方法相反的顺序在方法中传播。但是,一旦达到主要方法,该过程就会停止。如果未捕获到该异常,则异常将导致程序终止(这将是答案a,该异常被忽略)。但是,不会将异常抛出给Exception类,所以正确选D。

4.An exception can produce a "call stack trace" which lists

A.the active methods in the order that they were invoked

B.the active methods in the opposite order that they were invoked

C.the values of all instance data of the object where the exception was raised

D.the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised

E.the name of the exception thrown

解析:调用堆栈跟踪提供了存储在运行时堆栈上的方法的名称。方法名称以与放置时相反的顺序从堆栈中删除,也就是说,最早的方法首先放置在此,第二个方法放置在第二个,依此类推,以便最近调用的方法是堆栈中的最后一项,因此它是第一个被删除的。然后,堆栈跟踪以与调用它们相反的顺序显示所有活动方法(最新的优先),所以选B。

5.Assume Exceptionname is a checked exception. If a method uses a class that can generate Exceptionname, then either the method must include try and catch statements where a catch statement catches Exceptionname, or the method header must include the statement

A.throw Exceptionname

B.throws Exceptioname

C.catch Exceptionname

D.catches Exceptionname

E.implements Exceptionname

解析:必须在某个地方捕获已检查的异常,因此,如果未将其捕获到可能生成异常的方法中,则必须将异常抛出给所谓的方法。通过声明该方法抛出Exceptionname来完成此操作,所以本题选B。

6.Character streams manage

A.byte-sized data

B.binary data

C.Unicode characters

D.ASCII characters

E.compressed data

解析:字符流用于管理16位Unicode字符。这不同于用于管理任何类型的字节大小的数据(包括ASCII字符和其他类型的二进制数据)的字节流,所以选c。

7.PrintWriter is a better output stream class that PrintStream because PrintWriter

A.has both print and println methods and PrintStream only has print

B.can output both byte and character streams and PrintStream can only output byte streams

C.has error checking mechanisms as part of the class and PrintStream does not

D.will not throw checked exceptions and PrintStream will

E.all of the above

解析:PrintWriter类是Writer类,而PrintStream类是Stream类。主要区别在于PrintWriter专用于文件,因此具有不属于PrintStream的错误检查机制,所以选c。

  • 上周博客互评情况

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

  • 这周对于课本的学习相对来说较少,一是内容并不是特别难,二是遇上了国庆小假期,接着是实验比较难,花费的时间比较多。但是还是收获了很多,对于多态还有异常这两块内容,对于我们将来在编写大程序的时候会起到相当至关重要的作用。
  • 异常这一张让我意识到,idea并不是全部,在idea里打异常有着先天的优势,什么类型的错误,哪里改写什么,都可以很好的给我们提示让我们打出来,甚至有时候代码都不需要我们打,只需要象征性打几个开头就好了,有时候还是要回归一下记事本和linux,在那里学习的java基础比较牢固。
代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 200/200 2/2 20/20
第二周 300/500 2/4 18/38
第三周 623/1000 3/7 22/60
第四周 600/1600 2/9 22/82
第五周 1552/2987 2/11 22/94
第六周 892/3879 2/11 8/102
参考:软件工程软件的估计为什么这么难软件工程 估计方法
  • 计划学习时间:10小时

  • 实际学习时间:8小时

  • 改进情况:

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

参考资料

原文地址:https://www.cnblogs.com/yangkaihan/p/11680465.html