1

所以我有三个类:books、booksOut 和 Allbooks。bookout 和 Allbooks 是扩展/是书籍的子类。但是我有一个问题/问题:

我从 toString 方法中的 allBooks 中得到一个错误。说 bookID 等(即 books 中的所有变量)都设置为私有。我被教导你应该总是尽量避免公共变量,所以我很犹豫。还有其他方法吗?还是将它们公开是处理此问题的最简单/最佳方法?

这是代码:

图书

public class books {

private int bookID;
private String title;
private String author;


public books() {
}

public books(int bookID, String title, String author) {
    this.bookID = bookID;
    this.title = title;
    this.author = author;
}

public int getBookID() {
    return bookID;
}

public void setBookID(int bookID) {
    this.bookID = bookID;
}

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 addSpaces(String s, int w) {
    String spc = "";
    for (int i = 1; i <= (w - s.length()); i++) {
        spc = spc + " ";
    }
    return spc;
}

public class AllBooks extends books{
private String genre;
private String status;
private String Location;
private String condition;

所有书籍

public AllBooks(int bookID, String title, String author, String genre, String status, String Location, String condition ) {
    super(bookID, title, author);
    this.genre = genre;
    this.status = status;
    this.Location = Location;
    this.condition = condition;
}

public String getGenre() {
    return genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getLocation() {
    return Location;
}

public void setLocation(String Location) {
    this.Location = Location;
}

public String getCondition() {
    return condition;
}

public void setCondition(String condition) {
    this.condition = condition;
}



    @Override
public String toString() {
    String stg = "";
    stg = stg + bookID + '\t' + title + addSpaces(title, 30) + author + addSpaces(author, 30) + genre + addSpaces(genre, 15) + status + addSpaces(status, 5) + Location + addSpaces(Location, 20) + condition;
    return stg;
}

    public String toString(int i){
    String stg = "";
    stg += bookID + "#" + title + "#" + author + "#" + genre + "#" + status + "#" + Location + "#" + condition + "#";
    return stg;
}

附言

如果这是一个愚蠢的问题,我很抱歉。这是一个学校项目,它是在我的假期之后到期的,我现在正在进行,这就是我不问老师的原因。我确实在网上查了答案,但是我遇到的教程并没有提到太多关于礼仪的内容。感谢您的帮助,和/或抱歉浪费您的时间。

4

3 回答 3

6

使用现有的公共访问器。例如,getBookID()在子类中使用而不是尝试直接访问超类的私有字段。

@Override
public String toString() {
    String stg = getBookID() + '\t' + getTitle() 
        + addSpaces(title, 30) + getAuthor() 
        + addSpaces(author, 30) + getGenre() 
        + addSpaces(genre, 15) + getStatus() 
        + addSpaces(status, 5) + getLocation() 
        + addSpaces(Location, 20) + getCondition();
    return stg;
}

另一种方法是制作字段protected。但是,这允许潜在的任意数量的子类直接耦合到您的超类的表示。将数据封装在方法后面通常是可取的。

顺便说一句,您可能会发现String.format()很有用。有关语法,请参阅格式化字符串语法

于 2013-07-16T15:49:56.640 回答
1

既然你已经有了 bookID 的 getter,你就不能在你的其他类中使用它,例如:

System.out.println(books.getBookID());
于 2013-07-16T15:52:41.690 回答
0

正如 Andy Thomas 所指出的,您可以直接使用公共方法来访问它。公共方法实际上是封装了私有变量。

如果您仍然需要直接使用它,您应该将其设置为受保护的,这意味着只有扩展它的类才能访问它。

于 2013-07-16T15:55:58.427 回答