繁体   English   中英

如何跨不同的类访问我的私有变量?

[英]How can I access my private variables across different classes?

我正在开发类似子手的游戏。 它从一个包含四个字母词的.txt文件中读取并随机选择一个词,然后播放器将尝试7次猜测该词...我还没有完全完成,我无法从一个词访问变量班级到另一个。 这是我的代码:

package wordguessinggame2;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class WordGuessingGame2 {

    static class RandomWordProvider {

        public final List<String> words;

        public RandomWordProvider() {
        words = readFile();
    }

    public int randomInteger() {
        int randomInt = (int) (Math.random() * words.size());
        return randomInt;
    }

    private String getWord() {
        int randomPosition = randomInteger();
        String randomWord = words.get(randomPosition);
        return randomWord;
    }

    private List<String> readFile() {

        List<String> wordsList = new ArrayList<>();

        try {
            File fourLetterWords = new File(System.getProperty("user.home"),"Documents/FourLetterWords.txt");
            Scanner in = new Scanner(fourLetterWords);

        while (in.hasNextLine()) {
            String line = in.nextLine();
            if (line!=null && !line.isEmpty()) {
                wordsList.add(line);
            }
        }
    } 
    catch (FileNotFoundException ex) {
        System.out.println("File not found.");
    }
    return wordsList ;
    }
}
    public static class PlayerCharacterEntry {

        private String playerEntry() {
            Scanner characterEntry = new Scanner(System.in);
            System.out.print("Enter a character: ");
            String playerInput = characterEntry.next();
            playerInput = playerInput.toUpperCase();
            return playerInput;
    }
}

    public static void main(String[] args) {

        Scanner wantToPlay = new Scanner(System.in);
        System.out.print("Welcome to the word guessing game! Would you like to play? ");
        String playerAnswer = wantToPlay.next();

        if (playerAnswer.equalsIgnoreCase("Yes")) {
            System.out.print("\nYour objective is to guess a four letter word by entering"
            + "\nletters on your keyboard. If you can not guess the word in seven attempts,"
            + "\nyou lose! You will be told if the letter you entered is in the word, and"
            + "\nyou will be told if the letter you entered is not in the word. You will be"
            + "\nallowed to guess the word any time during your seven attempts. If at anytime"
            + "\nyou would like to terminate the game, enter the word 'terminate'. Good Luck!"
            + "\n \n");
    }
        if (playerAnswer.equalsIgnoreCase("No")) {
            System.out.print("Maybe another time!");
            System.exit(0);
    }

RandomWordProvider randomWordProvider = new RandomWordProvider();
PlayerCharacterEntry playerCharacterEntry = new PlayerCharacterEntry();

randomWordProvider.getWord();
playerCharacterEntry.playerEntry();

if (randomWord.containsIgnoreCase(playerInput)){

    }

}
}

在底部,我试图从其他类访问randomWord和playerInput,但是我不知道该怎么做。 我对编程还是很陌生,所以我还不知道该怎么做。 我会为每个变量执行get和set方法吗? 我已经尝试过这样做,但是我遇到了很多麻烦。 如果有人可以告诉我如何跨类访问变量,将不胜感激!

这是一个稍微简化的示例,其中RandomWordProviderPlayerCharacterEntry类未嵌套在WordGuessingGame2 我只显示需要修改的方法:

class RandomWordProvider {   
    String getWord() {
        int randomPosition = randomInteger();
        String randomWord = words.get(randomPosition);
        return randomWord;
    }

    // ...
}

class PlayerCharacterEntry {
    String playerEntry() {
        Scanner characterEntry = new Scanner(System.in);
        System.out.print("Enter a character: ");
        String playerInput = characterEntry.next();
        playerInput = playerInput.toUpperCase();
        return playerInput;
    }
}

class WordGuessingGame2 {
    public static void main(String[] args) {

        // ...

        RandomWordProvider randomWordProvider = new RandomWordProvider();
        PlayerCharacterEntry playerCharacterEntry = new PlayerCharacterEntry();

        randomWordProvider.getWord();
        playerCharacterEntry.playerEntry();
    }
}

请注意,我放弃了private的来自调节剂getWordplayerEntry方法,否则他们是不会从访问WordGuessingGame2

最好从最严格的修饰符开始,然后根据需要减少限制。

不可以,只能从其自身的类内部访问私有变量。 强烈建议您创建公共获取程序和设置程序,以维护OO封装原则。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM