簡體   English   中英

私有實例變量/類定義

[英]Private instance variables/Class Definitions

我正在嘗試解決這個問題:

一本書,作為標題,作者和出版年份。 包括用於獲取和設置私有實例變量的方法,以及用於顯示對象的toString方法。 還創建一個moreRecent方法,該方法將兩本書作為輸入參數,並返回最近出版的一本書。 為moreRecent創建3個JUnit測試。

但是,我不知道如何訪問其他私有實例變量author和yearofPub。 我每個課只能做一個構造函數吧? 那么,如何獲得其他兩個變量? 到目前為止,這是我的代碼:謝謝!

class Book {
private String title;
private String author;
private String yearofPub;

Book(String input) {
    title = input;
}


String moreRecent(int Book1, int Book2) {
    String moreRecent;
    if(Book1 > Book2) {
        moreRecent = "Book 1";
    }
    if (Book2 > Book1) {
        moreRecent = "Book 2";
    }
    else 
        moreRecent = "Both";
    return moreRecent;

}

}

public class Chpt2930BookTitleAuthorYear {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter title of Book: ");
    String nextLine = scan.next();
    Book titleofBook = new Book(nextLine);

}
}

編輯 - - - - - - - - - - - - -

好的,感謝您的所有答復! 我進一步改進了代碼,但仍然無法獲得正確的結果。 我特別難以使用moreRecent方法。 我該如何為這些書設置yearofPub? 這是我的代碼:

class Book {

private String title;
private String author;
private String yearofPub;

Book(String input) {
    title = input.toString();
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getYearofPub() {
    return yearofPub;
}

public void setYearofPub(String yearofPub) {
    this.yearofPub = yearofPub;
}

public String toString() {
    System.out.println("Book{" + "Title=" + title + " Author=" + author + " Year of
      Publication=" + yearofPub + "}");
    return " ";
}
//confused on this part :(
String moreRecent(Book Book1, Book Book2) {
    if (Book1.getYearofPub>Book.getYearofPub) { //Is this correct? I'm not sure :/

    }
}
}

public class Chpt2930BookTitleAuthorYear {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter title of Book: ");
    String nextLineTitle = scan.nextLine();
    Book titleofBook = new Book(nextLineTitle);


    System.out.println("Enter author of the Book: ");
    String nextLineAuthor = scan.nextLine();
    titleofBook.setAuthor(nextLineAuthor);


    System.out.println("Enter the date of publication of the Book: ");
    String nextLineDate = scan.nextLine();
    titleofBook.setYearofPub(nextLineDate);

    titleofBook.toString();

}
}

謝謝!

編輯2 -----------------

Nvm我發現問題了嗎:)盡管感謝您的所有幫助

做這個:

class Book {

    private String title;
    private String author;
    private String yearofPub;

    Book(String input) {
        title = input;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getYearofPub() {
        return yearofPub;
    }

    public void setYearofPub(String yearofPub) {
        this.yearofPub = yearofPub;
    }



}

在主要方法中,您可以:

Book titleofBook = new Book(nextLine);
titleofBook.setAuthor(...) 

提供變量的所謂訪問器。 例如:

public String getAuthor() { 
   return this.author; 
}

public void setAuthor(String author) { 
   this.author = author; 
}

yeaofPub字段也是yeaofPub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM