对象创建对象

时间:2016-02-18 05:23:10

标签: java bluej

我是一名非常新的程序员,我的任务中的这个问题让我非常难过。我们目前正致力于用对象创建对象,而我完全迷失了。我有一个MediaDemo类,它应该从Book类创建一个实例。我觉得我已经接近了一点,但错误一直说它不能接受变量类型......这里是两个类的代码:

    public class Book 
{ 
    // The fields. 
    private String author; 
    private String title;
    private int pages;
    private String refNumber;
    private int borrowed;
    private boolean courseText;

/** 
 * Set the author and title fields when this object 
* is constructed. 
*/ 
    public Book( String bookAuthor, String bookTitle, int bookPages, boolean theCourseText) 
   { 
       author = bookAuthor; 
        title = bookTitle;
        pages = bookPages;
        refNumber = ("");
        courseText = theCourseText;
    }

public String printAuthor()
{
return author;
}

public String printTitle()
{
return title;
}

public void setRefNumber(String ref)
{
if(ref.length() == 3){
refNumber = ref;
}
else{
System.out.println("Please enter a String that's 3 characters long.");
}
}

public String printRef()
{
return refNumber;
}

public void borrow()
{
    borrowed += 1;
}
public int getBorrowed()
{
    return borrowed;
}

public void printDetails()
{
if(refNumber == ("")) {
System.out.println("Book Author: " + author + ". | Book Title: " + title + ". | Book Length: " + pages + " pages. refNumber: ZZZ | Borrowed Books: " + borrowed);
}
else {
System.out.println("Book Author: " + author + ". | Book Title: " + title + ". | Book Length: " + pages + " pages. | Number: " + refNumber + ". | Borrowed Books:     " + borrowed);
}
}
} 

感谢您提供任何帮助,一旦找到阻止我在我的桌子上敲打头的解决方案,我就会很高兴......

- 更新 -

/**
 * Write a description of class MediaDemo here.
 * 
 * @author ---
 * @version 2-17-16
 */
public class MediaDemo
{
 private Book book;
 private String bookAuthor;
 private String bookTitle;
 private int bookPages;
 private boolean theCourseText;
 public MediaDemo (String author,String title,int pages,boolean courseBook)
 {
     bookAuthor = author;
     bookTitle = title;
     bookPages = pages;
     theCourseText = courseBook;
     createBook();
 }

 private void createBook()

{
  book = new Book(bookAuthor, bookTitle, bookPages, theCourseText);
}
}

好的,这是Book的当前版本。

4 个答案:

答案 0 :(得分:1)

语句book = new Book();实例化一个新的对象书,当你说新的Book()时,它意味着调用Book构造函数

public Book( String bookAuthor, String bookTitle, int bookPages, boolean theCourseText) 
   { 
       author = bookAuthor; 
        title = bookTitle;
        pages = bookPages;
        refNumber = "(\"\")"; \\result : ("")
        courseText = theCourseText;
    }

。在您的代码中,您没有提供创建对象簿新实例所需的参数。所以你可以尝试替换:

book = new Book();

book = new Book( "Scarlet", "I dunnu", 100, true); 

当然还有参数的相应数据类型

答案 1 :(得分:0)

你在书中有一个参数化的构造函数...... 但是当你打电话时,你正在调用一个非参数化的构造函数..

所以代码应该是

Book book = new Book(author, title, bookPages, theCourseText) ;

是的,它会创建一个新实例,而创建时会调用参数化的构造函数。 谢谢..

答案 2 :(得分:0)

在MediaDemo类中,您正在调用默认构造函数Book() 但它未在Book Class中声明。因为你有一个明确的构造函数
public Book(String bookAuthor,String bookTitle,int bookPages,boolean theCourseText) 你需要使用它或明确声明一个dafault构造函数

答案 3 :(得分:0)

在调用下面的行之前,您必须为所有参数指定一些值 book = new Book(bookAuthor,bookTitle,bookPages,theCourseText);