简体   繁体   English

如何从另一个类调用方法?

[英]How to call method from another class?

So I'm a rookie at OOP and I am currently having trouble using a function from another method. 因此,我是OOP的新手,目前无法使用其他方法中的函数。 The class below is the main class for a card game. 下面的课程是纸牌游戏的主要课程。 Here it creates a deck of cards by creating an object from the Game class. 在这里,它通过从Game类中创建一个对象来创建一副纸牌。 It then starts the game with the created deck and should print the size of the deck. 然后,它将使用创建的套牌开始游戏,并应打印套牌的大小。

public class Run {

public static void main(String[] args) {

    Game game;
    System.out.println("Welcome!");
    play = true;
    while (play) {
        game = new Game(3); //Create deck of card based on number of ranks given
        game.play(); //Starts the game with deck of card
    }

}

The class below is the Game class. 下面的类是Game类。 When the game starts it should print the size of the deck created. 游戏开始时,它应打印所创建套牌的大小。

public class Game {
public Game(int ranks)
{
    Deck Main = new Deck(ranks);
}
public static void play()
{
    System.out.println(Main.size()); //Where the error occurs
}

The class below is the Deck class where it actually creates the deck and has a method that returns the size of that deck. 下面的类是Deck类,它实际上在其中创建甲板,并具有一个返回甲板尺寸的方法。

public class Deck {

private ArrayList<Card> cards;

public Deck(int range) {
    cards = new ArrayList<Card>();
    for (int i=1; i<=range; i++)
    {
        Card card = new Card(1, i);
        Card card2 = new Card(2, i);
        Card card3 = new Card(3, i);
        Card card4 = new Card(4, i);
        cards.add(card);
        cards.add(card2);
        cards.add(card3);
        cards.add(card4);
    }
}
public int size() 
{
    int num=cards.size();
    return num;
}

The final class is the Card class. 最后一类是卡片类。

public class Card {
private int rank;
private int suit;
public Card(int s, int r)
{
    suit=s;
    rank=r;
}
public int getSuit()
{
    return suit;
}
public int getRank()
{
    return rank;
}

It must be an obvious error due to my lack of understanding so can anyone show what to do to fix it? 由于我缺乏理解,这肯定是一个明显的错误,因此任何人都可以显示解决该问题的方法吗?

You declared "Main" as a local variable inside the constructor of the Deck class. 您在Deck类的构造函数中将“ Main”声明为局部变量。 That means it is only visible in the body of that constructor. 这意味着它仅在该构造函数的主体中可见。

You need to turn the local variable into a field of your class instead. 您需要将局部变量转换为类的字段 Only then it becomes "visible" in other places of your class! 只有这样,它才能在您班上的其他地方变为“可见”! The very same thing you did correctly for suite and rank in your Card class! 您正确地为套件和卡类排名做了同样的事情!

And of course the play method should not be static! 当然剧中方法应该是静态的! A game is about one deck, not "all of them"! 一个游戏大约只有一个套牌,而不是“全部”!

Main declared in constructor which is impacting its visibility to other class, you can write it like: Main在构造函数中声明,这影响了它对其他类的可见性,您可以这样编写:

Deck Main = null;
public Game(int ranks)
{
    Main = new Deck(ranks);
}

another pount is, No need to create object to call a static method, You can directly call it with class name, like: 另一个麻烦是,无需创建对象即可调用静态方法,您可以使用类名直接调用它,例如:

Game.play();

Change game class like the following : 更改游戏类,如下所示:

public class Game {
   private static Deck Main;
public Game(int ranks)
{
    Main = new Deck(ranks);
}
public  static void play()
{
    System.out.println(Main.size()); //Where the error occurs
}
}

Here you have to make it static , if you want to get it in static method. 如果要使用静态方法将其设为静态,则必须将其设为静态。

Another way is : 另一种方法是:

public class Game {
    Deck Main;
public Game(int ranks)
{
    Main = new Deck(ranks);
}
public   void play()
{
    System.out.println(Main.size()); //Where the error occurs
}
}

Either way you can make it error free. 无论哪种方式,您都可以使其没有错误。 Problem was : You want to get a variable which is inside a constructor, As it is in constructor , its local variable. 问题是:您想要获取一个在构造函数内部的变量,就像在构造函数内部一样,它的局部变量。

NB : variable name should start with lowerCase. 注意:变量名称应以小写字母开头。 Use Pascal case as naming convention for JAVA. 使用Pascal大小写作为JAVA的命名约定。

please try this: 请尝试以下操作:

public class Game {

private Deck Main;

public Game(int ranks)
{
    Main = new Deck(ranks);
}
public static void play()
{
    System.out.println(Main.size()); //Where the error occurs
}

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

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