Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在参数内调用构造函数的构造函数?_Java_Arrays_Constructor - Fatal编程技术网

Java 如何在参数内调用构造函数的构造函数?

Java 如何在参数内调用构造函数的构造函数?,java,arrays,constructor,Java,Arrays,Constructor,我需要用另一个类的对象创建一个数组,但另一个类也使用构造函数中另一个类的对象。。。我将努力更好地解释: public class BookStore { private String storeName; //e.g. "Jason's New Books" private Book[] inventory; // Array of Book objects public BookStore() { inventory = new Book[10

我需要用另一个类的对象创建一个数组,但另一个类也使用构造函数中另一个类的对象。。。我将努力更好地解释:

public class BookStore
{
    private String storeName; //e.g. "Jason's New Books"
    private Book[] inventory; // Array of Book objects

    public BookStore()
    {
        inventory = new Book[100];

        inventory[3] = new Book("James Joyce", 2013, "ULYSSES");
    }
我需要创建这个Book数组,但我不知道如何放置参数,因为在Book类中构造函数是这样的:

public Book(Author author, Date published, String title)
public Date()
如您所见,此构造函数使用另一个类中的对象来初始化,而另一个类的构造函数如下所示:

public Book(Author author, Date published, String title)
public Date()

当我尝试编译BookStore类时,出现以下错误:

“不兼容的类型:java.lang.String无法转换为作者”

使用以下命令:

import java.util.Date;

inventory[index] = new Book(
    new Author("Author name"),
    new Date(115, 11, 10), // The 10th of December, 2015
    "book title"
);
考虑到
作者
类定义了这样一个构造函数

public class Author {
    private String name;
    // Other member variables

    public Author(String name) {
         this.name = name;
    }

    // Other methods
}
使用以下命令:

import java.util.Date;

inventory[index] = new Book(
    new Author("Author name"),
    new Date(115, 11, 10), // The 10th of December, 2015
    "book title"
);
考虑到
作者
类定义了这样一个构造函数

public class Author {
    private String name;
    // Other member variables

    public Author(String name) {
         this.name = name;
    }

    // Other methods
}

如果在
Date
Author
类中只有无参数构造函数,则必须使用多个语句创建实例:

Author author = new Author();
author.setName("James Joyce"); // assuming such a method exists
Date date = new Date();
date.setYear(2013); // assuming such a method exists
inventory[3] = new Book(author, date, "ULYSSES");

如果在
Date
Author
类中只有无参数构造函数,则必须使用多个语句创建实例:

Author author = new Author();
author.setName("James Joyce"); // assuming such a method exists
Date date = new Date();
date.setYear(2013); // assuming such a method exists
inventory[3] = new Book(author, date, "ULYSSES");

就快到了,只需为每个对象创建一个实例:

private Book[] inventory; // Array of Book objects

    public BookStore()
    {
        inventory = new Book[100];
        inventory[3] = new Book(new Author(), new Date(), "title");
您也可以这样做:

        Author author = new Author();
        Date date = new Date();

        inventory[3] = new Book(author, title, "title");

您需要为每个对象传入所需的参数,以匹配您的构造函数

您就快到了,您只需要为每个对象创建一个实例:

private Book[] inventory; // Array of Book objects

    public BookStore()
    {
        inventory = new Book[100];
        inventory[3] = new Book(new Author(), new Date(), "title");
您也可以这样做:

        Author author = new Author();
        Date date = new Date();

        inventory[3] = new Book(author, title, "title");

您需要为每个对象传递所需的参数以匹配您的构造函数,因为您的类构造函数
书(作者、作者、发布日期、字符串标题)
接受以下3个参数

  • 第一个参数是作者类的对象
  • 第二个参数是日期的对象
  • 第三个参数是字符串变量
  • 而您使用的是字符串,因此它将通过该错误

    因此,与其使用

    inventory[3] = new Book("James Joyce", 2013, "ULYSSES");
    
    使用


    由于您的类构造函数
    书(作者、出版日期、字符串标题)
    接受以下3个参数

  • 第一个参数是作者类的对象
  • 第二个参数是日期的对象
  • 第三个参数是字符串变量
  • 而您使用的是字符串,因此它将通过该错误

    因此,与其使用

    inventory[3] = new Book("James Joyce", 2013, "ULYSSES");
    
    使用


    @Sparta给出的答案有一个问题:java.util.Date中的月数是以零为基础的(一月是0,二月是1,等等),年份是以1900为基础的,因此
    新日期(2015,12,10)
    不是“2015年12月10日”,而是“3916年1月10日”


    很抱歉,我想对你的答案发表评论,但我还没有足够的声誉…

    来自@Sparta的答案有一个问题:java.util.Date中的月数是以零为基础的(一月是0,二月是1,等等),年份是以1900为基础的,因此
    新日期(2015,12,10)
    不是“2015年12月10日”但是“3916年1月10日”


    很抱歉,我想对您的答案发表评论,但我还没有足够的声誉…

    不兼容的类型:java.lang.String无法转换为Author
    非常简单。您需要首先创建一个author对象,然后传递它而不是字符串。简单版本-
    作者a=新作者();新书(a)
    不兼容的类型:java.lang.String无法转换为Author
    非常简单。您需要首先创建一个author对象,然后传递它而不是字符串。简单版本-
    作者a=新作者();新书(a)