**
P69(媒体资料库的设计(CD、DVD例子)):
**
一、如何将一堆成员变量快速初始化起来?
点击蓝色区域之后:
点击生成后:
这里的 super()先暂时不说,此处可有可无。
二、同一个类里放两个容器
- 该CD、DVD例子的结构:
代码段:
package dome;
import java.util.ArrayList;
public class Database {
private ArrayList<CD> listCD=new ArrayList<CD>();
private ArrayList<DVD> listDVD=new ArrayList<DVD>();
public void add(CD cd) {
listCD.add(cd);
}
public void add(DVD dvd) {
listDVD.add(dvd);
}
public void list() {
for(CD cd:listCD) {
cd.print();
}
for(DVD dvd:listDVD) {
dvd.print();
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Database db=new Database();
db.add(new CD("abc","abc",4,60,"..."));
db.add(new CD("def","def",4,60,"..."));
db.add(new DVD("xxx","aaa",60,"..."));
db.list();
}
}
package dome;
public class CD {
private String title;
private String artist;
private int numofTracks;
private int playingTime;
private boolean gotIt=false;
private String comment;
public CD(String title, String artist, int numofTracks, int playingTime, String comment) {
super();
this.title = title;
this.artist = artist;
this.numofTracks = numofTracks;
this.playingTime = playingTime;
this.comment = comment;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
}
public void print() {
System.out.println("CD:"+title+":"+artist);
}
}
package dome;
public class DVD {
private String title;
private String director;
private int playingTime;
private boolean gotIt=false;
private String comment;
public DVD(String title, String director, int playingTime, String comment) {
super();
this.title = title;
this.director = director;
this.playingTime = playingTime;
this.comment = comment;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
}
public void print() {
System.out.println("DVD:"+title+":"+director);
}
}