Java 如何引用另一个对象中的变量

Java 如何引用另一个对象中的变量,java,arraylist,field,Java,Arraylist,Field,我是一个绝对的编程初学者,我正试图从一本书中学习Java。我很困惑 这本书有一个练习(这就像是本章的一半,所以在这方面有很多内容,但我会尽量弄清楚),要求我们向类添加一个方法。基本上,我们得到了一组预构建的类,这些类应该像一个简单的拍卖程序。因此,有一个数组列表,其中包含一个投标人列表,他们出价多少,以及批号。本练习要求我们添加一种方法,该方法将打印出中标人姓名及其投标价值的列表 好吧,这是有道理的。我可以把我的思想集中在它应该如何工作上。我甚至写了以下代码:` /** * Exerc

我是一个绝对的编程初学者,我正试图从一本书中学习Java。我很困惑

这本书有一个练习(这就像是本章的一半,所以在这方面有很多内容,但我会尽量弄清楚),要求我们向类添加一个方法。基本上,我们得到了一组预构建的类,这些类应该像一个简单的拍卖程序。因此,有一个数组列表,其中包含一个投标人列表,他们出价多少,以及批号。本练习要求我们添加一种方法,该方法将打印出中标人姓名及其投标价值的列表

好吧,这是有道理的。我可以把我的思想集中在它应该如何工作上。我甚至写了以下代码:`

    /**
 * Exercise 4.48
 * for each item in the list of lots, get the highest bid.
 * if highest bid is not null, print the bidder and value
 * otherwise, print "lot not sold"
 */
 public void close()
{
    for(Lot lot : lots) {
        Bid highestBid = lot.getHighestBid();
        if(highestBid != null) {
            System.out.println(bidder, value);
        }
        else{
            System.out.println("Lot not sold.");
        }
    }
}
当试图编译它时,它会停止,因为我还没有定义变量。所以很明显,我应该告诉它“投标人”是什么意思。Bidder是同一程序中“Person”对象中变量的名称,在整个项目中使用,但我仍然不清楚如何让它理解我的“Bidder”是同一个“Bidder”。我假设我在“value”方面也会遇到同样的问题

我错过了什么

编辑以使代码实际上看起来像代码

按要求,这是那个人。。。班(我不太懂术语。我会去的。对不起。)

/**
*维护参与拍卖的人的详细信息。
*@作者大卫·J·巴恩斯和迈克尔·科林。
*@version 2011.07.31
*/
公共阶层人士
{
//这个人的名字。
私有最终字符串名;
/**
*创建具有给定名称的新人员。
*@param name这个人的名字。
*/
公众人物(字符串名称)
{
this.name=名称;
}
/**
*@返回此人的姓名。
*/
公共字符串getName()
{
返回名称;
}
}
**/**
*一个类,用于对项目中的一个项目(或一组项目)进行建模
*拍卖:很多。
* 
*@作者大卫·J·巴恩斯和迈克尔·科林。
*@version 2011.07.31
*/
公共类地段
{
//唯一的识别号。
私人最终整数;
//对该地段的描述。
私有字符串描述;
//这一地段目前的最高出价。
私人出价最高出价;
/**
*构建地块,设置地块编号和描述。
*@param number是批号。
*@param description此批次的描述。
*/
公共地段(整数编号、字符串说明)
{
这个数字=数字;
this.description=描述;
this.highestBid=null;
}
/**
*试图投标这批货。中标
*必须具有高于任何现有出价的值。
*@param重新出价。
*@成功返回true,否则返回false
*/
公共布尔投标(投标)
{
如果(最高出价==null){
//之前没有出价。
最高出价=最高出价;
返回true;
}
else if(bid.getValue()>highestBid.getValue()){
//这次出价比上一次好。
最高出价=最高出价;
返回true;
}
否则{
//出价并不好。
返回false;
}
}
/**
*@返回此批次详细信息的字符串表示。
*/
公共字符串toString()
{
字符串详细信息=编号+“:”+说明;
如果(最高出价!=null){
详细信息+=“投标:”+
highestBid.getValue();
}
否则{
详情+=“(无投标)”;
}
退货详情;
}
/**
*@返回批号。
*/
public int getNumber()
{
返回号码;
}
/**
*@返回批次描述。
*/
公共字符串getDescription()
{
返回说明;
}
/**
*@返回此批次的最高出价。
*如果存在,则该值可能为空
*没有当前的出价。
*/
公开投标获得最高投标()
{
返回最高出价;
}
}
**
/**
*模拟拍卖出价的类。
*它包含对投标者和投标金额的引用。
* 
*@作者大卫·J·巴恩斯和迈克尔·科林。
*@version 2011.07.31
*/
公开课投标
{
//出价的人。
私人最终投标人;
//出价的价值。这可能是一个很大的数字
//使用了长型。
私人最终长期价值;
/**
*创建一个出价。
*@param投标人,正在竞标该地块。
*@param value出价的价值。
*/
公开投标(个人投标人,长期价值)
{
this.bidder=投标人;
这个值=值;
}
/**
*@返回投标人。
*/
公众人士
{
退还投标人;
}
/**
*@返回出价的价值。
*/
公共长getValue()
{
返回值;
}
}
导入java.util.ArrayList;
/**
*拍卖的简单模型。
*拍卖会保留一份任意长度的拍卖品清单。
*
*@作者大卫·J·巴恩斯和迈克尔·科林。
*@version 2011.07.31
* 
*2015年3月12日增加了闭合法练习4.48
* 
*/
公开拍卖
{
//这次拍卖的拍卖品清单。
私人ArrayList l
        /**
     * Maintain details of someone who participates in an auction.
     * @author David J. Barnes and Michael Kölling.
     * @version 2011.07.31
     */
    public class Person
    {
        // The name of this person.
        private final String name;

        /**
         * Create a new person with the given name.
         * @param name The person's name.
         */
        public Person(String name)
        {
            this.name = name;
        }

        /**
         * @return The person's name.
         */
        public String getName()
        {
            return name;
        }
    }

        **/**
     * A class to model an item (or set of items) in an
     * auction: a lot.
     * 
     * @author David J. Barnes and Michael Kölling.
     * @version 2011.07.31
     */
    public class Lot
    {
        // A unique identifying number.
        private final int number;
        // A description of the lot.
        private String description;
        // The current highest bid for this lot.
        private Bid highestBid;

        /**
         * Construct a Lot, setting its number and description.
         * @param number The lot number.
         * @param description A description of this lot.
         */
        public Lot(int number, String description)
        {
            this.number = number;
            this.description = description;
            this.highestBid = null;
        }

        /**
         * Attempt to bid for this lot. A successful bid
         * must have a value higher than any existing bid.
         * @param bid A new bid.
         * @return true if successful, false otherwise
         */
        public boolean bidFor(Bid bid)
        {
            if(highestBid == null) {
                // There is no previous bid.
                highestBid = bid;
                return true;
            }
            else if(bid.getValue() > highestBid.getValue()) {
                // The bid is better than the previous one.
                highestBid = bid;
                return true;
            }
            else {
                // The bid is not better.
                return false;
            }
        }

        /**
         * @return A string representation of this lot's details.
         */
        public String toString()
        {
            String details = number + ": " + description;
            if(highestBid != null) {
                details += "    Bid: " + 
                           highestBid.getValue();
            }
            else {
                details += "    (No bid)";
            }
            return details;
        }

        /**
         * @return The lot's number.
         */
        public int getNumber()
        {
            return number;
        }

        /**
         * @return The lot's description.
         */
        public String getDescription()
        {
            return description;
        }

        /**
         * @return The highest bid for this lot.
         *         This could be null if there is
         *         no current bid.
         */
        public Bid getHighestBid()
        {
            return highestBid;
        }
    }
    **

    /**
 * A class that models an auction bid.
 * It contains a reference to the Person bidding and the amount bid.
 * 
 * @author David J. Barnes and Michael Kölling.
 * @version 2011.07.31
 */
public class Bid
{
    // The person making the bid.
    private final Person bidder;
    // The value of the bid. This could be a large number so
    // the long type has been used.
    private final long value;

        /**
         * Create a bid.
         * @param bidder Who is bidding for the lot.
         * @param value The value of the bid.
         */
        public Bid(Person bidder, long value)
        {
            this.bidder = bidder;
            this.value = value;
        }

        /**
         * @return The bidder.
         */
        public Person getBidder()
        {
            return bidder;
        }

        /**
         * @return The value of the bid.
         */
        public long getValue()
        {
            return value;
        }
    }


    import java.util.ArrayList;

    /**
     * A simple model of an auction.
     * The auction maintains a list of lots of arbitrary length.
     *
     * @author David J. Barnes and Michael Kölling.
     * @version 2011.07.31
     * 
     * 3/12/15 added close method exercise 4.48
     * 
     */

        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;

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

            /**
             * Enter a new lot into the auction.
             * @param description A description of the lot.
             */
            public void enterLot(String description)
            {
                lots.add(new Lot(nextLotNumber, description));
                nextLotNumber++;
            }

            /**
             * Show the full list of lots in this auction.
             */
            public void showLots()
            {
                for(Lot lot : lots) {
                    System.out.println(lot.toString());
                }
            }

            /**
             * Make a bid for a lot.
             * A message is printed indicating whether the bid is
             * successful or not.
             * 
             * @param lotNumber The lot being bid for.
             * @param bidder The person bidding for the lot.
             * @param value  The value of the bid.
             */
            public void makeABid(int lotNumber, Person bidder, long value)
            {
                Lot selectedLot = getLot(lotNumber);
                if(selectedLot != null) {
                    Bid bid = new Bid(bidder, value);
                    boolean successful = selectedLot.bidFor(bid);
                    if(successful) {
                        System.out.println("The bid for lot number " +
                                           lotNumber + " was successful.");
                    }
                    else {
                        // Report which bid is higher.
                        Bid highestBid = selectedLot.getHighestBid();
                        System.out.println("Lot number: " + lotNumber +
                                           " already has a bid of: " +
                                           highestBid.getValue());
                    }
                }
            }

            /**
             * Return the lot with the given number. Return null
             * if a lot with this number does not exist.
             * @param lotNumber The number of the lot to return.
             */
            public Lot getLot(int lotNumber)
            {
                if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
                    // The number seems to be reasonable.
                    Lot selectedLot = lots.get(lotNumber - 1);
                    // Include a confidence check to be sure we have the
                    // right lot.
                    if(selectedLot.getNumber() != lotNumber) {
                        System.out.println("Internal error: Lot number " +
                                           selectedLot.getNumber() +
                                           " was returned instead of " +
                                           lotNumber);
                        // Don't return an invalid lot.
                        selectedLot = null;
                    }
                    return selectedLot;
                }
                else {
                    System.out.println("Lot number: " + lotNumber +
                                       " does not exist.");
                    return null;
                }
            }
            /**
             * Exercise 4.48
             * for each item in the list of lots, get the highest bid.
             * if highest bid is not null, print the bidder and value
             * otherwise, print "lot not sold"
             */
             public void close()
            {
                for(Lot lot : lots) {
                    Bid highestBid = lot.getHighestBid();
                    if(highestBid != null) {
                        System.out.println(bidder, value);
                    }
                    else{
                        System.out.println("Lot not sold.");
                    }
                }
            }
System.out.println(bidder, value);
System.out.println(highestBid.getBidder().getName() + " bids " + highestBid.getValue())
public void close()
        {
            for(Lot lot : lots) {
                Bid highestBid = lot.getHighestBid();
                if(highestBid != null) {
                    System.out.println(highestBid.getBidder(), highestBid.getValue());
                }
                else{
                    System.out.println("Lot not sold.");
                }
            }