问题 问答题

请完成以下程序,首先由一个类Example2_3实现Serializable接口,并有三个成员变量,分别为int型、double型和String型,可以用toString的方法显示这三个成员变量。在main方法中创建这个Example2_3的持久对象,根据用户在命令行输入的三个参数来设定其中成员变量的值。然后,将这个对象写入名为TheSerial.data的文件,并显示成员变量。最后从文件TheSerial.data中读出三个成员变量并显示出来。 注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; class TheSerial implements Serializable { private int intValue; private double doubleValue; private String string; TheSerial() { intValue = 123; doubleValue = 12.34; string = "Serialize Test"; } public void setDouble(double d) { doubleValue = d; } public void setInt(int i) { intValue = i; } public void setString(String s) { string = s; } public String toString() { return("int="+intValue+"double="+doubleValue+" string="+string); } } public class Example2_3 { public static void main(String argv[]) { TheSerial e1 = new TheSerial(); TheSerial e2; try { e1.setInt(Integer.parseInt(argv[0])); e1.setDouble(Double.parseDouble(argv[1])); e1.setString[argv[2]); } catch(Exception e) { e1.setString(e.getMessage{)); } System.out.println(e1); try { FileOutputStream oS = new FileOutputStream("TheSerial.data"); ObjectOutputStream oIS = new ObjectOutputStream(oS); ______; } catch(IOException ioException) { System.out.println(ioException.getMessage()); } try { FileInputStream iS = new FileInputStream("TheSerial. data"); ObjectInputStream oIS = new ObjectInputStream(iS); ______ System.out.println(e2); } catch(IOException ioException) { System.out.println(ioException.getMessage()); } catch(ClassNotFoundException cnfException) { System.out.println(cnfException.getMessage()); } } }

答案

参考答案:

解析:①oOS.writeObject(e1)②e2=(TheSerial)oIS.readObject() 本题主要考查串行化相关的方法和实现。解题中首先要掌握串行化的基本过程和反串行化的过程。串行化过程首先要创建一个输出流FileOutputStream,通过该类的实例对文件进行访问,然后创建一个ObJectOutput Stream对象,通过writeObject()方法来实现对象的序列化。第一个空就是使用writeObject()实现序列化。反序列化过程中用FileInputStream对象建立读取文件的连接,并使用该对象创建一个 ObiectInputStream的实例,这个ObjectInput Stream实例的readObject()方法就可以实现对象的反序列化。第二个空就是使用readObject()实现反序列化。

完形填空

根据短文内容,从短文后的选项中选出能填入空白处的最佳的选项。选项中有两项为多余选项。 

All children want to have pocket money(零用钱). Why do their parents just give them a certain amount?  小题1: 

The amount of money that parents give to their children to spend as they wish differs(不同)from family to family.    小题2:   Some children get weekly pocket money. Others get monthly pocket money.

First of all, children are expected to make a choice between spending and saving. Then parents should make the children understand what is expected to pay for with the money. At first, some young children may spend all of the money soon after they receive it. Parents are usually advised not to offer more money until it is the right time.    小题3:  

In order to encourage their children to do some housework, some parents give pocket money if the children help around the home. Some experts think it not wise to pay the children for doing that.     小题4:   

Pocket money can give children a chance to experience the three things they can do with the money. They can spend it by giving it to a good cause. They can spend it by buying things they want.     小题5: Saving helps children understand that costly goals require sacrifice(牺牲). Saving can also open the door to future saving and investing(投资) for children. 

A.One main purpose(目的) is to let kids learn how to manage their own money.

B.They can save it for future use.

C.Timing is another consideration.

D.As helping at home is a normal(正常的) part of family life.E. Some children are not good at managing their pocket money. 

F. Learning how to get money is very important for every child.  

G. By doing so, these children will learn that spending must be done with a budget(预算)

问答题