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