English 中文(简体)
使用来自其他类的方法
原标题:Use a method from another class
  • 时间:2012-05-24 01:13:40
  •  标签:
  • java

我被授予一个名为 PokerHand 的课程,该课程创建了一支扑克牌手。

现在我要对一个叫 fiveCardDraw 的不同类别使用同样的方法,为交易商和玩家创建完全不同的扑克手,所以我创建了两个阵列:

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

我要存放卡片的地方, 但我想在 < code > Pokerhand 类上使用方法, 例如 :

addCard()
getCards()

我如何将这些方法调用到 < code> 5CardDraw 类, 并将其储存在我创建的阵列中?

问题回答

您可以从类型 PokerHand (我推荐的方法) 的对象中调用它们。

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

或者如果它们重新适用,你可以将方法转换为 stistic ,这些方法可以从 pokerHand.addCard() 调用。

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

如果它们是静态方法,那么你会说:

fiveCardDraw.addCard();

如果它们不是静态的, 那么您必须创建和实例( 或对象) 5CardDraw 类 :

fiveCardDraw cardDraw = new fiveCardDraw();

然后添加

cardDraw.addCard();

你想要什么 这是?

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;
}

`` `





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签