-2

我被困在这个问题上,我有一门课叫书,另一门课叫作者

在图书类中,我有标题、作者、价格和 ISBN

在作者类中,我有名字、姓氏和国籍

问题是将作者类与书籍类联系起来......

测试类--------

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 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;
    }   
}
4

4 回答 4

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;
}

完整代码

BookTest.java

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());


    }
}

图书.java

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;
    }
}

作者.java

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;
    }
}
于 2013-02-26T19:39:25.437 回答
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
于 2013-02-26T19:39:44.933 回答
0

您的意思是将 Author 类导入 Book 类吗?

那么它只是

导入“包”。作者;

于 2013-02-26T19:37:03.937 回答
-2

如果您询问如何从书籍实例中检索作者信息,那么您可以使用

bookInf.getAuthor().getAuFname()

或者

bookInf.getAuthor().getAuSname()

但是,您必须先修改 Book 类的 getAuthor 方法,因为它返回的是 isbn 而不是 author。希望能帮助到你。

于 2013-02-26T19:36:28.357 回答