我在使用多态性向对象添加一些字符串时遇到了麻烦。经历的第一个对象获取字符串,但是我传递字符串的第二个对象由于某种原因没有获得它。对不起,如果我发布了太多代码,我不太清楚哪些部分对于找出问题很重要。
这是代码
public class Publication {
public static void main(String[] args) {
List <Book> list = new ArrayList <Book>();
Book bookOne = new Periodical("Harry Potter", "House", "2000", "Fantasy");
bookOne.setPubFreq("Yearly");
bookOne.setEditor("Dave");
Book bookTwo = new Book("Game of Thrones", "Lanister", "2000", "Fantasy");
bookTwo.setPubFreq("Yearly");
bookTwo.setEditor("George");
System.out.print(bookOne);
System.out.print(bookTwo);
}
使用上面的主要方法,我试图传递字符串,第一个打印就像这样
Book Title :Harry Potter
Publisher :House
Publication date :2000
Subject :Fantasy
Publication Frequency :Yearly
Editor :Dave
然而,由于某种原因,本书不包括出版频率或编辑。
以下是代码的其余部分,我怀疑它与摘要有关。
public class Periodical extends Book {
private String pubFreq, editor;
//CONSTRUCTOR
public Periodical(String title, String publisher, String publicationDate, String subject) {
super(title, publisher, publicationDate, subject);
}
//PUBLICATION FREQUENCY
public String getPubFreq() {
return pubFreq;
}
public void setPubFreq(String bookPubFreq) {
this.pubFreq = bookPubFreq;
}
//EDITOR
public String getEditor() {
return editor;
}
public void setEditor(String bookEditor) {
this.editor = bookEditor;
}
public String toString() {
StringBuffer strBuf = new StringBuffer(super.toString());
strBuf.append("\nPublication Frequency :");
strBuf.append(pubFreq);
strBuf.append("\nEditor :");
strBuf.append(editor);
return strBuf.toString();
}
}
public class Book {
private String title, publisher, publicationDate, subject;
//CONSTRUCTOR
Book() {
setTitle("");
setPublisher("");
setPublicationDate("");
setSubject("");
}
Book(String title, String publisher, String publicationDate, String subject) {
setTitle(title);
setPublisher(publisher);
setPublicationDate(publicationDate);
setSubject(subject);
}
//TITLE
public String getTitle() {
return title;
}
public void setTitle(String bookTitle) {
this.title = bookTitle;
}
//PUBLISHER
public String getPublisher() {
return publisher;
}
public void setPublisher(String bookPublisher) {
this.publisher = bookPublisher;
}
//PUBLICATION DATE
public String getPublicationDate() {
return publicationDate;
}
public void setPublicationDate(String bookPublicationDate) {
this.publicationDate = bookPublicationDate;
}
//SUBJECT
public String getSubject() {
return subject;
}
public void setSubject(String bookSubject) {
this.subject = bookSubject;
}
public String toString() {
return ("Book Title :" + title + "\n" +
"Publisher :" + publisher + "\n" +
"Publication date :" + publicationDate + "\n" +
"Subject :" + subject);
}
//ABSTRACT
public void setPubFreq(String string) {
}
public void setEditor(String string) {
}
}
答案 0 :(得分:0)
您没有在Book类中将setEditor和setPubFreq声明为abstract。如果你想要它们是抽象的,你必须像这样声明它们:
public abstract void setPubFreq(String string);
public abstract void setEditor(String string);
您还必须将整个类声明为抽象 - 然后它将无法实例化。如果这不是你的意图,你可以使用一个声明这两个方法的接口,让你的Book和Periodical类实现它,或者制作另一个Book扩展的抽象类,只有这两个方法,声明为abstract。 / p>
但我想你可能只是想这样做:
Book bookTwo = new Periodical("Game of Thrones", "Lanister", "2000", "Fantasy");
而不是:
Book bookTwo = new Book("Game of Thrones", "Lanister", "2000", "Fantasy");
就像你使用bookOne一样。