java - 不调用继承类的方法

标签 java arrays inheritance tostring

在这个例子中我有 3 个类。 Method类、Book类、AllBook类。 AllBooks 扩展了图书类别。方法类是我的大部分方法所在的地方(废话)。我只有一个问题,即 addBook 方法。

代码:

方法类

public class methods {
// Variables
int bCount = 0;
int mCount = 0;
int lCount = 0;
int boCount = 0;
//Arrays
Book[] bArr = new Book[500];
Member[] mArr = new Member[250];
Librarian[] lArr = new Librarian[50];
MainMenu mm;
public void newBook() {
    int cont2 = JOptionPane.YES_OPTION;
    int cont = JOptionPane.YES_OPTION;
    int choice = JOptionPane.YES_OPTION;
    while (choice == JOptionPane.YES_OPTION) {
        String ttl = mm.tiltleTF.getText();
        String athr = mm.authorTF.getText();
        String gnr = mm.genreTF.getText();
        String lctn = mm.locationTF.getText();
        String cndtn = mm.conditionTF.getText();
        if (athr == null && gnr == null && lctn == null && cndtn == null) {
            cont = JOptionPane.showConfirmDialog(null, "You have left out some fields are you sure you wish to continue?", "Information left out", JOptionPane.YES_NO_OPTION);
        }
        while (cont == JOptionPane.YES_OPTION) {
            ttl = ttl.toUpperCase();
            athr = athr.toUpperCase();
            gnr = gnr.toUpperCase();
            lctn = lctn.toUpperCase();
            cndtn = cndtn.toUpperCase();
            cont2 = JOptionPane.showConfirmDialog(null, "The book you wish to enter is:" + "\n Title:\t" + ttl + "\n Author:\t" + athr + "\n Genre:/t" + gnr + "\n Location: \n" + lctn + "\n Condition:\t" + cndtn, "Are you sure?", JOptionPane.YES_NO_OPTION);
            while (cont2 == JOptionPane.YES_OPTION) {
                bArr[bCount] = new AllBooks(bCount, ttl, athr, gnr, "IN", lctn, cndtn);
                try {
                    PrintWriter pw = new PrintWriter(new FileWriter("books.txt", true));
                    pw.println(bArr[bCount].toString(0);
                    pw.close();
                    bCount++;
                    displayBooks();
                    choice = JOptionPane.showConfirmDialog(null, "Book added! Do you wish to enter another book?", "Enter another book?", JOptionPane.YES_NO_OPTION);
                } catch (IOException ex) {
                }
            }
        }
    }
}

所有书籍

public class AllBooks extends Book {

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 + getBookID() + '\t' + getTitle() + addSpaces(getTitle(), 30) + getAuthor() + addSpaces(getAuthor(), 30) + genre + addSpaces(genre, 15) + status + addSpaces(status, 5) + Location + addSpaces(Location, 20) + condition;
    return stg;
}

public String toString(int i) {
    String stg = "";
    stg += getBookID() + "#" + getTitle() + "#" + getAuthor() + "#" + getGenre() + "#" + getStatus() + "#" + getLocation() + "#" + getCondition() + "#";
    return stg;
}

}

预订 公开课书籍{

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


public Book() {
}

public Book(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;
}

}

我遇到的问题是,在 newBook 方法中,当它使用 toString 写入文件时,它不能“链接(?)”或识别所有书籍类中的 toString (在 i 中)方法。有什么问题吗?

最佳答案

你的 newBook 方法在 Book 类中吗?继承的工作原理是,事物只能从它们之上的事物继承,因此,如果您希望某个方法沿着继承树向下传播,则必须在更高的级别上定义它比您想要在其中使用它的类。

又名。在 Book 中定义 toString 方法,而不是在 AllBooks 中。

关于java - 不调用继承类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17728353/

上一篇:java - XSLT 收集数据

下一篇:Java重绘问题

相关文章:

java - 使用 Android Studio 从 Firebase 读取节点标题

java - 将列表均匀分布到 Java 中的子列表中

javascript - 需要帮助根据测验获得的结果更改背景颜色吗?

c# - WCF 派生的 DataContract 不继承基础 DataContract 的属性

c++ - 继承自具有相同成员的两个 parent

java - java按钮java中的空指针异常错误

java - 带有 tomcat 的 HAproxy - 连接不足

javascript - 删除数组中重复的未排序对象

c - 为数组元素动态分配内存

c++ - 具有混合继承修饰符的菱形继承(钻石问题)( protected /私有(private)/公共(public))