简体   繁体   English

使用另一个类的方法

[英]Use a method from another class

I was given a class called PokerHand that creates a poker hand. PokerHand了名为PokerHand ,该课程创建了扑克手。

Now I want to use the same methods on a different class called fiveCardDraw to create a totally different poker hand for a dealer and a player so I created two arrays: 现在,我想在另一个名为fiveCardDraw类上使用相同的方法为发牌者和玩家创建完全不同的扑克手,因此我创建了两个数组:

PokerHand [] dealer = new PokerHand[5];
PokerHand [] player = new PokerHand[5];

Where I am going to store the cards, but I want to use the methods on the Pokerhand class such as: 我将在哪里存储卡,但是我想使用Pokerhand类上的方法,例如:

addCard()
getCards()

How can I called those methods into the fiveCardDraw class and store them into the arrays I created? 如何将这些方法调用到fiveCardDraw类中并将它们存储到我创建的数组中?

You can either call them from the objects that are of type PokerHand (my recommended method) 您可以从类型为PokerHand的对象中调用它们(我推荐的方法)

dealer[i].addCard();
player[i].getCards();

Or if they're applicable you can convert the methods to static and they'll be callable from PokerHand.addCard() . 或者,如果适用,则可以将方法转换为static方法,并且可以从PokerHand.addCard()调用它们。

MyClass obj = new MyClass();
obj.foo = PokerHand.addCard();

if they are static methods then you would put: 如果它们是静态方法,则应输入:

fiveCardDraw.addCard();

if they are not static then you would have to create and instance (or object) of the fiveCardDraw class ex: 如果它们不是静态的,那么您将必须创建FiveCardDraw类的实例(或对象),例如:

fiveCardDraw cardDraw = new fiveCardDraw();

and then add 然后添加

cardDraw.addCard();

what do you want is this?` 您想要什么?

class fiveCardDraw {
private PokerHand [] player = new PokerHand[5];
private PokerHand [] dealer = new PokerHand[5]; 
pulic void addPlayerCard(PokerHand [] ph){
do something...
} 
pulic void addDealerCard(PokerHand [] ph){
do something...
} 
public PokerHand [] getPlayerCards()
return player;
}
public PokerHand [] getDealerCards()
return dealer;
}

` `

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

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