简体   繁体   English

Java将数组传递给构造函数

[英]Java passing an array to the constructor

I am new to Java and encountered this problem: I am learning how to save object state to a file and I got stuck with passing an array to the constructor. 我是Java的新手,遇到了这个问题:我正在学习如何将对象状态保存到文件中,并且无法将数组传递给构造函数。 I believe that the problem is the base class with the constructor but I am not sure. 我相信问题在于构造函数的基类,但我不确定。

here is my Hero class: 这是我的英雄课:

import java.io.Serializable;


public class Hero implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

private int power;
private String type;
private String[] wepons;


public int getPower() {
    return power;
}


public void setPower(int power) {
    this.power = power;
}


public String getType() {
    return type;
}


public void setType(String type) {
    this.type = type;
}


public String[] getWepons() {
    return wepons;
}


public void setWepons(String[] wepons) {
    this.wepons = wepons;
}

public Hero(int powerH, String typeH, String[] weponsH) {
    this.power = powerH;
    this.type = typeH;
    this.wepons = weponsH;
}

}

and here is class which I try to use to save the object state: 这是我尝试用来保存对象状态的类:

import java.io.*;
public class SaveGame {

public static void main(String[] args) {

    Hero hero1 = new Hero(50, "Elf", new String[] {"bow", "short sword", "powder"});


    try{
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
        os.writeObject(hero1);
        os.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    ObjectInputStream is;

    try {
        is = new ObjectInputStream(new FileInputStream("Game.ser"));
        Hero p1N = (Hero) is.readObject();
        System.out.println(p1N.getPower() + " " + p1N.getType() + " " + p1N.getWepons());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

Can you tell me and explain what am I doing wrong. 你能告诉我并解释我做错了什么吗? Do I really need setters and getters in my Hero class and I have the feeling that I am using them incorrectly. 在我的Hero类中,我真的需要二传手和吸气剂吗,我有种使用不正确的感觉。

My problem was that when I tried to print out the Hero's parameters I got content of the array instead of string representation of the array. 我的问题是,当我尝试打印Hero的参数时,我得到了数组的内容,而不是数组的字符串表示形式。 Thanks to user2336315 I know now that i should use Arrays.toString method when printing content of an array 多亏了user2336315,我现在知道在打印数组内容时应该使用Arrays.toString方法

I ran your code and everything seems to be fine. 我运行了您的代码,一切似乎都很好。 The only problem is that you want to print the content of the array itself, not the string representation of the array itself. 唯一的问题是您要打印数组本身的内容,而不是数组本身的字符串表示形式。 So use Arrays.toString : 因此,使用Arrays.toString

System.out.println(p1N.getPower() + " " + p1N.getType() + " " + Arrays.toString(p1N.getWepons()));

Output : 输出:

50 Elf [bow, short sword, powder]

Deserialization mechanism creates the class using its meta data. 反序列化机制使用其元数据创建类。 It does not depend on the access levels of the members of the target class, inclusing constructors. 它不依赖于目标类成员的访问级别,包括构造函数。 (Your code will work even Hero class has private default constructor.) (即使Hero类具有私有默认构造函数,您的代码也可以使用。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM