0

不确定我的标题是否措辞好,但我正在使用 BlueJ 来学习一点 Java,并且我正在从事一个拍卖项目(基于Objects First With Java: A Practical Introduction Using BlueJ的第 4 章中的一个示例) ,有一些变化)。我要做的是添加第二个构造函数,该构造函数将拍卖作为参数,如果该拍卖当前已关闭,则使用其中未售出的地块创建一个新的拍卖。如果它仍然打开或为空,则此构造函数应该像我的默认构造函数一样工作。

这是我的默认构造函数代码的开头:

...

public class Auction
{
    // The list of Lots in this auction.
    private ArrayList<Lot> lots;
    // The number that will be given to the next lot entered
    // into this auction.
    private int nextLotNumber;
    // Whether or not the auction is open for bidding.
    private boolean openForBid;

    /**
     * Create a new auction.
     */
    public Auction()
    {
        lots = new ArrayList<Lot>();
        nextLotNumber = 1;
        openForBid = true;
    }

    /**
     * Second Constructor
     */ 
    public Auction(Auction auction)
    {
        //If the auction is open..
            //Create a new Auction the same as above
        //else..
            //create a new auction with unsold lots from the specified auction
    }

我正在为这个 Auction 类制作一个框架,几乎没有什么指令,但是有一个方法应该返回一个当前没有出价的批次的 ArrayList。

public ArrayList<Lot> getNoBids()

所以我想我需要在传递给构造函数的 Auction 上调用它,但我似乎无法将这一切放在一起。感谢任何帮助,因为我对 Java 和 ArrayLists 还很陌生!谢谢。

4

3 回答 3

2

由于您将默认行为/构造函数设置为打开,因此您可以拥有以下内容:

public Auction(Auction auction) {

   this();

   if (!auction.openClosed) {
      lots.addAll(auction.getNoBids());
      // set close flags as necessary...
   }
} 

同样使用变量 likeopenClosed是令人困惑的。可以调用openForBidding它来使其目的更明确。

于 2012-10-18T00:36:06.233 回答
1

如果给定通过Auction auction状态(关闭),您想在新拍卖中添加批次,您可以执行以下操作:

public Auction(Auction auction)  {
     this.lots =new ArrayList<Lot>();
     openClosed = true;
     if(!auction.isOpenForBid()){
       nextLotNumber = 1;
       this.lots.addAll(auction.getLots()); 
    }else{
       nextLotNumber = this.lots.size(); 
    }
}

我想,在这两种情况下,openClosed都应该设置为 true,如果您要从以前的拍卖中添加手nextLotNumber数,您可能需要相应地进行初始化,即添加手数。

于 2012-10-18T00:33:35.337 回答
-1

这个功劳应该归功于上面的 Reimeus,但我似乎无法评论他的回答。

基本上,他是对的,只是需要调用 this() 而不是 super()。

  public Auction(Auction auction)
  {
      this();

      if (!auction.openClosed) {
         lots.addAll(auction.getNoBids());
         // set close flags as necessary...
      }
  } 
于 2012-10-18T00:48:12.660 回答