[英]Java - How do you pass an object as an argument into a constructor from that objects class
I have a class that acts as a bettor, and in that class the bettor can place a bet in a method that creates a bet. 我有一个充当下注者的类,在该类中,下注者可以将下注放入创建下注的方法中。 However, the constructor of the bet class needs to take in the same reference of that bettor from the bettor class.
但是,下注类的构造函数需要从下注类中获取该下注者的相同引用。 How does one go about doing that?
如何做到这一点?
Here is the code I was trying to use for this. 这是我试图用于此的代码。 I realize that making a new reference of the bettor class, but I thought I'd give it a try anyways
我意识到可以重新引用下注者类别,但我想还是应该尝试一下
public Bet placeBet(Bet.BetType betType, double amount)
{
if(betType.equals(Bet.BetType.passBet))
{
this.bankroll=bankroll-amount;
return new PassBet(new Bettor(this.name,this.bankroll),amount);
}
else if(betType.equals(Bet.BetType.any7))
{
this.bankroll=bankroll-amount;
return new Any7Bet(new Bettor(this.name,this.bankroll),amount);
}
else if(betType.equals(Bet.BetType.hard8)||betType.equals(Bet.BetType.hard10))
{
this.bankroll=bankroll-amount;
return new HardWayBet(new Bettor(this.name,this.bankroll),amount);
}
return null;
}
while the PassBet Class looks as such (it is a subclass of the Bet class, which hold the Bettor reference and the amount bet). 而PassBet类看起来像这样(它是Bet类的子类,其中包含Bettor参考和下注金额)。
public PassBet(Bettor b, double amount)
{
super(b,amount);
}
How would I go about passing the original Bettor as an argument into my PassBet subclass, which is then stored in superclass Bet? 我将如何将原始Bettor作为参数传递给我的PassBet子类,然后将其存储在超类Bet中?
You can pass this
to another method/constructor like any other reference. 你可以通过
this
像任何其他引用的另一种方法/构造函数。 For example: 例如:
public Bet placeBet(Bet.BetType betType, double amount)
{
if(betType.equals(Bet.BetType.passBet))
{
this.bankroll=bankroll-amount;
return new PassBet(this,amount);
}
else if(betType.equals(Bet.BetType.any7))
{
this.bankroll=bankroll-amount;
return new Any7Bet(this,amount);
}
else if(betType.equals(Bet.BetType.hard8)||betType.equals(Bet.BetType.hard10))
{
this.bankroll=bankroll-amount;
return new HardWayBet(this,amount);
}
return null;
}
As an aside, I'd recommend against returning null
when none of the conditions match. 顺便说一句,我建议不要在没有条件匹配时返回
null
。 Consider throwing an IllegalArgumentException
, for example. 例如,考虑抛出
IllegalArgumentException
。 Like rcook pointed out, BetType
seems like a good candidate for an enum , in which case you could use a switch instead of else if, etc. Here's a rough example: 就像rcook指出的那样,
BetType
似乎是枚举的一个很好的候选者,在这种情况下,您可以使用switch代替else if等。这是一个粗糙的示例:
public enum BetType {
PASS,
ANY_7,
HARD_8,
HARD_10;
}
...
public Bet placeBet(BetType betType, double amount) {
final Bet bet;
switch (betType) {
case PASS:
bet = new PassBet(this, amount);
break;
case ANY_7:
bet = new Any7Bet(this, amount);
break;
case HARD_8:
case HARD_10:
bet = new HardWayBet(this, amount);
break;
default:
throw new IllegalArgumentException("Invalid bet type: " + betType + ".");
}
this.bankroll -= amount;
return bet;
}
This solution could still be more object-oriented, for example why not give BetType
a method for creating a Bet
? 该解决方案仍然可以面向对象,例如,为什么不给
BetType
一个创建Bet
的方法呢? We could take advantage of polymorphism: 我们可以利用多态性:
public enum BetType {
PASS {
@Override
public Bet makeBet(Bettor bettor, double amount) {
return new PassBet(bettor, amount);
}
},
ANY_7 {
@Override
public Bet makeBet(Bettor bettor, double amount) {
return new Any7Bet(bettor, amount);
}
},
HARD_8 {
@Override
public Bet makeBet(Bettor bettor, double amount) {
return new HardWayBet(bettor, amount);
}
},
HARD_10 {
@Override
public Bet makeBet(Bettor bettor, double amount) {
return new HardWayBet(bettor, amount);
}
};
public abstract Bet makeBet(Bettor bettor, double amount);
}
...
public Bet placeBet(BetType betType, double amount) {
this.bankroll -= amount;
return betType.makeBet(this, amount);
}
This might not even be the best solution but I hope it at least opens up some possibilities for you. 这甚至可能不是最佳解决方案,但我希望它至少为您提供了一些可能性。
只需this
关键字作为下注者实例即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.