package rexlh;
import java.lang.Exception;
import java.io.*;
import java.io.IOException;
import java.io.Serializable;
public class ReadTeacher {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
ObjectInputStream ois=null;
try{
ois=new ObjectInputStream(new FileInputStream("teacher.txt"));
Teacher t1=(Teacher)ois.readObject();
Teacher t2=(Teacher)ois.readObject();
Person p=(Person)ois.readObject();
Teacher t3=(Teacher)ois.readObject();
System.out.println("t1:"+(t1.getStudent()==p));
System.out.println("t2:"+(t2.getStudent()==p));
System.out.println("t2与t3"+(t2==t3));
}
catch(IOException ex)
{ ex.printStackTrace();
}
finally{
try{
if(ois!=null)
ois.close();}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
import java.lang.Exception;
import java.io.*;
import java.io.IOException;
import java.io.Serializable;
public class ReadTeacher {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
ObjectInputStream ois=null;
try{
ois=new ObjectInputStream(new FileInputStream("teacher.txt"));
Teacher t1=(Teacher)ois.readObject();
Teacher t2=(Teacher)ois.readObject();
Person p=(Person)ois.readObject();
Teacher t3=(Teacher)ois.readObject();
System.out.println("t1:"+(t1.getStudent()==p));
System.out.println("t2:"+(t2.getStudent()==p));
System.out.println("t2与t3"+(t2==t3));
}
catch(IOException ex)
{ ex.printStackTrace();
}
finally{
try{
if(ois!=null)
ois.close();}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
import java.io.*;
public class ReadTeacher {/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("teacher.txt"));
Teacher t1;
try {
t1 = (Teacher) ois.readObject();
Teacher t2 = (Teacher) ois.readObject();
Person p = (Person) ois.readObject();
Teacher t3 = (Teacher) ois.readObject();
System.out.println("t1:" + (t1.getStudent() == p));
System.out.println("t2:" + (t2.getStudent() == p));
System.out.println("t2与t3" + (t2 == t3));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}class Person{}
class Teacher extends Person{ public Person getStudent() {
return null;
}}
teacher.txt 这里面的内容对吗 ?
Person哪里来的 ?
也可以像1楼这样定义在ReadTeacher类后面,都能解决