gpt4 book ai didi

java - 对象的序列化给出了不同的实例?

转载 作者:行者123 更新时间:2023-11-30 06:45:19 25 4
gpt4 key购买 nike

所以我有 3 个不同的对象:牙医、患者和预约。

牙医和患者各有一个 ArrayList,以便他们共享相同的预约。但问题来了。当我将牙医和患者序列化为文本文件,然后再次反序列化它们时,在预约中所做的更改仅在患者预约中更新?

我有以下创建约会的代码:

Appointment a = new Appointment (p, d, 20.0, startDate, endDate, s);
p.addAppointment(a);
d.addAppointment(a);

patients.add(p);
dentists.add(d);

ReadAndWrite.writePatients(patients, "patients.txt");
ReadAndWrite.writeDentists(dentists, "dentists.txt");

这是同一个奇怪的 Appointment 对象 - 为什么所做的更改在两者中都没有更新???

编辑:我在下面添加了 3 个类(以及患者和牙医继承的用户类:

用户:

public class User implements Serializable {

protected String firstName;
protected String lastName;
protected String address;
protected LocalDate dateOfBirth;
protected int phoneNumber;
protected long cpr;
protected int postCode;
protected String username;
protected String password;
protected boolean loggedIn;

protected ArrayList<Appointment> appointments = new ArrayList<Appointment>();

public User (String firstName, String lastName, String address, LocalDate dateOfBirth, int phoneNumber, int cpr, int postCode)
{
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.dateOfBirth = dateOfBirth;
this.phoneNumber = phoneNumber;
this.cpr = cpr;
this.postCode = postCode;


this.username = createUsername(firstName, lastName);
this.password = createPassword(lastName, cpr);
}

public User () { }

public static String createUsername(String firstName, String lastName)
{
if(!(firstName.length() == 0 || lastName.length() < 3))
{
String first = firstName.substring(0, 1);
String last = lastName.substring(0, 3);
return first + last;
}
return null;
}

public static String createPassword(String lastName, long l)
{
if(!(lastName.length() < 3))
{
String last = lastName.substring(0 , 3);
String pass = Long.toString(l);

pass = pass.substring(pass.length() - 4, pass.length());
return last + pass;
}
return null;
}

public static boolean isPostCodeValid(String s)
{
String n = ".*[0-9].*";
if(s.matches(n) && s.length() == 4)
{
int temp = Integer.parseInt(s);

if (temp <= 9999)
{
return true;
}

return false;
}

return false;
}

public static boolean isCPRValid(String s)
{
if (!s.contains("-"))
{
return false;
}

String[] parts = s.split("-");
String ddmmyy = parts[0];
String security = parts[1];

if (!(ddmmyy.length() != 6 || security.length() != 4))
{
String n = ".*[0-9].*";
if(ddmmyy.matches(n) && security.matches(n))
{
return true;
}
}
return false;
}

public static boolean isPhoneNumberValid(String s)
{
if (s.length() != 8)
{
return false;
}
String n = ".*[0-9].*";
return (s.matches(n));
}

public static boolean isNameValid(String s)
{
if(s.isEmpty())
{
return false;
}
String n = ".*[0-9].*";
return !(s.matches(n));
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public LocalDate getDateOfBirth() {
return dateOfBirth;
}

public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public int getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
}

public long getCpr() {
return cpr;
}

public void setCpr(Long cpr) {
this.cpr = cpr;
}

public int getPostCode() {
return postCode;
}

public void setPostCode(int postCode) {
this.postCode = postCode;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public boolean isLoggedIn() {
return loggedIn;
}

public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}

public ArrayList<Appointment> getAppointments() {
return appointments;
}


public void addAppointment (Appointment a)
{
appointments.add(a);
}`

牙医

public class Dentist extends User  {        
static AtomicInteger nextId = new AtomicInteger();
private int id;
protected ArrayList<LocalDateTime> availabilities = new ArrayList<LocalDateTime>();
protected ArrayList<Service> services = new ArrayList<Service>();
protected String clinicName;

public Dentist(String firstName, String lastName, String address, LocalDate dateOfBirth, int phoneNumber, int cpr, int postCode, String clinicName) {
super(firstName, lastName, address, dateOfBirth, phoneNumber, cpr, postCode);
this.clinicName = clinicName;
id = nextId.incrementAndGet();
}

public Dentist()
{
id = nextId.incrementAndGet();
}

public int getId() {
return id;
}

public ArrayList<LocalDateTime> getAvailabilities() {
return availabilities;
}

public void setAvailabilities(ArrayList<LocalDateTime> availabilities) {
this.availabilities = availabilities;
}

public void addAvailabilities(LocalDateTime date)
{
this.availabilities.add(date);
}

public ArrayList<Service> getServices() {
return services;
}

public void setServices(ArrayList<Service> services) {
this.services = services;
}

public void addService (Service s)
{
services.add(s);
}

public String getClinicName() {
return clinicName;
}

public void setClinicName(String clinicName) {
this.clinicName = clinicName;
}

public static void setNextId(AtomicInteger nextId) {
Dentist.nextId = nextId;
}

患者:

public class Patient extends User  {

static AtomicInteger nextId = new AtomicInteger();
private int id;
private CreditCard creditCard;

public Patient(String firstName, String lastName, String address, LocalDate dateOfBirth, int phoneNumber, int cpr, int postCode) {
super(firstName, lastName, address, dateOfBirth, phoneNumber, cpr, postCode);
this.creditCard = null;
id = nextId.incrementAndGet();

}

public Patient ()
{
id = nextId.incrementAndGet();
}

public CreditCard getCreditCard() {
return creditCard;
}

public void setCreditCard(CreditCard creditCard) {
this.creditCard = creditCard;
}

public int getId() {
return id;
}

public static void setNextId(AtomicInteger nextId) {
Patient.nextId = nextId;
}

预约:

public class Appointment implements Serializable {
private double price;

private Patient patient;
private Dentist dentist;
private Service service;

private LocalDateTime startTime;
private LocalDateTime endTime;

static AtomicInteger nextId = new AtomicInteger();
private int id;

public Appointment(Patient patient, Dentist dentist, double price, LocalDateTime startTime, LocalDateTime endTime, Service service)
{
this.patient = patient;
this.dentist = dentist;
this.price = price;
this.startTime = startTime;
this.endTime = endTime;
this.service = service;
id = nextId.incrementAndGet();
}

public Appointment()
{
id = nextId.incrementAndGet();
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}


public Patient getPatient() {
return patient;
}

public void setPatient(Patient patient) {
this.patient = patient;
}

public Dentist getDentist() {
return dentist;
}

public void setDentist(Dentist dentist) {
this.dentist = dentist;
}

public LocalDateTime getStartTime() {
return startTime;
}

public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}

public LocalDateTime getEndTime() {
return endTime;
}

public void setEndTime(LocalDateTime endTime) {
this.endTime = endTime;
}

public int getId() {
return id;
}


public String toString()
{
return getService().getName() + " -------" + "aID: " + getId() + " " + getStartTime() + " with PATIENT: " + patient.getFirstName() +" " + patient.getLastName() + " and DENTIST: " + dentist.getFirstName() + " " + dentist.getLastName() + " at " + dentist.getAddress();
}

public Service getService() {
return service;
}

public void setService(Service service) {
this.service = service;
}

public static void setNextId(AtomicInteger nextId) {
Appointment.nextId = nextId;
}

序列化代码:

public static void writePatients(ArrayList<Patient> list, String file) 
{
try {
FileOutputStream f = new FileOutputStream(new File(file));
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(list);
o.close();
f.close();
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
}
}

最佳答案

您必须更改您的实现,以便一次对所有对象进行(反)序列化。例如通过提供/实现

public class PersistenceService {
public void serialize(String fname, List<Serializable> outgoing) {...

public List<Serializable> deserialize(String fname) {

您已经拥有实现此类的大部分代码;最重要的是,您只需:

  • 将您的列表“合并”为一个以便写作
  • 检索该单个列表;并根据每个条目的特定类别将其“切片”为子列表(例如,通过迭代该结果列表;并使用 instanceof 来确定每个条目是否是患者、牙医等...... .)

关于java - 对象的序列化给出了不同的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43781674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com