如何序列化具有多个参数的对象

时间:2019-10-10 15:13:54

标签: java parameters java-custom-serialization

我是Java的初学者,需要帮助。我有两个班级,一个(歌曲)参见代码是第二个(日期)的子级。 Song是可序列化的,而Date不是可序列化的(我打算以这种方式保留Date类)。我正在使用Date中的名为setDate的方法,它需要三个参数,月,日和年,所有整数。我正在尝试使用自定义序列化(使用readObject和writeObject方法等)。

package assignment7;

import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
*
* @author Owner
*/
public class Song extends Date implements Serializable{


 private String title;
 private String artist;
 private String genre;
 //private String dateOpened;
 //private Date obj = new Date();

 public Song(){

 }
 public void setTitle(String t) {
    title = t;
 }

public void setArtist(String a) {
    artist = a;
}

public void setGenre(String g) {
    genre = g;
}

public void setDateOpen(int m, int d, int y){  
    setDate(m, d, y);
}

public void setDayOpen(){

}

public void setDayOpen(){
Date
}

public void setDayOpen(){

}

public String getDateOpen(){
   return getDate();
}

public String getTitle() {
    return title;
}

public String getArtist() {
    return artist;
}

public String getGenre() {
    return genre;
}

private void writeObject( ObjectOutputStream out ) throws IOException, ClassNotFoundException, NotSerializableException {
    out.defaultWriteObject();

      out.writeObject(getTitle());
      out.writeObject(getArtist());
      out.writeObject(getGenre());
      out.writeObject(getDateOpen());

}

private void readObject( ObjectInputStream in ) throws IOException, NotSerializableException, ClassNotFoundException {
      in.defaultReadObject();

      setTitle((String)in.readObject());
      setArtist((String)in.readObject());
      setGenre((String)in.readObject());
      setDateOpen((int)in.readObject(), (int)in.readObject(), (int)in.readObject());

}

}

问题是getDateOpen方法返回一个字符串,而setDateOpen需要3个整数。有没有办法让readObjects()读取3个整数并且仍然输出序列化的字符串? (还包括我的老师说不要更改的日期课程)

package assignment7;

import java.util.Scanner;

public class Date
{
private int month;
private int day;
private int year;

public Date() { month = 0; day = 0; year = 0; }
public Date( int m, int d, int y )
{
month = editMonth( m );
day = editDay( d );
year = editYear( y );
}
public void setDate( int m, int d, int y )
{
month = editMonth( m );
day = editDay( d );
year = editYear( y );
}
public String getDate( )
{
return month + "/" + day + "/" + year;
}
public int getMonth() { return month; }
public int getDay() { return day; }
public int getYear() { return year; }

protected int editMonth( int m )
{
if( m >= 1 && m <= 12 )
  return m;
else
{
  Scanner input = new Scanner( System.in );
  while( !( m >= 1 && m <= 12 ) )
  {
    System.out.print( "Month must be 1-12 --- Please re-enter: " );
    m = input.nextInt();
  }
  return m;
  }          
  }

 protected int editDay( int d )
 {
 int [] monthDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 if( d >= 1 && d <= monthDays[month - 1] )
  return d;
 else
 {
  Scanner input = new Scanner( System.in );
  while( !( d >= 1 && d <= monthDays[month - 1] ) )
  {
    System.out.print( "Day must be 1 - " + monthDays[month - 1] + " --- 
  please re-enter: " );
    d = input.nextInt();
  }
  return d;
  }
  }

 protected int editYear( int y )
 {
 if( y >= 1 )
  return y;
 else
 {
  Scanner input = new Scanner(System.in);
  while( y < 1 )
  {
    System.out.print( "Year must be greater than 1 --- please re-enter: " 
   );
    y = input.nextInt();
  }
  return y;
  }
  }
 }

1 个答案:

答案 0 :(得分:0)

如果Date类型仅提供一个String日期,那么您需要在某个时候传递该日期。在writeObject中进行解析并存储int,或者与String保持一致是串行形式,并在readObject中进行解析。

Date仅提供字符串化日期可能不是一个好的设计选择。同样,Song也不应该是Date的子类型(除非存在一些严重的性能问题,这似乎不太可能)。

也避免Java序列化。 JSON似乎是通常的选择。