簡體   English   中英

如何序列化 object 它的方法有多個參數

[英]How to serialize an object which it's method has multiple parameters

我是 java 的初學者,我需要幫助。 我有兩個班級,一個(宋)看代碼是第二個(日期)的孩子。 Song 是可序列化的,而 Date 是不可序列化的(我打算以這種方式保留 Date class)。 我正在使用來自 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 個整數,而 output 仍然是一個序列化的字符串? (iv'e 還包括我的老師說不要更改的日期 class)

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;
  }
  }
 }

如果Date類型僅提供String日期,那么您需要在某個時候傳遞它。 要么在writeObject中解析並存儲int ,要么與String保持串行形式並在readObject中解析。

僅提供字符串化Date的日期可能不是一個好的設計選擇。 也沒有辦法Song應該是Date的子類型(除非有一些關鍵的性能問題,這似乎不太可能)。

還要避免 Java 序列化。 JSON 似乎是通常的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM