与 Socket 一起使用时 Java 中的可序列化外部 class
Serializable outer class in Java when using with Socket
这里我有两个classes ClientToSendCalculable.java
import java.io.*;
import java.net.*;
public class ClientToSendCalculable implements Serializable
{
public ClientToSendCalculable(int port)
{
try
(
Socket s = new Socket("localhost", port);
ObjectOutputStream writer = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream reader = new ObjectInputStream(s.getInputStream());
)
{
System.out.println("Connection was established!");
ClientToSendCalculable.Addition addition = new ClientToSendCalculable.Addition();
addition.firstAddendum = 5;
addition.secondAddendum = 7;
writer.writeObject(addition);
double res = (Double)reader.readObject();
System.out.println(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private class Addition implements Calculable, Serializable
{
private double firstAddendum, secondAddendum;
public double calc()
{
return firstAddendum + secondAddendum;
}
}
public static void main(String[] args)
{
new ClientToSendCalculable(7777);
}
}
和ServerToGetCalculable.java
import java.io.*;
import java.net.*;
public class ServerToGetCalculable
{
public ServerToGetCalculable(int port)
{
try
(
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
ObjectOutputStream writer = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream reader = new ObjectInputStream(s.getInputStream());
)
{
System.out.println("Got a client!");
Calculable calcObj = (Calculable)reader.readObject();
double res = calcObj.calc();
writer.writeObject(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{new ServerToGetCalculable(7777);}
}
我的问题是为什么 ClientToSendCalculable.java 应该实现 Serializable?我只通过 Socket 发送其内部的 class Addition.java 但我不单独发送 ClientToSendCalculable.java 。这就是为什么对我来说,如果只有 Addition.java 是可序列化的就足够了,但是如果我使 ClientToSendCalculable.java 不是可序列化的,我会在尝试执行这行代码时遇到异常:
writer.writeObject(addition);
Addition.java 是一个内部 class(非静态)-> 它包含一个字段,其中包含对封闭的 ClientToSendCalculable 实例的引用 -> 那外部 class 的字段必须被序列化 -> 它必须实现 Serializable 接口。
这里我有两个classes ClientToSendCalculable.java
import java.io.*;
import java.net.*;
public class ClientToSendCalculable implements Serializable
{
public ClientToSendCalculable(int port)
{
try
(
Socket s = new Socket("localhost", port);
ObjectOutputStream writer = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream reader = new ObjectInputStream(s.getInputStream());
)
{
System.out.println("Connection was established!");
ClientToSendCalculable.Addition addition = new ClientToSendCalculable.Addition();
addition.firstAddendum = 5;
addition.secondAddendum = 7;
writer.writeObject(addition);
double res = (Double)reader.readObject();
System.out.println(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private class Addition implements Calculable, Serializable
{
private double firstAddendum, secondAddendum;
public double calc()
{
return firstAddendum + secondAddendum;
}
}
public static void main(String[] args)
{
new ClientToSendCalculable(7777);
}
}
和ServerToGetCalculable.java
import java.io.*;
import java.net.*;
public class ServerToGetCalculable
{
public ServerToGetCalculable(int port)
{
try
(
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
ObjectOutputStream writer = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream reader = new ObjectInputStream(s.getInputStream());
)
{
System.out.println("Got a client!");
Calculable calcObj = (Calculable)reader.readObject();
double res = calcObj.calc();
writer.writeObject(res);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{new ServerToGetCalculable(7777);}
}
我的问题是为什么 ClientToSendCalculable.java 应该实现 Serializable?我只通过 Socket 发送其内部的 class Addition.java 但我不单独发送 ClientToSendCalculable.java 。这就是为什么对我来说,如果只有 Addition.java 是可序列化的就足够了,但是如果我使 ClientToSendCalculable.java 不是可序列化的,我会在尝试执行这行代码时遇到异常:
writer.writeObject(addition);
Addition.java 是一个内部 class(非静态)-> 它包含一个字段,其中包含对封闭的 ClientToSendCalculable 实例的引用 -> 那外部 class 的字段必须被序列化 -> 它必须实现 Serializable 接口。