多媒体数据库小练习

 1 package com.dome;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class Database
 6 {
 7 
 8 //    private ArrayList<CD> listCD=new ArrayList<CD>();
 9 //    private ArrayList<DVD> listDVD=new ArrayList<DVD>();
10     private ArrayList<Iterm> listIterm=new ArrayList<Iterm>();
11     
12     
13 //    public void add(CD cd)
14 //    {
15 //        listCD.add(cd);
16 //    }
17 //    
18 //    public void add(DVD dvd)
19 //    {
20 //        listDVD.add(dvd);
21 //    }
22     public void add(Iterm iterm)
23     {
24         listIterm.add(iterm);
25     }
26     
27     
28     
29     public void list()
30     {
31 //        for (CD cd : listCD)
32 //        {
33 //            cd.print();
34 //        }
35 //        for (DVD dvd : listDVD)
36 //        {
37 //            dvd.print();
38 //        }
39         for (Iterm iterm : listIterm)
40         {
41             iterm.print();
42         }
43     }
44     
45     public static void main(String[] args)
46     {
47         // TODO Auto-generated method stub
48 
49 //        Database db=new Database();
50 //        db.add(new CD("abc", "abc", 4, 60, "..."));
51 //        db.add(new CD("def", "def", 4, 60, "..."));
52 //        db.add(new DVD("xxx","xxx",120,"..."));
53 //        db.list();
54         Iterm i=new Iterm("a", 3, false, "..");
55         i.print();
56     }
57 
58 }

Iterm类

package com.dome;

public class Iterm
{

    private String title;
    private int playingTime;
    private  boolean gotIt = false;
    private String comment;
    

    public Iterm(String title, int playingTime, boolean gotIt, String comment)
    {
        super();
        this.title = title;
        this.playingTime = playingTime;
        this.gotIt = gotIt;
        this.comment = comment;
    }


    protected void print()
    {
        // TODO Auto-generated method stub
        System.out.print(title);
    }

}

CD类

 1 package com.dome;
 2 
 3 public class CD extends Iterm
 4 {
 5 
 6 
 7     private String artist;
 8     private int numofTracks;
 9     
10 
11     public CD(String title, String artist, int numofTracks, int playingTime, String comment)
12     {
13         super(title,playingTime,false,comment);
14 //        this.title = title;
15         this.artist = artist;
16         this.numofTracks = numofTracks;
17     }
18 
19     public static void main(String[] args)
20     {
21         // TODO Auto-generated method stub
22     
23     }
24 
25     public void print()
26     {
27         // TODO Auto-generated method stub
28         System.out.print("CD:");
29         super.print();
30         System.out.println(":"+artist);
31     }
32 }

DVD类

 1 package com.dome;
 2 
 3 public class DVD extends Iterm
 4 {
 5 
 6     private String director;
 7     
 8     
 9     
10     public DVD(String title, String director, int playingTime, String comment)
11     {
12         super(title, playingTime, false, comment);
13         this.director = director;
14     }
15 
16 
17     public static void main(String[] args)
18     {
19         // TODO Auto-generated method stub
20 
21     }
22 
23     public void print()
24     {
25         // TODO Auto-generated method stub
26         System.out.print("CD:");
27         super.print();
28         System.out.print(":"+director);
29     }
30 
31 }

 

总结:通过这次小练习,体会了代码重复的危害和继承和函数的好处。

posted on 2020-02-05 20:51  生长的力量  阅读(173)  评论(0编辑  收藏  举报