简体   繁体   English

如何序列化 object 它的方法有多个参数

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

I'm a beginner to java and I need help.我是 java 的初学者,我需要帮助。 I have two classes, one (Song) see code is a Child of the second one (Date).我有两个班级,一个(宋)看代码是第二个(日期)的孩子。 Song is serializeable while Date is not serializeable (and i intend to keep the Date class that way). Song 是可序列化的,而 Date 是不可序列化的(我打算以这种方式保留 Date class)。 I'm using method from Date called setDate, it take three parameters, month, day and year, all integers.我正在使用来自 Date 的方法,称为 setDate,它需要三个参数,月、日和年,都是整数。 I'm trying to use custom serialization (using readObject and writeObject methods and such).我正在尝试使用自定义序列化(使用 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());

}

} }

The problem is that the getDateOpen method returns a string, while setDateOpen requires 3 ints.问题是 getDateOpen 方法返回一个字符串,而 setDateOpen 需要 3 个整数。 is there a way to to have readObjects() read 3 ints and still output a serialized string?有没有办法让 readObjects() 读取 3 个整数,而 output 仍然是一个序列化的字符串? (iv'e also included the date class which my teach said not to change) (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;
  }
  }
 }

If the Date type only offers a String date, then you'll need to pass that at some point.如果Date类型仅提供String日期,那么您需要在某个时候传递它。 Either parsing in the writeObject and storing int s, or keeping with the String is the serial form and parsing in readObject .要么在writeObject中解析并存储int ,要么与String保持串行形式并在readObject中解析。

Date only offering a stringified date probably isn't a good design choice.仅提供字符串化Date的日期可能不是一个好的设计选择。 Also there is no way a Song should be a subtype of Date (unless there's some critical performance issue, which seems unlikely).也没有办法Song应该是Date的子类型(除非有一些关键的性能问题,这似乎不太可能)。

Also avoid Java Serialization.还要避免 Java 序列化。 JSON seems the usual alternative. JSON 似乎是通常的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Postman 测试控制器的方法,该方法在 Spring 应用程序中将一个(或多个)对象作为参数? - How do I test with Postman a Controller's method which has one (or multiple) objects as parameters in a Spring Application? 如何序列化具有ObservableList的对象? - How to serialize an object that has an ObservableList on it? 如何序列化具有一个成员(即json字符串)的Java对象 - How can I serialize a Java object that has one member which is a json string 如何在工厂方法中初始化具有多个参数的构造函数的类 - How to initialize the classes in factory method which have constructors with multiple parameters 方法有8个参数,大于7个已授权 - Method has 8 parameters, which is greater than 7 authorized 将具有json-format-string的对象序列化为json - Serialize an object which has a json-format-string to json 如何运行以“KeyEvent e”为参数的方法? - How to run a method which has "KeyEvent e" as it's parameter? 如何使用方法显示对象的参数 - How do I display my Object's parameters using a method 如何重构这个有多个if / else语句的方法 - how to refactor this method which has multiple if/else statements 如何在规范方法中使用多个参数过滤 object? - How to filter an object using multiple parameters in the specification method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM