1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| package 面向对象程序设计_Java语言_翁恺;
import java.util.ArrayList;
class Item { private String title; private int playTime; private boolean gotIt = false; private String comment;
public Item(String title, int playTime, boolean gotIt, String comment) { this.title = title; this.playTime = playTime; this.gotIt = gotIt; this.comment = comment; } public void print() { System.out.print("title:"+title+";"+"playTime:"+playTime+";"+"gotIt:"+gotIt+";"+"comment:"+comment+";"); } }
class Mp3 extends Item { private String artist; private int numofTracks; public Mp3(String title, int playTime, boolean gotIt, String comment,String artist,int numofTracks) { super(title, playTime, gotIt, comment); this.artist=artist; this.numofTracks=numofTracks; }
@Override public void print() { System.out.print("Mp3:{"); super.print(); System.out.println("artist:"+artist+";"+"numofTracks:"+numofTracks+"}"); } }
public class Database { private ArrayList<Item> list=new ArrayList<Item>(); public void add(Item item) { list.add(item); } public void list() { for(Item item:list) item.print(); } public static void main(String[] args) { Database db=new Database(); db.add(new Mp3("A", 60, true, "good", "John", 20)); db.add(new Mp3("B", 80, true, "vrey good", "Tom", 10)); db.list(); }
}
|