我已经建立了一个客户端服务器应用程序,并具有有效的连接设置和处理功能。我希望客户端发送不同类型的请求,每个请求都有唯一的字段,但是所有字段都继承自“ Request”超类。有办法吗?
例如,我有一个实现可序列化的Foo类
class Foo implements Serializable{
public String name;
public Foo() {
this.name = "Default";
}
public Foo(String name) {
this.name = name;
}
}
和两个扩展它的子类
class BarOne extends Foo {
public String name;
public int id;
public Bar(String name, int id) {
super(name);
this.id = id;
}
}
class BarTwo extends Foo {
public String name;
public String lastName;
public Bar(String name, String lastName) {
super(name);
this.lastName = lastName;
}
}
是否可以让客户端通过ObjectOutputStream发送BarOne和BarTwo的实例,并让服务器确定接收到的是对方还是对方的实例?
答案 0 :(得分:1)
这里没有限制,您可以轻松地序列化子类。
在这里,我提供一个使用与您相同的结构的快速示例(但有具体示例)。
抽象类。
public abstract class Pojo implements Serializable {
private static final long serialVersionUID = -4947411931465651278L;
protected int id;
public Pojo() {
// TODO Auto-generated constructor stub
}
public Pojo(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
第一个子类Person
class Person extends Pojo {
private static final long serialVersionUID = -7814628079202659483L;
private String name;
private int age;
public Person(int id, String name, int age) {
super(id);
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
}
}
第二个子类Address
class Address extends Pojo {
private static final long serialVersionUID = -8266402026827561883L;
private String address;
private String city;
public Address(int id, String address, String city) {
super(id);
this.address = address;
this.city = city;
}
@Override
public String toString() {
return "Address [address=" + address + ", city=" + city + ", id=" + id
+ "]";
}
}
然后您可以为此示例创建一个集合,其中包含两种类型的实例:
List<Pojo> pojos = new ArrayList<>();
pojos.add(new Address(1, "Address 1", "city1"));
pojos.add(new Address(2, "Address 2", "city2"));
pojos.add(new Person(1, "Name1", 5));
pojos.add(new Person(2, "Name2", 10));
pojos.add(new Person(3, "Name3", 15));
您可以使用:
使用文件进行序列化(因为我没有时间创建套接字系统;))private static void serialize(String filename, Serializable data)
throws IOException {
try (OutputStream outStream = new FileOutputStream(filename);
ObjectOutputStream fileObjectOut = new ObjectOutputStream(
outStream)) {
fileObjectOut.writeObject(data);
}
}
private static Object deserialize(String filename) throws IOException,
ClassNotFoundException {
try (InputStream inStream = new FileInputStream(filename);
ObjectInputStream fileObjectIn = new ObjectInputStream(inStream)) {
return fileObjectIn.readObject();
}
}
让序列化和反序列化列表以查看类型是否已恢复:
serialize("data.ser", pojos);
List<Pojo> tmp = deserialize("data.ser");
System.out.println(tmp);
然后您将看到具有正确实例类型的结果:
[
Address [address=Address 1, city=city1, id=1],
Address [address=Address 2, city=city2, id=2],
Person [name=Name1, age=5, id=1], Person [name=Name2, age=10, id=2],
Person [name=Name3, age=15, id=3]
]
因此,您可以执行任何操作,仅序列化Person
,就需要首先恢复Object
,然后使用instanceof
来查看恢复的类型。 / p>
Pojo p1 = new Person(1, "Name1", 10);
serialize("person.ser", p1);
Object o1 = deserialize("person.ser");
System.out.println(o1);
System.out.println(o1.getClass());
System.out.println(o1 instanceof Person);
输出
Person [name=Name1, age=10, id=1]
class serial.Person
true