没有合适的资源?快使用搜索试试~ 我知道了~
OCJP认证-3期(PX017) 面向对象程序设计-IO处理.doc
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 201 浏览量
2022-07-13
05:20:25
上传
评论
收藏 62KB DOC 举报
温馨提示
试读
11页
OCJP认证-3期(PX017) 面向对象程序设计-IO处理.doc 学习资料 复习资料 教学资源
资源推荐
资源详情
资源评论
真题分析
面向对象程序设计
I/O 处理
(1)序列化及解(反)序列化(serialization and deserialization)
import java.io.*;
class Brad implements Serializable {
Brad(){
System.out.println("Brad");
}
}
class A {
public A(){
System.out.println("A");
}
}
class B extends A {
public B(){
System.out.println("B");
}
}
class C extends B implements Serializable {
private Brad brad = new Brad();
public C(){
System.out.println("C");
}
public static void main(String[] args){
C c = new C();
try{
FileOutputStream fos = new FileOutputStream("oo.tmp");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(c);
oos.close();
fos.close();
System.out.println("------------------------");
FileInputStream fis = new FileInputStream("oo.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
C cc = (C)ois.readObject();
ois.close();
fis.close();
}catch (Exception ee){System.out.println("Exception");}
}
}
(2)
Given the following Java code:
import java.io.* ;
public class Forest implements Serializable {
private Tree tree = new Tree () ;
public static void main ( String[] args ) {
Forest f = new Forest () ;
try {
FileOutputStream fs = new FileOutputStream ("Forest.Ser") ;
ObjectOutputStream os = new ObjectOutputStream (fs) ;
os.writeObject (f) ;
os.close () ;
} catch (Exception ex) { ex.printStackTrace () ; }
}
}
class Tree {}
What is the result? (
結果如何
?)
A. Compilation fails. (
編譯失敗
)
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. An instance of Forest and an instance of Tree are both serialized.
答案:B
解析:
� 有编译失败的答案选项, 当然要先检查基本语法.
� Forest 类别宣告 implements Serializable, 而该类别中的对象成员 Tree, 是必须也
可以被串行化, 然而在 Tree 类别并未 implements Serializable.
� 因此会在执行时期, 在执行 os.writeObject (f) ; 时丢出一个
java.io.NoSerializableException, 被捕捉到而印出.
� 里面的成员也是可串行化的,Tree 类继承 Serializable;
(3)Given the following Java code:
1.import java.io.* ;
2.public class Foo implements Serializable {
3.public int x, y;
4.public Foo ( int x, int y ) { this.x = x; this.y = y; }
5.
6.private void writeObject ( ObjectOutputStream s )
7.throws IOException {
8.s.writeInt(x); s.writeInt ( y ) ;
9.}
10.
11.private void readObject ( ObjectInputStream s )
12.throws IOException, ClassNotFoundException {
13.
14.// insert code here
15.
16.}
17.}
Which code, inserted ay line 14, will allow this to correctly serialized and desterilize?
A. s.defaultReadObject();
B. this = s.defaultReadObject();
C. y = s.default(); x = s.readInt();
D. x = s.readInt(); y = s.readInt();
剩余10页未读,继续阅读
资源评论
努力搬砖的小王
- 粉丝: 16
- 资源: 7706
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功