设置者和获取者到不同的类 [英] Setters And Getters to different Class

查看:74
本文介绍了设置者和获取者到不同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我根本不知道该使用什么代码将值从getX方法传递到其他类main方法.

My problem is that, simply I don't know what code to use to get my value from my getX method to my other classses main method.

package hangman;

公开课的Hang子手{

public class Hangman {

private int triesLimit;
private String word;

public void setTriesLimit(int triesLimit) {
    this.triesLimit = triesLimit;
}

public void setWord(String word) {
    this.word = word;
}

public int getTriesLimit() {
    return this.triesLimit;
}

public String getWord() {
    return this.word;
}


@Override
public String toString() {
    return ("Enter Secret Word " + this.getWord()
            + ".\nEnter max # of tries (Must be under 7) "
            + this.getTriesLimit());
}

}

那是从子类,我试图将trysLimit的值存储到此类的main方法中 子手包;

Thats from the sub-class and I am trying to store the value of the triesLimit into the main of this classes main method package hangman;

public class PlayHangman {

public static void main(String[] args) {
    Hangman hangman = new Hangman();
    Scanner scn = new Scanner(System.in);
    int triesCount = 0;
    int correctCount = 0;
    hangman.toString();
    int triesLimit = hangman.getTriesLimit();
    String secretWord = hangman.getWord();
    StringBuilder b = new StringBuilder(secretWord.length());
    for (int i = 0; i < secretWord.length(); i++) {
        b.append("*");
    }
    char[] secrectStrCharArr = secretWord.toCharArray();
    int charCnt = secretWord.length();
    for (int x = 0; triesCount < triesLimit; triesCount++) {
        while (charCnt >= 0) {
            System.out.println("Secrect Word :" + b.toString());
            System.out.println("Guess a letter :");

            char guessChar = scn.next().toCharArray()[0];
            for (int i = 0; i < secrectStrCharArr.length; i++) {
                if (guessChar == secrectStrCharArr[i]) {
                    b.setCharAt(i, guessChar);
                    correctCount++;
                } else if (guessChar != secrectStrCharArr[i]) {
                    triesCount++;
                    System.out.println("Incorrect: " + triesCount);hangmanImage(triesCount,correctCount);
                }
            }
        }
    }
}

我试图在这里查找它,但是找不到子类/超类中使用的二传手和二传手

I tried looking it up on here but couldn't find setters and getters used in a sub/superclass

推荐答案

您需要在main方法中创建该类的实例,以访问该类中可用的变量和方法,如

You need to create an instance of the class in the main method to access the variables and method available in that class like so

public class PlayHangman {

        public static void main(String[] args) {
       Hangman hangman = new Hangman();
       hangman.setTriesLimit(2)
        int value = hangman.getTriesLimit();

     }

您可以查看static关键字以直接访问该值,但这需要对OOP和JAVA有所了解. 这应该工作正常.

You can look into static keyword to access the value directly but that requires a bit more understanding of OOP's and JAVA. This should work fine.

希望它会有所帮助:)

已编辑

ToString方法只是将模型类中的所有内容都转换为正确完成的String,但是实现不正确....更改ToString的内容

ToString method is just to convert everything in your model class to String which you have done correctly,but you have implemented incorrectly.... Change your ToString content so

@Override
public String toString() {
    return ("The Secret Word you entered: " + this.getWord()
            + ".\n The max # of tries (Must be under 7): "
            + this.getTriesLimit());
}

您已经初始化了Scanner,它可以执行您想要的操作,要求用户输入值,但是您又没有实现它,因此请将其添加到您的主方法中

You have initialized Scanner which does what you want, to ask the user to enter the values but again you haven't implemented it so add this to your main method

    Scanner scn = new Scanner(System.in);
         hangman.setTriesLimit(scn.nextInt());
        hangman.setWord(scn.next());
       hangman.toString()//Will work now

试错是您最好的朋友:) 和Google的某些问题,而不是等待答案:)

Trial and error is your best friend now :) and Google some of the issues rather than waiting for an answer :)

这篇关于设置者和获取者到不同的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆