我得到了以下错误:
java.io.NotSerializableException: cscie55.project.AccountInfo我的AccountInfo类没有实现java.io.Serializable.,我必须使它成为Serializable?吗?
如果是的话,谁能帮我做这件事吗?
以下是我的客户端类的一部分:
public class Client extends UnicastRemoteObject implements ATMListener {
public Client() throws RemoteException {
}
private static AccountInfo getAccountInfo(int accountNumber, int pin) {
//AccountInfo accountInfo = new AccountInfo(accountNumber, pin);
return new AccountInfo(accountNumber, pin);
}
public static void main(String[] args) {
ATM atm;
try {
ATMFactory factory = (ATMFactory)Naming.lookup("atmfactory");
atm = factory.getATM();
Client clientListener = new Client();
atm.addListener(clientListener);
Client.testATM(atm);
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (NotBoundException nbe) {
nbe.printStackTrace();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (RemoteException re) {
re.printStackTrace();
}
}AccountInfo类:
public final class AccountInfo {
private final int accountNumber;
private final int pin;
public AccountInfo(int accountNumber, int pin) {
this.accountNumber = accountNumber;
this.pin = pin;
}
public int getAccountNumber() {
return accountNumber;
}
public int getPin() {
return pin;
}
}帐户在BankImpl类中创建:
public class BankImpl extends UnicastRemoteObject implements Bank {
public static LinkedHashMap<Integer, Account> accounts;
public BankImpl() throws RemoteException {
accounts = new LinkedHashMap<Integer, Account>();
//constructor creates 3 accounts
Account account1 = new Account(0000001, 1234, 0, true, true, true);
Account account2 = new Account(0000002, 2345, 100, true, false, true);
Account account3 = new Account(0000003, 3456, 500, false, true, true);
//assigns all the accounts to a collection
accounts.put(0000001, account1);
accounts.put(0000002, account2);
accounts.put(0000003, account3);
}发布于 2014-06-02 23:36:02
使您的AccountInfo类实现可序列化:
public final class AccountInfo implements Serializable {您不需要添加任何其他方法,但是为了安全起见,您应该在serialVersionUID类中定义一个AccountInfo:
private static final long serialVersionUID = insert_random_long_here;您可以使用这个站点生成一个随机的serialVersionUID:http://random.hd.org/getBits.jsp?numBytes=0&type=svid
发布于 2014-06-03 01:42:56
我的AccountInfo类没有实现java.io.Serializable。我必须序列化它吗?
没有,但您的问题表明您正在序列化它。我想你的意思是‘我必须让它成为Serializable?吗?答案是’是‘,如果你想序列化它的话。
如果它正在被序列化,并且您不希望它序列化,那么在被序列化的对象中对它进行transient引用。
UnicastRemoteObject是服务器,而不是客户端。
https://stackoverflow.com/questions/24004891
复制相似问题