简体   繁体   English

从另一个班级访问一个班级的成员

[英]Access members of one class from another class

The code: 编码:

import java.util.*;
/*
TicTac Game
__X_|__O_|__X_
__O_|__X_|__O_
  X |  O |  X
*/
public class TicTac{
  public static void main(String[] args) {
    Welcome.greet();
    Game start = new Game();
    start.inputName();
    Welcome.greetPlayer();
    start.show();
  }
}
class Welcome{
  public static void greet(){
    System.out.println("\tTicTac Game By Abhi:");
    System.out.println("\t  __X_|__O_|__X_");
    System.out.println("\t  __O_|__X_|__O_");
    System.out.println("\t    X |  O |  X");
  }
  public static void greetPlayer(){
    Game call = new Game();
    System.out.println("Welcome " + " " + call.x + " and " + call.y + "\n" + "Have Fun!");
  }
}
class Game{
  public String x,y;
  public void inputName(){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your name Player 1:");
    String Player1 = input.nextLine();
    System.out.println("Enter your name Player 2:");
    String Player2 = input.nextLine();
    x = Player1;
    y = Player2;
  }
  public void show(){
    System.out.println("Hi " + " " + x + " and " + y);
  }
}

When I try to call Welcome.greetPlayer() it gives a null value both times. 当我尝试调用Welcome.greetPlayer()时,两次都给出一个空值。 But whenever try to call start.show it give me values of x and y. 但是无论何时尝试调用start.show,它都会给我x和y的值。 I want to access String x and y in welcome class. 我想在欢迎类中访问字符串x和y。

The problem is you are using two different Game objects. 问题是您正在使用两个不同的Game对象。 The first is created in your main() method, the second in the greetPlayer() method. 第一个在您的main()方法中创建,第二个在greetPlayer()方法中创建。 You are initializing the player names only for the object in the main() method. 您仅在main()方法中初始化对象的播放器名称。 They are never initialized in the second object. 它们永远不会在第二个对象中初始化。

I assume you want to use only one Game object. 我假设您只想使用一个Game对象。 One solution would be to pass the Game object to the greetPlayer() method: 一种解决方案是将Game对象传递给greetPlayer()方法:

public static void main(String[] args) {
    Welcome.greet();
    Game start = new Game();
    start.inputName();
    Welcome.greetPlayer(start);
    start.show();
}
public static void greetPlayer(Game call){
    System.out.println("Welcome " + " " + call.x + " and " + call.y + "\n" + "Have Fun!");
}

Another option would be to pass the names directly to the greetPlayer() method: 另一个选择是将名称直接传递给greetPlayer()方法:

public static void main(String[] args) {
    Welcome.greet();
    Game start = new Game();
    start.inputName();
    Welcome.greetPlayer(start.x, start.y);
    start.show();
}
public static void greetPlayer(String player1, String player2){
    System.out.println("Welcome " + " " + player1 + " and " + player2 + "\n" + "Have Fun!");
}

It's because of Game call = new Game(); 这是因为Game call = new Game(); in greetPlayer() greetPlayer()

You have a new instance of Game so your are losing the x and y of your input. 您有一个新的Game实例,因此您丢失了输入的xy

You are creating object of the Game() in two places. 您将在两个地方创建Game()对象。

In main(): 在main()中:

Game start = new Game();

And in greetPlayer() : 并在greetPlayer()

Game call = new Game();

You can try to merge two classes, so you have players introduction, getting names and rest of the game in one class: 您可以尝试合并两个类别,因此可以在一个类别中为玩家提供介绍,获取名称和其余游戏的信息:

import java.util.Scanner;

public class TicTac {
    public static void main(String[] args) {
        Game start = new Game();
        Game.greet();
        start.inputName();
        start.show();
    }
}

class Game {
    public String x, y;

    public void inputName() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your name Player 1:");
        String player1 = input.nextLine();
        System.out.println("Enter your name Player 2:");
        String player2 = input.nextLine();

        x = player1;
        y = player2;
    }

    public static void greet() {
        System.out.println("\tTicTac Game By Abhi:");
        System.out.println("\t  __X_|__O_|__X_");
        System.out.println("\t  __O_|__X_|__O_");
        System.out.println("\t    X |  O |  X");
    }

    public void show() {
        System.out.printf("Hi %s and %s", x, y);
    }
}

You need to have the start instance of your Game class passed into the call to the greetPlayer function as a parameter. 您需要将Game类的start实例作为参数传递给greetPlayer函数的调用。 Try something like this: 尝试这样的事情:

import java.util.*;
public class TicTac{
  public static void main(String[] args) {
    Welcome.greet();
    Game start = new Game();
    start.inputName();
    Welcome.greetPlayer(start);
    start.show();
  }
)
class Welcome{
  public static void greetPlayer(Game call){
    System.out.println("Welcome " + " " + call.x + " and " + call.y + "\n" + "Have Fun!");
  }
}

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

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