Java GuessAnimal不会序列化
我已经在这方面做了很长时间了,不明白为什么它不能序列化我的数据。游戏是为了存储用户输入的问题和答案,当它得到它,所以下次游戏打开时,它将有这些问题提出。。。例如,动物会飞吗。。。不输入一个有助于我下次猜的问题。。。它不会飞。。它是什么?猴子。。。。好吧,下次游戏开始时。。。它会问自己是否会飞,是否是猴子。但问题是,我似乎无法把这些放在一起 如果我能很快得到帮助,我会非常感激。谢谢Java GuessAnimal不会序列化,java,Java,我已经在这方面做了很长时间了,不明白为什么它不能序列化我的数据。游戏是为了存储用户输入的问题和答案,当它得到它,所以下次游戏打开时,它将有这些问题提出。。。例如,动物会飞吗。。。不输入一个有助于我下次猜的问题。。。它不会飞。。它是什么?猴子。。。。好吧,下次游戏开始时。。。它会问自己是否会飞,是否是猴子。但问题是,我似乎无法把这些放在一起 如果我能很快得到帮助,我会非常感激。谢谢 //GuessTheAnimal Class-----------------------------------
//GuessTheAnimal Class-----------------------------------
import javax.swing.JOptionPane;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class GuessTheAnimal implements Serializable
{
public static void main(String[] args)
{
Tree animal = new Tree();
animal.instruction();
animal.play();
animal.writeToFile("treeData.ser");
Tree newAnimal = Tree.readFromFile("treeData.ser");
animal.play();
}
}
//Tree Class-----------------------------------------
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.swing.JOptionPane;
public class Tree
//Tree class
{
private Node root;
//constructor
public Tree()
{ root = new Node();
root.leftChild = new Node();
root.rightChild = new Node();
root.questionText = "Does it live on land?";
root.leftChild.questionText ="bear"; // left side is Yes, right side is No
root.rightChild.questionText = "parrot";
}
public void instruction()
{
JOptionPane.showMessageDialog(null, "Think of an animal, I will try to guess it, answer yes or no");
}
public void play()
{
Node current = root;
Node parent = current;
boolean isLeftChild = true;
while(true)
{ parent = current;
int response = JOptionPane.showConfirmDialog(null,current.questionText );
//code here for yes
if (response == JOptionPane.YES_OPTION)
{
current = current.leftChild;
isLeftChild=true;
}
//code here for no
else if (response == JOptionPane.NO_OPTION)
{
current = current.rightChild;
isLeftChild = false;
}
if (current.leftChild == null && current.rightChild == null)
{
int secondQ = JOptionPane.showConfirmDialog(null, "Is your animal a " + current.questionText + "?");
if (secondQ == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null,"I Guessed your animal!");
return;
}
else if (secondQ == JOptionPane.NO_OPTION)
{
Node nodeOne = new Node();
Node nodeTwo = new Node();
nodeOne.questionText = JOptionPane.showInputDialog("Write a question that differentiates your animal from the animal I guessed, it would be yes for your animal");
nodeTwo.questionText = JOptionPane.showInputDialog("What is this animal?");
nodeOne.rightChild = current;
nodeOne.leftChild = nodeTwo;
// parent.leftChild = nodeOne or parent.rightChild = nodeOne
if(isLeftChild == false)
{
parent.rightChild = nodeOne;
System.out.println("right child");
}
else
{
parent.leftChild = nodeOne;
System.out.println("left Child");
}
return;
}
}
}
}
public void preOrder(Node localRoot)
{
if(localRoot != null)
{
System.out.print(localRoot.questionText + " ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
public Node getRoot(){
return root;
}
public boolean writeToFile(String text) {
try {
FileOutputStream fileOut = new FileOutputStream("treeData.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(root);
out.close();
fileOut.close();
return true;
} catch (java.io.IOException e) {
e.printStackTrace();
System.out.println("-----problem in writeToFile()--------");
return false;
}
}
/** @return tree read from file, if successful input, else null */
public static Tree readFromFile(String fileName) {
try {
FileInputStream fileIn = new FileInputStream("treeData.ser");
ObjectInputStream objIn = new ObjectInputStream(fileIn);
Tree newAnimal = (Tree) objIn.readObject();
objIn.close();
fileIn.close();
return newAnimal;
}
catch (Exception e){//Catch exception if any
e.printStackTrace();
System.out.println("-----problem in readFromFile()--------");
}
return null;
}
}
//Node Class----------------------------------
public class Node {
//Node class
//instance variables
public String questionText;
public Node leftChild;
public Node rightChild;
public void displayText()
{
System.out.println(questionText);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
如果要序列化一个对象,不仅该对象必须是可序列化的,而且它的所有子对象也必须是可序列化的。如果要序列化树,必须将树标记为可序列化。树的组件也必须是可序列化的类,等等,所以您还需要使节点也可序列化。我看不到Node类的声明,但这也将应用于它所持有的任何成员变量。您再次调用的是
animal
,而不是您试图反序列化的实例
Tree animal = new Tree();
animal.instruction();
animal.play();
animal.writeToFile("treeData.ser");
Tree newAnimal = Tree.readFromFile("treeData.ser");
///should the following not be newAnimal?
animal.play();
好的,所以我是一个初学者——但你是说要在我所有的类中实现可序列化,然后我就应该被设置了?实际上我确实做了一些修改,它似乎正在编写,但它不会读回(崩溃)说java.lang.ClassCastException:Node不能在Tree.readFromFile(Tree.java:134)处强制转换到Tree在GuessTheAnimal.main(GuessTheAnimal.java:27)