问程序错误的解法,在图书管理程序中,类Book,Newspaper和Video都是Media的子类。编写一个类,该类能够实现对一组书,报纸等的存储,并提供一定的
public abstract class Media { public String name;
public String authorName;
public abstract String toString();
public Media(String name,String authorName){
this.name=name;
this.authorName=authorName;
}
}
class Book extends Media {
public String name;
public String authorName;
public Book(String name,String authorName){
super(name,authorName);
this.name=name;
this.authorName=authorName;
}
public String toString(){
String message="书名为"+name+",作者是"+authorName+"";
return message;
}
}
class Newspapper extends Media{
public String name;
public String authorName;
public Newspapper(String name,String authorName){
super(name,authorName);
this.name=name;
this.authorName=authorName;
}
public String toString(){
String message="报名为"+name+",编辑是"+authorName+"";
return message;
}
}
import java.util.*;
public class Manage {
public static ArrayList<Media> a=new ArrayList<Media>();
public static void find(String name){
Iterator<Media> ite=a.iterator();
while(ite.hasNext()){
Media exe=ite.next();
if(exe.name==name||exe.authorName==name){
System.out.println(exe.toString());
return;
}
}
System.out.println("没有此记录");
}
public void add(Media m){
a.add(m);
}
public static void main(String args[]){
Media temp1=new Book("钢铁是怎样炼成的","保尔.柯察金");
a.add(temp1);
Media temp2=new Newspapper("人民日报","李刚");
a.add(temp2);
Media temp3=new Book("一座城池","韩寒");
Media temp4=new Book("平凡的世界","路遥");
a.add(temp3);
a.add(temp4);
Manage.find("路遥");
}
}