如何在另一个类中使用方法(在Try-Catch中)的变量?

时间:2013-05-16 02:58:22

标签: java oop scope try-catch

所以我正在尝试使用JFrame创建一个基本的RPG用户ID选择菜单;我选择使用单选按钮显示ID选项。一旦用户选择了ID,我就会使用JOptionPane将其打嗝。所有这些都在MyClass类中完成。当用户进行ID选择时,我从.txt文件中读取以检查用户所做的选择,因为每个userID与其统计信息也包含在.txt文件中的Character相关联。我能够分解.txt文件的多行,并将所有不同的特征存储为单独的字符串。我的问题是,现在我想在MyClass之外创建另一个使用JFrame显示所述特征的类,因为.txt文件的分解是在Try-Catch内完成的,所以我无法传递变量已经读过,这些变量只包含在Try-Catch的范围内。这就是我提到的第一堂课中的方法:

public void checkID()
{

    StringBuilder allInfo=null; //Used to store all the Info
    String line=null; //Used as condition for loop & to generate the String Builder
    String strAll2=null;
    String[] sa=null;
    String[] sb=null;
    String[] sd=null;
    String[] se=null;
    BufferedReader br=null;
     //User Id as a string

    try {



        br = new BufferedReader(new FileReader("charList.txt"));
        allInfo= new StringBuilder();

        while ((line=br.readLine()) != null)
        { 
            allInfo.append(line + "\n");
        }

        strAll2=allInfo.toString(); //all the info of charList.txt as one StringBuilder
        sa=strAll2.split("\n"); //all the info of charList.txt as a String
        sb=sa[0].split("\t"); //all the info of the first line of the file as individual strings
        sd=sa[1].split("\t"); //all the info of the second line of the file as individual strings
        se=sa[2].split("\t"); //all the info of the third line of the file as individual strings


        if (firstIdButton.isSelected())
        {
            strId=sb[0];
        }
        else
        {
            if (secondIdButton.isSelected())
        {
            strId=sd[0];
        }
            else
            {
                if (thirdIdButton.isSelected())
                {
                    strId=se[0];
                }
            }
        }

        ID = Integer.parseInt(strId);
    }//end of try 
    catch (IOException e) {
        e.printStackTrace();        

    }//end of catch



    setVisible(false); //Hides Window



}//end of check id method

我在主类中分别将ID和strId声明为public int和String现在,我正在寻找在MyClass之外的另一个类中调用ID的行,并且可能通过几个if-elses查看要显示的字符。问题是,当我实例化构造函数并尝试访问变量时,它返回0,因为try-catch的范围不允许我使用块内获得的值传递它。我还试图让我的方法返回ID的值,但我遇到了MyClass

之外的同样问题

这是我在实施它时遇到的问题

公共类CharacterEditor {

public static void main(String[] args) throws IOException{ 

    MyClass iw= new MyClass();
    int theID=iw.ID;
    System.out.println(theID);

} }

2 个答案:

答案 0 :(得分:1)

你需要分解你的课程和他们的责任。您需要 model 类:代表您的游戏角色的类,以及进行游戏所需计算的类。像

这样的东西
/**
 * A character from the game Toon.
 * @see http://en.wikipedia.org/wiki/Toon_(role-playing_game)
 */
public class Toon implements Serializable {
    private int muscle;
    private int zip;
    private int smarts;
    private int chutzpah;
    private String name;
    private String species;
    // Constructor, getters, setters, etc.
}

您需要自定义视图类:自定义JFrames和Swing ActionListeners。这些不应该用于验证id或从文本文件中读取数据。

你的MyClass(这是一个你想要避免的名字,因为在六个月内你会忘记你想要做的事情),剪掉它的所有JFramesJButtons成为一名控制者。它和兄弟类从文本文件中读取Toons并将它们放入模型类中。它从视图中侦听ActionEvent,尝试执行用户想要的操作,并更新模型和视图,或向视图报告错误并单独保留模型。

答案 1 :(得分:0)

CharacterEditor课程中,您只是实例化MyClass,只是不会调用checkID()并为IDstrId分配正确的值(除非你在构造函数中调用它)

MyClass iw= new MyClass();
iw.checkID(); // MISSING!
int theID=iw.ID;
System.out.println(theID);

如果没有checkID() ID,则默认为0(未初始化的int成员变量的默认值),strId将保留null。你认为try-catch会以某种方式阻止你设置的值在其他地方可见是不正确的。

Try-catch就像任何其他带有大括号的Java构造一样,会创建自己的Scope,并且在范围内声明的任何内容都不会在其外部可见。但是,如果变量已经存在于其范围之外(例如,在您的情况下为ID),则在该范围内对其值进行的任何更改也会反映在其外部。

并且,最好将成员字段声明为private并提供public getter(以及可变属性的setter),以便在其他类中访问它们。另外,与@DaveNewton一样,建议使用else if