简体   繁体   English

Java-如何将对象作为参数传递给该对象类的构造函数

[英]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.

相关问题 如何在另一个类的构造函数中将对象作为参数传递? - How to pass an object as an argument in a constructor of another class? 将参数从构造函数传递给不同类的构造函数 - Pass argument from a constructor to a constructor of a different class 如何在Java中将变量从一类传递到另一类? - How Do You Pass a Variable from One Class to Another in Java? 如何将 object 类型作为参数传递给构造函数并将变量转换为它(JAVA)? - How to pass an object type to constructor as argument and cast a variable to it (JAVA)? 如何将类的实例作为该类方法的参数传递? - How do you pass an instance of a class as an argument for a method of that class? 将构造函数参数传递给Java中的@component类 - Pass a constructor argument to a @component class in java 如何从类函数本身的构造函数中修改参数? - How do I modify an argument from the constructor in the class function itself? 如何通过类A将Java中的对象从类B传递到类C而不重复 - How do I pass an object in Java from Class B to Class C via Class A without duplication 如何在JAVA中将构造函数中的值传递给另一个类中的setter? - How to pass a value from a constructor to a setter in another class in JAVA? 你如何在同一个类的构造函数中调用mutator? - How do you call a mutator from within a constructor in the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM