我坚持这个问题,我有一个名为book的类和另一个名为author
的类在图书课上我有标题,作者,价格和ISBN
在作者班,我有名字,第二名和国籍
问题是将作者类与书类联系起来......
识别TestClass --------
public class BookTest {
public static void main(String[] args) {
Author fullAuth = new Author("Bob", "Marly", "Russian");
Book bookInf = new Book("Alice", fullAuth ,60000,2000);
Student studInf = new Student("Ted", "21/10/1992", "Male","Simmonds Close 63","King Close 65","Computing", 12000);
System.out.println(fullAuth.getAuNational() +" "+ fullAuth.getAuFname ());
System.out.println(bookInf.getTitle() +" "+ bookInf.getPrice());
System.out.println(studInf.getName() +" "+ studInf.getName ());
}
}
Book Class ------------
public class Book{
private double price;
private int isbn;
private String title;
private String author;
public Book (String title, String author, double price, int isbn){
this.author = author;
this.title = title;
this.price = price;
this.isbn = isbn;
}
public void setTitleBook(String title) {
this.title = title;
}
public String getTitle(){
return title;
}
public void setPriceBook(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
public void setAuthor(String author) {
this.author = author;
}
public int getAuthor() {
return isbn;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public int getIsbn() {
return isbn;
}
}
作者类----------
public class Author{
private String auFname;
private String auSname;
private String auNational;
public Author (String auFname, String auSname, String auNational){
this.auFname = auFname;
this.auFname = auSname;
this.auNational = auNational;
}
public String getAuFname() {
return auFname;
}
public void setFirstName(String auFname) {
this.auFname = auFname;
}
public String getAuSname() {
return auSname;
}
public void setSecondName(String auSname) {
this.auSname = auSname;
}
public String getAuNational() {
return auNational;
}
public void setAuNational(String auNational) {
this.auNational = auNational;
}
}
答案 0 :(得分:3)
类Book
中的构造函数应如下所示:
public Book (String title, Author author, double price, int isbn){
this.author = author;
this.title = title;
this.price = price;
this.isbn = isbn;
}
还在主要做
Book bookInf = new Book("Alice", fullAuth ,60000,2000);
更改
public int getAuthor() {
return isbn;
}
到
public Author getAuthor() {
return author;
}
public class BookTest {
public static void main(String[] args) {
Author fullAuth = new Author("Bob", "Marly", "Russian");
Book bookInf = new Book("Alice", fullAuth, 60000, 2000);
//Student studInf = new Student("Ted", "21/10/1992", "Male", "Simmonds Close 63", "King Close 65", "Computing", 12000);
System.out.println(fullAuth.getAuNational() + " " + fullAuth.getAuFname());
System.out.println(bookInf.getTitle() + " " + bookInf.getPrice());
//System.out.println(studInf.getName() + " " + studInf.getName());
}
}
public class Book {
private double price;
private int isbn;
private String title;
private Author author;
public Book(Author a) {
author = a;
}
public Book(String title, Author author, double price, int isbn) {
this.author = author;
this.title = title;
this.price = price;
this.isbn = isbn;
}
public void setTitleBook(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setPriceBook(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
public void setAuthor(Author author) {
this.author = author;
}
public Author getAuthor() {
return author;
}
public void setIsbn(int isbn) {
this.isbn = isbn;
}
public int getIsbn() {
return isbn;
}
}
public class Author {
private String auFname;
private String auSname;
private String auNational;
public Author(String auFname, String auSname, String auNational) {
this.auFname = auFname;
this.auFname = auSname;
this.auNational = auNational;
}
public String getAuFname() {
return auFname;
}
public void setFirstName(String auFname) {
this.auFname = auFname;
}
public String getAuSname() {
return auSname;
}
public void setSecondName(String auSname) {
this.auSname = auSname;
}
public String getAuNational() {
return auNational;
}
public void setAuNational(String auNational) {
this.auNational = auNational;
}
}
答案 1 :(得分:1)
改变这个:
Book bookInf = new Book("Alice", author ,60000,2000);
到这个
Book bookInf = new Book("Alice", fullAuth ,60000,2000);
并改变这一点:
Author a = new Author(author, author, author);
到此:
this.author = author; //author being the parameter
答案 2 :(得分:0)
您的意思是将Author类导入Book类吗?
那就是
import“package”.Author;
答案 3 :(得分:-2)
如果您询问如何从图书实例中检索作者信息,那么您可以使用
获取该信息bookInf.getAuthor().getAuFname()
或
bookInf.getAuthor().getAuSname()
但是,您必须首先修改Book类的getAuthor方法,因为它返回的是isbn而不是author。希望它有所帮助。