简体   繁体   中英

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).

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?

You can pass this to another method/constructor like any other reference. 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. Consider throwing an IllegalArgumentException , for example. 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:

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 ? 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关键字作为下注者实例即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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