我想加载我的类Floyd并使用它的方法FW,但是Intellij write无法解析符号a。我在Floyd类中写了“import.io *”和“implements Serializable”
import java.io.*;
public class jj {
public void smth() {
Floyd fw = new Floyd();
try {
FileOutputStream fs = new FileOutputStream("Floyd.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(fw);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
try{
ObjectInputStream osNew = new ObjectInputStream(new FileInputStream("Floyd.ser"));
Floyd a = (Floyd) osNew.readObject();
osNew.close();
} catch (Exception ex1){
ex1.printStackTrace();
}
a.FW();
}
}
在第二个try语句中移动a.FW()
try
{
ObjectInputStream osNew = new ObjectInputStream(new FileInputStream("Floyd.ser"));
Floyd a = (Floyd) osNew.readObject();
a.FW()
osNew.close();
} catch (Exception ex1){
ex1.printStackTrace();
}
你已经在try块中初始化了变量,因此osNew
的可见性不会超出try块,这样IDE就可以了解cannot resolve symbol a