简体   繁体   中英

How can I pass a parameter into a constructor of type of a class I have created?

For example: I have created a Publisher class and I pass into a constructor.

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

And then in my testing class in which I excute:

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

In my Publisher class there is a method called getPublisherId() and in my testing class I have to pass that id. This what I did in my testing class:

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

And it is giving me an error

My Publisher class:

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



}

Your Book constructor accepts an Publisher object and you are passing the publisher ID, either change your Book constructor or overload it :-

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

Also change or overload publisher constructor since it accepts different parameters:-

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

}

There are various problems in the code.The below modifications can solve some part of your code

Book class constructor is expecting Publisher object in your code but you are passing primitive type

Add the code below

You can have overloaded constructor

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

You can have a method in Book class like this pass the id expicitly using method call

public void getpubId(int pubId)
{
// use pubId
}

call the method from your testing class like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM