如何从不同的类访问变量

时间:2013-10-20 19:54:37

标签: java class methods

我正在尝试访问WordSelect类中的变量,然后我尝试在Game类中使用它。代码如下: WordSelect类:

import static java.lang.System.*;
import java.util.*;

public class WordSelect {

    public WordSelect(){

    }

    public void Start(int diff){
        String words[] = new String[26];
        switch(diff){
        case 1:
            words[0] = "cat";
            words[1] = "dog";
            words[2] = "book";          
            words[3] = "breakfeast";          
            words[4] = "telephone";          
            words[5] = "mixture";          
            words[6] = "music";          
            words[7] = "animal";          
            words[8] = "school";          
            words[9] = "plant";          
            words[10] = "pen";          
            words[11] = "pencil";          
            words[12] = "paper";          
            words[13] = "note";          
            words[14] = "fog";          
            words[15] = "smoke";        
            words[16] = "bake";          
            words[17] = "alone";          
            words[18] = "drive";          
            words[19] = "town";          
            words[20] = "city";          
            words[21] = "sunny";          
            words[22] = "shine";          
            words[23] = "polish";          
            words[24] = "cap";          
            words[25] = "hat";
            break;

        case 2:
            words[0] = "president";
            words[1] = "exclamation";          
            words[2] = "statement";          
            words[3] = "television";          
            words[4] = "physics";          
            words[5] = "algebra";          
            words[6] = "geometry";          
            words[7] = "difficult";          
            words[8] = "extreme";          
            words[9] = "procedure";          
            words[10] = "ship";          
            words[11] = "soldier";          
            words[12] = "lunch";          
            words[13] = "hockey";          
            words[14] = "tennis";          
            words[15] = "soccer";          
            words[16] = "football";          
            words[17] = "basketball";          
            words[18] = "bias";          
            words[19] = "magazine";          
            words[20] = "computer";          
            words[21] = "internet";          
            words[22] = "allegedly";          
            words[23] = "system";          
            words[24] = "unison";          
            words[25] = "excited";         
            break;

        case 3:
            words[0] = "amalgamation";          
            words[1] = "proclomation";          
            words[2] = "establishment";          
            words[3] = "rehabilitation";          
            words[4] = "rhinoceros";          
            words[5] = "velociraptor";         
            words[6] = "declaration";         
            words[7] = "announcement";          
            words[8] = "binomial";          
            words[9] = "polynomial";          
            words[10] = "congregation";          
            words[11] = "obligation";          
            words[12] = "structure";          
            words[13] = "description";          
            words[14] = "perscription";          
            words[15] = "subscribe";          
            words[16] = "address";          
            words[17] = "township";          
            words[18] = "mischievous";          
            words[19] = "bewildered";          
            words[20] = "accusation";          
            words[21] = "designation";          
            words[22] = "disgusting";          
            words[23] = "prolonged";          
            words[24] = "restoration";          
            words[25] = "regeneration";          
        }

        int i = words.length;

        Random rng = new Random();
        int choice = rng.nextInt(words.length); //Varible storing random word
        String wd = words[choice];
        // Not sure what to put here to make wd available in the other class
     }
}

游戏类:

import static java.lang.System.*;
import java.util.*;

public class Game {

    public static void game(){
        out.println(wd); //Trying to print out the wd variable from WordSelect

    }
}

5 个答案:

答案 0 :(得分:1)

您可以将变量更改为static variable,因为在这种情况下,我认为wd变量与您的类中的任何对象无关。因此,static variable必须符合您的需求。然后,您只需以Classname.variable的形式使用它。

WordSelect课程中:

public class WordSelect {
public static String wd = words[choice];
public WordSelect(){
    ....
    ....
}

public void Start(int diff){
    ....
    ....
}

然后,在GameCalss

public class Game {

public static void game(){
    system.out.println(WordSelect.wd);  

}
}

注意: static variable应在任何方法之外定义。

答案 1 :(得分:0)

你能做的最好的事情是为你的班级宣布一个ivar。在这里,我称之为wordSelection

然后在构造函数方法中,您可以为ivar指定初始值(空)。

此外,您必须添加所谓的“存取方法”才能读取ivar。在这里,我称之为getWordSelection

Game课程中,您需要instance课程WordSelection。您可以使用new关键字来完成此操作。请参阅代码以获取示例。

最后,我在代码中的任何位置都没有看到main方法。您需要一个能够运行和测试您的程序。

import static java.lang.System.*;
import java.util.*;

public class WordSelect {

    // Add a priate ivar here...
    private String wordSelection;

    public WordSelect(){
        // give the ivar an initial value
        this.wordSelection = "";
    }

    // Add the method to access the ivar...
    public String getWordSelection() 
    {
        // this will return your saved word
        return wordSelection;
    }

    public void Start(int diff){
        String words[] = new String[26];
        switch(diff){
            // Your switch code here
            // I removed the switch code to keep the answer short
        }

        int i = words.length;

        Random rng = new Random();
        int choice = rng.nextInt(words.length); //Varible storing random word

        // This is what you have to do
        wordSelection = words[choice];
     }
}

游戏类:

import static java.lang.System.*;
import java.util.*;

public class Game {

    public static void game(){

        // This is how you instantiate your WordSelect class
        WordSelect wordSelect = new WordSelect();

        // call the "Start" method
        wordSelect.Start(30);

        // This is how you can access the ivar
        out.println(wordSelect.getWordSelection()); 

    }
}

希望这有帮助!

答案 2 :(得分:0)

你必须要做一些事情。首先,您需要创建一个变量来保存WordSelect类中的String,如下所示:

String wd;

接下来你需要一个返回字符串的return方法:

public String returnWord()
{
     return wd;
} 

现在我们需要在你的主要做一些事情。首先我们需要创建一个WordSelect对象,然后我们需要调用Start方法,然后我们需要调用returnWord方法并将其分配给一个字符串。

WordSelect words = new WordSelect();

words.Start(1);

String str = words.returnWord();

System.out.println(str);

答案 3 :(得分:0)

你可以通过以下方式实现这一目标:

import static java.lang.System.*;
import java.util.*;

public class WordSelect {
public WordSelect(){

}

private String wd = "";

public void Start(int diff){
    String words[] = new String[26];
    switch(diff){
    case 1:
      words[0] = "cat";
      words[1] = "dog";
      words[2] = "book";          
      words[3] = "breakfeast";          
      words[4] = "telephone";          
      words[5] = "mixture";          
      words[6] = "music";          
      words[7] = "animal";          
      words[8] = "school";          
      words[9] = "plant";          
      words[10] = "pen";          
      words[11] = "pencil";          
      words[12] = "paper";          
      words[13] = "note";          
      words[14] = "fog";          
      words[15] = "smoke";        
      words[16] = "bake";          
      words[17] = "alone";          
      words[18] = "drive";          
      words[19] = "town";          
      words[20] = "city";          
      words[21] = "sunny";          
      words[22] = "shine";          
      words[23] = "polish";          
      words[24] = "cap";          
      words[25] = "hat";

      break;
    case 2:
      words[0] = "president";
      words[1] = "exclamation";          
      words[2] = "statement";          
      words[3] = "television";          
      words[4] = "physics";          
      words[5] = "algebra";          
      words[6] = "geometry";          
      words[7] = "difficult";          
      words[8] = "extreme";          
      words[9] = "procedure";          
      words[10] = "ship";          
      words[11] = "soldier";          
      words[12] = "lunch";          
      words[13] = "hockey";          
      words[14] = "tennis";          
      words[15] = "soccer";          
      words[16] = "football";          
      words[17] = "basketball";          
      words[18] = "bias";          
      words[19] = "magazine";          
      words[20] = "computer";          
      words[21] = "internet";          
      words[22] = "allegedly";          
      words[23] = "system";          
      words[24] = "unison";          
      words[25] = "excited";         
      break;
    case 3:
      words[0] = "amalgamation";          
      words[1] = "proclomation";          
      words[2] = "establishment";          
      words[3] = "rehabilitation";          
      words[4] = "rhinoceros";          
      words[5] = "velociraptor";         
      words[6] = "declaration";         
      words[7] = "announcement";          
      words[8] = "binomial";          
      words[9] = "polynomial";          
      words[10] = "congregation";          
      words[11] = "obligation";          
      words[12] = "structure";          
      words[13] = "description";          
      words[14] = "perscription";          
      words[15] = "subscribe";          
      words[16] = "address";          
      words[17] = "township";          
      words[18] = "mischievous";          
      words[19] = "bewildered";          
      words[20] = "accusation";          
      words[21] = "designation";          
      words[22] = "disgusting";          
      words[23] = "prolonged";          
      words[24] = "restoration";          
      words[25] = "regeneration";          
  }

int i = words.length;

Random rng = new Random();
int choice = rng.nextInt(words.length); //Varible storing random word
this.wd = words[choice];

}

//// setter and getter of wd;

public void setWd(String wd) {
this.wd = wd;
}

public String setWd() {
return this.wd;
}

}

Game.java

import static java.lang.System.*;
import java.util.*;
public class Game {

public static void game(){

    WordSelect ws = new WordSelect();
    out.println(ws.getWd());

}
}

答案 4 :(得分:0)

有几种方法可以解决这个问题。

1您可以更改start方法以返回String。

public String start(int diff){
...
String wd = words[choice];
return wd;
}

然后

WordSelect ws = new WordSelect();
out.println(ws.start());

注意我将Start方法更改为start,方法应该从小写开始。

2您可以将其保存为WordSelect

中的类属性
public class WordSelect {
private String word;
...
public String start(int diff){
...
String wd = words[choice];
word = wd;
}
...
public String getWord(){
return word;
}
}

然后

WordSelect ws = new WordSelect();
ws.start();
out.println(ws.getWord());

3您也可以尝试将属性保存为公共属性而不是getter。不建议这样做,因为你正在失去OO封装的最佳功能之一。

4您也可以将其保存为所提到的静态变量,但这些应保持在最低限度。静态变量有它们的位置,例如常量,但它们并不是真正的OO方式。请记住,静态变量将应用于同一类的所有对象。 OO的主要好处之一是您可以拥有来自同一类的不同对象,每个对象具有不同的“状态”。

我想我会设计这些类有点不同。为什么每次想要获得随机字时都要初始化数组?在其他地方做这种事情可能会更好,可能在构造函数中,然后使用单个方法来获取随机单词。将数组保持为该类的私有,以便封装它。这样,您可以在将来轻松更改它以使用ArrayList或其他任何内容。