Java 在BookOrder中复制BookOrder

Java 在BookOrder中复制BookOrder,java,Java,我正在尝试复制我已经创建的BookOrder,但是,它没有正确创建。到目前为止,我有这个 public class BookOrder { private String author; private String title; private int quantity; private double costPerBook; private String orderDate; private double weight; pr

我正在尝试复制我已经创建的BookOrder,但是,它没有正确创建。到目前为止,我有这个

public class BookOrder
{
    private String author;      
    private String title;
    private int quantity;
    private double costPerBook;
    private String orderDate;
    private double weight;
    private char type;      //R,O,F,U,N

    public BookOrder (String author, String title)
    {
    }

    public BookOrder(String author, String title, int quantity, double costPerBook, String orderDate, double weight, char type)
    {
        this.author= author;
        this.title= title;
        this.quantity= quantity;
        this.costPerBook= costPerBook;
        this.orderDate= orderDate;
        this.weight= weight;
        this.type=type;
    }

    public BookOrder(BookOrder bookOrder)
    {
    }
但是,当我尝试在此处复制此内容时:

public class TestBookOrder
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        Utility.myInfo("11/5,2013", "Project4");
        Utility.pressEnterToContinue();
        Utility.clearScreen();

        BookOrder BookOrder1 = new BookOrder("Jonathan", "Book1", 12, 6.75, "11/5/2013", 8.75, 'r');
        System.out.print(""+ BookOrder1.invoice());

        BookOrder copyA = new BookOrder(BookOrder1);

        BookOrder copyB= new BookOrder(BookOrder1);

        copyB.adjustQuantity(-5);

        System.out.print("\n"+ copyB.invoice());
        System.out.print("\n"+ copyA.invoice());

    }
}

它只是将copyA和copyB的发票返回为null和0。有人知道复制方法中需要哪些代码吗?

要使这项工作正常,您需要更改以下代码。这将创建一个新对象,并使用作为输入传递的BookOrder值填充该对象。希望这能回答你的疑问

public BookOrder(BookOrder bookOrder)
    {
    this.author= bookOrder.getAuthor();
    this.title= bookOrder.getTitle();
    this.quantity= bookOrder.getQuantity();
    this.costPerBook= bookOrder.getCostPerBook();
    this.orderDate= bookOrder.getOrderDate();
    this.weight= bookOrder.getWeight();
    this.type=bookOrder.getType();
    }

您需要将参数BookOrder的值复制到当前参数中

e、 g

class Foo {
  private int bar;

  public Foo(Foo foo) {
    this.bar = foo.bar;
    // same for other fields if they exist.
  }
}
顺便说一句,您需要解决以下问题:

public BookOrder (String author, String title)
{
}
您正在抛出传递到此构造函数的参数,完全忽略它们


编辑:这已经在同一作业的前一个问题的前一个答案中向您解释过了

这是因为您需要在复制构造函数中将所有字段从传递的对象分配给当前对象

public BookOrder(BookOrder bookOrder) {
    this.author = bookOrder.getAuthor(); // since they are private and thus you need getter to get the values
    this.title = bookOrder.getTitle();
    this.quantity = bookOrder.getQuantity();
    this.costPerBook = bookOrder.getCostPerBook();
    this.orderDate = bookOrder.getOrderDate();
    this.weight = bookOrder.getWeight();
    this.type =bookOrder.getType();
}

你忽略了1小时前在回答类似问题时给你的建议。为什么?您需要实现
BookOrder(BookOrder BookOrder)
构造函数。但是您的变量是ptivate,所以我希望您有所有的getter(和setter)来实现这一点!)@安德烈·韦夫特:正如他在同一项任务的前一个问题中所看到的那样。@装满鳗鱼的气垫船[链接]哦,是的。。。但是有什么不对?)为什么他问了两次???)亲爱的
Jon5001
阅读更多关于构造函数的信息,以及它们是如何工作的。我想它会解决你所有的问题!