Java 如何访问另一个类中的方法

Java 如何访问另一个类中的方法,java,Java,我有3个文件 Guesser.java GuessWhoGame.java GuesserTest.java 在Guesser.java中 public class Guesser { /** * Guesses which character you picked */ public static String play(GuessWhoGame g) { if (g.hairIsColor(Color.BROW

我有3个文件

Guesser.java
GuessWhoGame.java
GuesserTest.java
在Guesser.java中

public class Guesser {



       /**
         * Guesses which character you picked
         */
        public static String play(GuessWhoGame g) {
    if (g.hairIsColor(Color.BROWN) && g.shirtIsColor(Color.GREEN)) 
    {
        if (g.eyeIsColor(Color.BLUE))
            return "Alice";
        else if (g.eyeIsColor(Color.GREEN))
            return "Frank";
        else if (g.eyeIsColor(Color.BROWN) && g.isWearingGlasses())
            return "Bob";
        else if (g.eyeIsColor(Color.BROWN))
            return "Dave";
        else
            return "Isabelle";
    }

    if (g.hairIsColor(Color.BROWN) && g.shirtIsColor(Color.RED)) 
    {
        if (g.eyeIsColor(Color.GREEN))
            return "Philip";
        else if (g.eyeIsColor(Color.BLUE) && !g.isSmiling())
            return "Wendy";
        else if (g.eyeIsColor(Color.BLUE) && g.isWearingGlasses()) 
            return "Mallie";
        else if (g.eyeIsColor(Color.BLUE))
            return "Nick";
        else if (g.eyeIsColor(Color.BROWN) && g.isWearingHat())
            return "Robert";
        else if (g.eyeIsColor(Color.BROWN) && g.isSmiling())
            return "Quinn";
        else if(g.eyeIsColor(Color.HAZEL))
            return "Emily";
    }



    else if (g.hairIsColor(Color.BLACK) && g.shirtIsColor(Color.BLUE)) {
        if (!g.isSmiling())
            return "Carol";
        else if (g.isWearingHat())
            return "Gertrude";
        else
            return "Olivia";
    }


    if (g.hairIsColor(Color.BROWN))
    {
        if (g.eyeIsColor(Color.BLUE))
        return "Tucker";
      else if (g.eyeIsColor(Color.BROWN))
        return "Zander";
    }
      if (g.hairIsColor(Color.BLOND))
    {  
       if(g.shirtIsColor(Color.RED))
        return "Henry";
      else if(g.shirtIsColor(Color.BLUE))
        return "Jack";
    }

     if (g.hairIsColor(Color.BLACK))
    { 
      if(g.eyeIsColor(Color.HAZEL))
        return "Karen";
    else if(g.isWearingHat())
        return "Xavier";
    else if(g.eyeIsColor(Color.BROWN))
        return "Ursula";
    }

    if (g.hairIsColor(Color.RED))
    {
     if(g.shirtIsColor(Color.GREEN))
        return "Yasmine";
    else if (g.eyeIsColor(Color.BLUE))
        return "Larry";
    else if (g.isWearingHat())
        return "Sarah";
    else if (g.isSmiling())
        return "Victor";
    }     
            return null;
        }

        /**
         * TODO documentation for the main method
         */
        public static void main(String[] args) 
        {
         }
    }
在GuessWhoGame.java中,我有

import java.util.Random;

/**
 * Implements the Guess Who game.
 * Picks a secret character and counts the number of questions asked.
 * 
 * @author pritchey
 * @version 2014-07-17
 */

public class GuessWhoGame {
    /**
     * secret character
     */
    private Character secret;

    /**
     * number of questions asked
     */
    private int numQuestions;

    /**
     * array of secret characters
     */
    private static final Character[] characters = new Character[] {
        new Character("Alice",    Color.BROWN, Color.BLUE,  Color.GREEN, true,  true,  false),
        new Character("Bob",      Color.BROWN, Color.BROWN, Color.GREEN, true,  false, true),
        new Character("Carol",    Color.BLACK, Color.BLUE,  Color.BLUE,  true,  false, false),
        new Character("Dave",     Color.BROWN, Color.BROWN, Color.GREEN, false, true,  true),
        new Character("Emily",    Color.BROWN, Color.HAZEL, Color.RED,   true,  true,  true),
        new Character("Frank",    Color.BROWN, Color.GREEN, Color.GREEN, true,  true,  false),
        new Character("Gertrude", Color.BLACK, Color.BLUE,  Color.BLUE,  true,  true,  true),
        new Character("Henry",    Color.BLOND, Color.BROWN, Color.RED,   true,  true,  false),
        new Character("Isabelle", Color.BROWN, Color.HAZEL, Color.GREEN, true,  true,  false),
        new Character("Jack",     Color.BLOND, Color.BROWN, Color.BLUE,  false, true,  false),
        new Character("Karen",    Color.BLACK, Color.HAZEL, Color.GREEN, false, true,  false),
        new Character("Larry",    Color.RED,   Color.BLUE,  Color.BLUE,  true,  false, false),
        new Character("Mallie",   Color.BROWN, Color.BLUE,  Color.RED,   true,  true,  false),
        new Character("Nick",     Color.BROWN, Color.BLUE,  Color.RED,   false, true,  false),
        new Character("Olivia",   Color.BLACK, Color.BROWN, Color.BLUE,  false, true,  false),
        new Character("Philip",   Color.BROWN, Color.GREEN, Color.RED,   false, true,  false),
        new Character("Quinn",    Color.BROWN, Color.BROWN, Color.RED,   false, true,  false),
        new Character("Robert",   Color.BROWN, Color.BROWN, Color.RED,   false, true,  true),
        new Character("Sarah",    Color.RED,   Color.BROWN, Color.BLUE,  true,  true,  true),
        new Character("Tucker",   Color.BROWN, Color.BLUE,  Color.BLUE,  false, true,  false),
        new Character("Ursula",   Color.BLACK, Color.BROWN, Color.GREEN, false, true,  false),
        new Character("Victor",   Color.RED,   Color.BROWN, Color.BLUE,  true,  true,  false),
        new Character("Wendy",    Color.BROWN, Color.BLUE,  Color.RED,   true,  false, false),
        new Character("Xavier",   Color.BLACK, Color.BROWN, Color.GREEN, true,  true,  true),
        new Character("Yasmine",  Color.RED,   Color.BLUE,  Color.GREEN, true,  true,  false),
        new Character("Zander",   Color.BROWN, Color.BROWN, Color.BLUE,  false, true,  false)
    };

    /**
     * Class to represent a Guess Who character
     * @author pritchey
     * @version 2014-07-17
     */
    private static class Character {
        /**
         * hair color
         */
        private Color hair;
        /**
         * eye color
         */
        private Color eyes;
        /**
         * shirt color
         */
        private Color shirt;
        /**
         * wears glasses?
         */
        private boolean glasses;
        /**
         * is smiling?
         */
        private boolean smiling;
        /**
         * wearing a hat?
         */
        private boolean hat;
        /**
         * character's name
         */
        private String name;

        /**
         * construct a new character with the specified attributes
         * @param name
         * @param hair
         * @param eyes
         * @param shirt
         * @param glasses
         * @param smiling
         * @param hat
         */
        public Character(String name, Color hair, Color eyes, Color shirt,
                         boolean glasses, boolean smiling, boolean hat) {
            this.hair = hair;
            this.eyes = eyes;
            this.shirt = shirt;
            this.glasses = glasses;
            this.smiling = smiling;
            this.hat = hat;
            this.name = name;
        }

        /**
         * 
         * @return the hair color of the character
         */
        public Color hair() { return this.hair; }
        /**
         * 
         * @return eye color of the character
         */
        public Color eyes() { return this.eyes; }
        /**
         * 
         * @return shirt color of the character
         */
        public Color shirt() { return this.shirt; }
        /**
         * 
         * @return true if character wears glasses
         */
        public boolean glasses() { return this.glasses; }
        /**
         * 
         * @return true if character is smiling
         */
        public boolean smiling() { return this.smiling; }
        /**
         * 
         * @return true if character is wearing a hat
         */
        public boolean hat() { return this.hat; }
        /**
         * 
         * @return the character's name
         */
        public String name() { return this.name; }
    }

    /**
     * select the secret character at random
     */
    public GuessWhoGame() {
        Random random = new Random(System.currentTimeMillis());
        secret = characters[random.nextInt(characters.length)];
        numQuestions = 0;
    }

    /**
     * select the i-th secret character<br>
     * use for JUnit testing
     */
    public GuessWhoGame(int i) {
        secret = characters[i % characters.length];
        numQuestions = 0;
    }

    /**
     * 
     * @param c - Color of hair to ask about
     * @return true if secret chartacter's hair is the specified color
     */
    public boolean hairIsColor(Color c) {
        numQuestions++;
        return secret.hair().equals(c);
    }

    /**
     * 
     * @param c - Color of etrue to ask about
     * @return true if secret character's etrue are the specified color
     */
    public boolean eyeIsColor(Color c) {
        numQuestions++;
        return secret.eyes().equals(c);
    }

    /**
     * 
     * @param c - Color of shirt to ask about
     * @return true if secret character's shirt is the specified color
     */
    public boolean shirtIsColor(Color c) {
        numQuestions++;
        return secret.shirt().equals(c);
    }

    /**
     * 
     * @return true if secret character is wearing glasses
     */
    public boolean isWearingGlasses() {
        numQuestions++;
        return secret.glasses();
    }

    /**
     * 
     * @return true if secret character is smiling
     */
    public boolean isSmiling() {
        numQuestions++;
        return secret.smiling();
    }

    /**
     * 
     * @return true if secret character is wearing a hat
     */
    public boolean isWearingHat() {
        numQuestions++;
        return secret.hat();
    }

    /**
     * method to guess the identity of the secret character
     * @param name - name of character as a String
     * @return true if secret character's name is correct
     */
    public boolean guess(String name) {
        if (secret.name().equalsIgnoreCase(name)) {
            numQuestions++;
            return true;
        } else {
            numQuestions += 11; // penalty for incorrect guess
            System.out.println("it was " + secret.name());
            return false;
        }
    }

    /**
     * 
     * @return the number of questions asked
     */
    public int score() {
        return this.numQuestions;
    }
}
我的问题是在主方法中的Guesser中,我需要实例化GuessWhoGame类,访问play方法,打印猜测是否正确,以及打印GuessWhoGame.class中的分数 在gussertest.java中,我需要测试gusser.play方法。
我该怎么做?我完全被难住了

初始化一个GuessWhoGame对象并调用它的play方法

基本上:

    @Test
    public void testGuesser() {
        GuessWhoGame guessWhoGame = new GuessWhoGame();
        guessWhoGame.play();
        // Do your test checks
    }

这些问题中的一些可以从各种java站点访问,但要让您首先开始,请:

要实例化另一个类,必须执行以下操作

GuessWhoGame gwg = new GuessWhoGame();
GuessWhoGame gwg = new GuessWhoGame(1);
如果需要其他构造函数,请执行以下操作

GuessWhoGame gwg = new GuessWhoGame();
GuessWhoGame gwg = new GuessWhoGame(1);
所以在main方法中执行这一部分,然后可以从main方法中调用GuessWhoGame的方法

还要玩吗

Guesser.play(gwg);
因为您是从静态main执行此操作的,所以不能使用“this”,而必须使用名称猜测器

要获得分数:

 int s = gwg.score();
要打印行,请执行以下操作:

System.out.println("STUFF GOES HERE");
System.out.println("Score:"+s);

我知道我不想读那堵代码墙。提供公共方法来调用,您将能够轻松完成此操作。请不要将您的家庭作业丢在这里,让我们为您完成。你要问的是基本的Java,如果你不知道如何开始,那么你需要回去学习你的课程材料。如果你被卡住了,请提出一个问题并发布你尝试过的代码——你自己的代码,而不是“pritchey”编写的代码。猜测者是我自己的代码,我只是不知道如何访问不在同一文件中的方法。我到处找。另一个代码是给me@Joe通过查看您的代码,看起来您必须说play(gwg)。观察Guesser中的游戏方法。通过查看代码,没有常规的play(),只有一个play(GuessWhoGame)说我想访问GuessWhoGame中的Guess方法和Guess who游戏中的Score方法并打印结果,我该怎么做?我需要储存然后再做?比如int分数=分数(gwg)@乔,首先我想你不明白发生了什么。因此,让我们先看看播放方法。转到Guesser类,查找play方法并查看其参数(或括号中的内容),其中包含什么?现在转到GuessWhoGame并查找score方法,参数(或括号中的东西)中有什么?play想要GuessWhoGame g,所以是GuessWhoGame的g实例?并且score没有参数,返回一个int@Joe对的因此,为了在main方法中使用类猜测器,我们必须这样做。play(g),因为它使用了GuessWhoGame的一个实例。还要注意,“this”关键字表示这个类。所以对于类猜测者来说,它在同一个类中有main方法和play方法,所以你所要做的就是这个。play(g)。但是对于score,它在另一个类中(猜猜whogame),所以您将执行g.score()。因此,存储它将是int s=g.score();