【发布时间】:2020-07-27 19:55:06
【问题描述】:
我创建了一个我在构造函数的方法签名中使用的 Author 对象:public Book [String name, Author author1........]
但是,我正在做的分配要求我将作者(实例变量)更改为Author[]。当然,现在我以前的构造函数将不起作用。这是代码
public class Author {
private String name;
private String email;
private char gender;
public String getName() {
return name;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public char getGender(){
return gender;
}
public Author(String name, String email, char gender){
this.name = name;
this.email = email;
this.gender = gender;
}
public String toString(){
return "Author[name = " + this.name + ", email = " + this.email + ", gender = " + this.gender + "]";
}
}
public class Book {
private String name;
private Author[] author;
private double price;
private int quantity;
// Getters and Setters
public String getName(){
return name;
}
public Author[] getAuthor(){
return author;
}
public void setPrice(double price){
this.price = price;
}
public double getPrice(){
return price;
}
public void setQuantity(int quantity){
this.quantity = quantity;
}
public int getQuantity(){
return this.quantity;
}
// Constructors
public Book(String name, Author[] author, int quantity){
this.name = name;
this.author = author;
this.price = price;
}
public class BookTest {
public static void main(String[] args){
Author author2 = new Author("Ben", "Ben@hgmail.com", 'm');
Author author3 = new Author("Jennie", "Jennie@hgmail.com", 'f');
Book theIdiot = new Book("The Idiot", [author2, author3], 44.5, 33);
}
}
如果我上传的方式不令人满意,对于给您带来的不便,我深表歉意。我还没有学会使用 Stack Overflow。
谢谢!
【问题讨论】:
-
我设法忽略了 Book Class public Book(String name, Author[] author, double price, int quantity){ this.name = name; this.author = 作者; this.price = 价格; this.quantity = 数量; } // 输出 public String toString(){ return "Book[name = " + this.name + ", Author[name = " + author.toString() + "], price = " + this.price + ", quantity = " + this.数量; } }
标签: java