如何在另一个 class 中使用来自 'main' 方法的变量?连载
How to use variables from 'main' method in another class? Serialization
这里是新手!我在 Java 中与 Save/Load 一起工作,我被踩了。我的代码正确地保存和加载了所有内容,但我无法在我的加载 class 中让这些加载的变量在 'public static void main' 之外的任何地方使用。我的意思是:
我有这个储蓄 class,非常好用:
package com.mestru.esport;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
public class NewPlayer {
public static void main (String args[])
{
Random rand = new Random();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
try
{
FileOutputStream saveFile = new FileOutputStream("Player.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(naturalTalent);
save.writeObject(luck);
save.writeObject(figure);
save.writeObject(agility);
save.writeObject(intelligence);
save.writeObject(multiTasking);
save.writeObject(stamina);
save.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
然后我有 class,它被设计用来加载并使加载的变量在其他 classes 中可用。如果加载部分工作完美,那么我就不能在 'main' 方法之外使用它们。
package com.mestru.esport;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Random;
public class Player
{
int naturalTalent;
int luck;
int figure;
int agility;
int intelligence;
int multiTasking;
int stamina;
public static void main (String args[]) {
int NEWnaturalTalent = 0;
int NEWluck = 0;
int NEWfigure = 0;
int NEWagility = 0;
int NEWintelligence = 0;
int NEWmultiTasking = 0;
int NEWstamina = 0;
try {
FileInputStream saveFile = new FileInputStream("Player.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);
NEWnaturalTalent = (Integer) save.readObject();
NEWluck = (Integer) save.readObject();
NEWfigure = (Integer) save.readObject();
NEWagility = (Integer) save.readObject();
NEWintelligence = (Integer) save.readObject();
NEWmultiTasking = (Integer) save.readObject();
NEWstamina = (Integer) save.readObject();
save.close();
} catch (Exception exc) {
exc.printStackTrace();
}
Player player = new Player();
player.naturalTalent = NEWnaturalTalent;
player.luck = NEWluck;
player.figure = NEWfigure;
player.agility = NEWagility;
player.intelligence = NEWintelligence;
player.multiTasking = NEWmultiTasking;
player.stamina = NEWstamina;
}
public void test ()
{
System.out.println("Natural Talent: " + naturalTalent);
System.out.println("Luck: " + luck);
System.out.println("Figure: " + figure);
System.out.println("Agility: " + agility);
System.out.println("Intelligence: " + intelligence);
System.out.println("Multitasking: " + multiTasking);
System.out.println("Stamina: " + stamina);
}
}
当我在另一个 class 中使用方法 'test' 时,所有值都等于 0。
所以这是我的问题。我能否以某种方式使所有实例变量等于 'main' 方法中加载的实例变量?我也不能在 'main' 方法之外使用 "try" :(
在此先感谢您的帮助<3 感谢您提供每条线索,我才刚刚开始学习。
加载时,您创建一个新的播放器,设置它然后丢弃它。
也许您打算设置当前玩家的字段。
你也不需要临时变量。您可以只设置字段。
public static void main(String... ignored) throws IOException {
Player player = new Player();
player.load("Player.sav");
player.test();
}
public void load(String filename) throws IOException {
try (FileInputStream saveFile = new FileInputStream(filename);
ObjectInputStream save = new ObjectInputStream(saveFile)) {
naturalTalent = (Integer) save.readObject();
luck = (Integer) save.readObject();
figure = (Integer) save.readObject();
agility = (Integer) save.readObject();
intelligence = (Integer) save.readObject();
multiTasking = (Integer) save.readObject();
stamina = (Integer) save.readObject();
}
}
您可以将其写为文本,而不是以二进制形式写入此数据。这将使 read/edit 更容易,并且在此用例中它会更紧凑。即序列化一个 Integer
对象可能会大得惊人。
主要的 JavaCode Class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Integer> arl = new ArrayList<Integer>();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
String fileName = "/Users/rk4262/Player.txt";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("naturalTalent value is :" + naturalTalent);
bufferedWriter.newLine();
bufferedWriter.write("luck value is : " + luck);
bufferedWriter.newLine();
bufferedWriter.write("figure value is : " + figure);
bufferedWriter.newLine();
bufferedWriter.write("agility value is : " + agility);
bufferedWriter.newLine();
bufferedWriter.write("intelligence value is : " + intelligence);
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Player p = new Player();
String s = p.readLine();
System.out.println(s);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(s);
while (m.find()) {
int n = Integer.parseInt(m.group());
arl.add(n);
}
p.NEWnaturalTalent = arl.indexOf(0);
p.NEWluck=arl.indexOf(1);
p.NEWfigure=arl.indexOf(2);
p.NEWagility=arl.indexOf(3);
}
}
玩家代码Class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Integer> arl = new ArrayList<Integer>();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
String fileName = "/Users/rk4262/Player.txt";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("naturalTalent value is :" + naturalTalent);
bufferedWriter.newLine();
bufferedWriter.write("luck value is : " + luck);
bufferedWriter.newLine();
bufferedWriter.write("figure value is : " + figure);
bufferedWriter.newLine();
bufferedWriter.write("agility value is : " + agility);
bufferedWriter.newLine();
bufferedWriter.write("intelligence value is : " + intelligence);
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Player p = new Player();
String s = p.readLine();
System.out.println(s);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(s);
while (m.find()) {
int n = Integer.parseInt(m.group());
arl.add(n);
}
p.NEWnaturalTalent = arl.indexOf(0);
p.NEWluck=arl.indexOf(1);
p.NEWfigure=arl.indexOf(2);
p.NEWagility=arl.indexOf(3);
}
}
这里是新手!我在 Java 中与 Save/Load 一起工作,我被踩了。我的代码正确地保存和加载了所有内容,但我无法在我的加载 class 中让这些加载的变量在 'public static void main' 之外的任何地方使用。我的意思是:
我有这个储蓄 class,非常好用:
package com.mestru.esport;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
public class NewPlayer {
public static void main (String args[])
{
Random rand = new Random();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
try
{
FileOutputStream saveFile = new FileOutputStream("Player.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(naturalTalent);
save.writeObject(luck);
save.writeObject(figure);
save.writeObject(agility);
save.writeObject(intelligence);
save.writeObject(multiTasking);
save.writeObject(stamina);
save.close();
}
catch(Exception exc)
{
exc.printStackTrace();
}
}
}
然后我有 class,它被设计用来加载并使加载的变量在其他 classes 中可用。如果加载部分工作完美,那么我就不能在 'main' 方法之外使用它们。
package com.mestru.esport;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Random;
public class Player
{
int naturalTalent;
int luck;
int figure;
int agility;
int intelligence;
int multiTasking;
int stamina;
public static void main (String args[]) {
int NEWnaturalTalent = 0;
int NEWluck = 0;
int NEWfigure = 0;
int NEWagility = 0;
int NEWintelligence = 0;
int NEWmultiTasking = 0;
int NEWstamina = 0;
try {
FileInputStream saveFile = new FileInputStream("Player.sav");
ObjectInputStream save = new ObjectInputStream(saveFile);
NEWnaturalTalent = (Integer) save.readObject();
NEWluck = (Integer) save.readObject();
NEWfigure = (Integer) save.readObject();
NEWagility = (Integer) save.readObject();
NEWintelligence = (Integer) save.readObject();
NEWmultiTasking = (Integer) save.readObject();
NEWstamina = (Integer) save.readObject();
save.close();
} catch (Exception exc) {
exc.printStackTrace();
}
Player player = new Player();
player.naturalTalent = NEWnaturalTalent;
player.luck = NEWluck;
player.figure = NEWfigure;
player.agility = NEWagility;
player.intelligence = NEWintelligence;
player.multiTasking = NEWmultiTasking;
player.stamina = NEWstamina;
}
public void test ()
{
System.out.println("Natural Talent: " + naturalTalent);
System.out.println("Luck: " + luck);
System.out.println("Figure: " + figure);
System.out.println("Agility: " + agility);
System.out.println("Intelligence: " + intelligence);
System.out.println("Multitasking: " + multiTasking);
System.out.println("Stamina: " + stamina);
}
}
当我在另一个 class 中使用方法 'test' 时,所有值都等于 0。
所以这是我的问题。我能否以某种方式使所有实例变量等于 'main' 方法中加载的实例变量?我也不能在 'main' 方法之外使用 "try" :(
在此先感谢您的帮助<3 感谢您提供每条线索,我才刚刚开始学习。
加载时,您创建一个新的播放器,设置它然后丢弃它。
也许您打算设置当前玩家的字段。
你也不需要临时变量。您可以只设置字段。
public static void main(String... ignored) throws IOException {
Player player = new Player();
player.load("Player.sav");
player.test();
}
public void load(String filename) throws IOException {
try (FileInputStream saveFile = new FileInputStream(filename);
ObjectInputStream save = new ObjectInputStream(saveFile)) {
naturalTalent = (Integer) save.readObject();
luck = (Integer) save.readObject();
figure = (Integer) save.readObject();
agility = (Integer) save.readObject();
intelligence = (Integer) save.readObject();
multiTasking = (Integer) save.readObject();
stamina = (Integer) save.readObject();
}
}
您可以将其写为文本,而不是以二进制形式写入此数据。这将使 read/edit 更容易,并且在此用例中它会更紧凑。即序列化一个 Integer
对象可能会大得惊人。
主要的 JavaCode Class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Integer> arl = new ArrayList<Integer>();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
String fileName = "/Users/rk4262/Player.txt";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("naturalTalent value is :" + naturalTalent);
bufferedWriter.newLine();
bufferedWriter.write("luck value is : " + luck);
bufferedWriter.newLine();
bufferedWriter.write("figure value is : " + figure);
bufferedWriter.newLine();
bufferedWriter.write("agility value is : " + agility);
bufferedWriter.newLine();
bufferedWriter.write("intelligence value is : " + intelligence);
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Player p = new Player();
String s = p.readLine();
System.out.println(s);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(s);
while (m.find()) {
int n = Integer.parseInt(m.group());
arl.add(n);
}
p.NEWnaturalTalent = arl.indexOf(0);
p.NEWluck=arl.indexOf(1);
p.NEWfigure=arl.indexOf(2);
p.NEWagility=arl.indexOf(3);
}
}
玩家代码Class
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestProgram {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Random rand = new Random();
ArrayList<Integer> arl = new ArrayList<Integer>();
int naturalTalent = rand.nextInt(10);
int luck = rand.nextInt(20);
int figure = rand.nextInt(50);
int agility = rand.nextInt(30);
int intelligence = rand.nextInt(30);
int multiTasking = rand.nextInt(30);
int stamina = rand.nextInt(30);
String fileName = "/Users/rk4262/Player.txt";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("naturalTalent value is :" + naturalTalent);
bufferedWriter.newLine();
bufferedWriter.write("luck value is : " + luck);
bufferedWriter.newLine();
bufferedWriter.write("figure value is : " + figure);
bufferedWriter.newLine();
bufferedWriter.write("agility value is : " + agility);
bufferedWriter.newLine();
bufferedWriter.write("intelligence value is : " + intelligence);
bufferedWriter.newLine();
// Always close files.
bufferedWriter.close();
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
Player p = new Player();
String s = p.readLine();
System.out.println(s);
Pattern pattern = Pattern.compile("[0-9]+");
Matcher m = pattern.matcher(s);
while (m.find()) {
int n = Integer.parseInt(m.group());
arl.add(n);
}
p.NEWnaturalTalent = arl.indexOf(0);
p.NEWluck=arl.indexOf(1);
p.NEWfigure=arl.indexOf(2);
p.NEWagility=arl.indexOf(3);
}
}