简体   繁体   English

一个对象如何从另一个类中创建的对象中调用方法

[英]How can one object call methods from an object created in another class

I'm new to Java (and programming in general), and I'm trying to write a program that will play Go Fish, but I'm having a hard time getting one object to call another objects methods. 我对Java(以及一般而言的编程)不熟悉,并且试图编写一个可以播放Go Fish的程序,但是我很难让一个对象调用另一个对象方法。 It's not finished yet, but here's what I have so far. 尚未完成,但这是我到目前为止的内容。 This will be called to start a game: 这将被称为开始游戏:

public class StartGame {

public static void start(){
    Deck deck = new Deck(1);
    GoFishHumanPlayer player = new GoFishHumanPlayer();
    GoFishAiPlayer ai = new GoFishAiPlayer();
    player.drawHand();
    ai.drawHand();
    player.turn();
}
}

An object that functions as the games deck. 用作游戏甲板的对象。

import java.util.Random;

public class Deck {

private int[] card = new int[14];
private int size;

Random r = new Random();

public Deck(int x){
    size = x;
    card[0] = 4 * size * 13;
    card[1] = 4 * size;
    card[2] = 4 * size;
    card[3] = 4 * size;
    card[4] = 4 * size;
    card[5] = 4 * size;
    card[6] = 4 * size;
    card[7] = 4 * size;
    card[8] = 4 * size;
    card[9] = 4 * size;
    card[10] = 4 * size;
    card[11] = 4 * size;
    card[12] = 4 * size;
    card[13] = 4 * size;}

public int draw(){
    int x = r.nextInt(card[0]) + 1;
    int y = 1;
    while(x>card[y]){
        x -=card[y];
        y++;}
    card[y]--;
    card[0]--;
    return y;}

}

This next class will be extended by GoFishHumanPlayer and GoFishAiPlayer. GoFishHumanPlayer和GoFishAiPlayer将扩展下一个类。 These objects will represent the players. 这些对象将代表玩家。

public class GoFishPlayer {

private int[] hand = new  int[14];
private int pairs;

public void drawHand(){
    for(int x = 0; x < 5; x++){
        int y = deck.draw();//Here's where I get an error "deck cannot be resolved"
        hand[y]++;}
}
}

On line 8 of GoFishPlayer my IDE says "deck cannot be resolved". 在GoFishPlayer的第8行中,我的IDE表示“无法解决甲板”。 Back in StartGame I created an object called deck, but how do I make it accessible to all classes. 回到StartGame,我创建了一个名为eck的对象,但是如何使所有类都可以访问它。

No need to make it accessible to all classes, simply pass a reference in the constructor of GoFishPlayer : 无需使其对所有类都可访问,只需在GoFishPlayer的构造函数中传递一个引用:

public class GoFishPlayer {
  private Deck deck;

  public GoFishPlayer(Deck deck) { this.deck = deck; }
  ...
}

public class StartGame {
  public static void start(){
    Deck deck = new Deck(1);
    GoFishHumanPlayer player = new GoFishHumanPlayer(deck);
    GoFishAiPlayer ai = new GoFishAiPlayer(deck);
    player.drawHand();
    ai.drawHand();
    player.turn();
  }
}

Simply pass it to the constructor if U need it to be accessible to all the classes . 如果U要求所有类都可以访问它,只需将其传递给构造函数。

public class GoFishPlayer {
private Deck deck;

public GoFishPlayer(Deck deck) { this.deck = deck; }
...
}

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

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