如何将参数传递给我创建的类的构造函数?

时间:2014-05-09 10:30:41

标签: java

例如: 我创建了一个Publisher类,然后传入构造函数。

public Book(String pulicationTitle, Date publicationDate,  Publisher publisher,  String ISBN_10, String ISBN_13, String authorName) {
    super(pulicationTitle, publicationDate, publisher);
    ISBN_10 = ISBN_10;
    ISBN_13 = ISBN_13;
    this.authorName = authorName;
    bookCounter++;
}

然后在我执行的测试课程中:

Book book2 = new Book(pulicationTitle, publicationDate, publisher, ISBN_10, ISBN_13,   authorName);

在我的Publisher类中有一个名为getPublisherId()的方法,在我的测试类中,我必须传递该id。 这就是我在测试课中所做的:

Date date = new Date();
Publisher pub = new Publisher("Cenage Learning", "13344-Tobean Drive,Independence, KY 45536", "Josef Blumenfield");
Book book = new Book("The world is Flat", date,pub.getPublisherID(), "037889948837", "099887636627", "Thomas L. Friedman");

它给了我一个错误

我的发布商类:

import java.util.ArrayList;


public class Publisher {
    static int  publisherCounter;
    protected static int publisherID;
    protected static String publisherName;
    private String publisherAddress;
    private String contactName;
    private ArrayList publications;

    public Publisher(String publisherName, String publisherAddress,
            String contactName) {
        super();
        this.publisherName = publisherName;
        this.publisherAddress = publisherAddress;
        this.contactName = contactName;
        publisherCounter++;
        publisherID = publisherCounter;
    }
    public static String getPublisherName() {
        return publisherName;
    }
    public void setPublisherName(String publisherName) {
        this.publisherName = publisherName;
    }
    public String getPublisherAddress() {
        return publisherAddress;
    }
    public void setPublisherAddress(String publisherAddress) {
        this.publisherAddress = publisherAddress;
    }
    public String getContactName() {
        return contactName;
    }
    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String printPublisherDetails(){
        return publisherID+publisherName+publisherAddress+contactName;
    }
    public String printAllPublications(){
        return Publication.getPulicationTitle() + Journal.getArticleTitle();
    }
    public static int getPublisherID() {
        return publisherID;
    }
    public ArrayList getPublications() {
        return publications;
    }
    public void addPublication(String adds){
        publications.add(adds);
    }



}

2 个答案:

答案 0 :(得分:0)

您的Book构造函数接受Publisher对象,并且您要传递发布者ID,更改Book构造函数或重载它: -

public Book(String pulicationTitle, Date publicationDate,  int publisherId,  String ISBN_10, String ISBN_13, String authorName) {
    super(pulicationTitle, publicationDate, publisherId);
    ISBN_10 = ISBN_10;
    ISBN_13 = ISBN_13;
    this.authorName = authorName;
    bookCounter++;
}

同时更改或重载发布者构造函数,因为它接受不同的参数: -

public Publisher(String publicationTitle,Date publicationDate,int publicationId){

}

答案 1 :(得分:0)

代码中存在各种问题。以下修改可以解决代码的某些部分

Book类构造函数在您的代码中期待Publisher object,但您正在传递primitive type

添加以下代码

您可以重载构造函数

public Book(String pulicationTitle, Date publicationDate, String ISBN_10, String ISBN_13, String authorName) {
//operations
}

你可以在Book类中使用方法调用

来快速传递id
public void getpubId(int pubId)
{
// use pubId
}

从你的测试类中调用这个方法

    //create book object
Book book = new Book("The world is Flat", date, "037889948837", "099887636627", "Thomas L. Friedman"); 
//instead of passing publisher id through constructor, pass it through a method call 
        book.getpubId(publisher.getId());