zoukankan      html  css  js  c++  java
  • Java进阶学习(4)之继承与多态.demo

    多媒体数据库小练习

     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 }

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

  • 相关阅读:
    GoGin 跨域处理
    Vue sso认证快速接入实践
    领域驱动设计(DDD):项目目录(包、模块)结构
    高绩效团队建设与管理系列课程
    VR设备产业链
    Supercell资深策划谈三大产品制作经验:游戏设计就像丛林探险,必须险中求胜
    领导力管理培训课
    博众投资:虚拟数字人概念,开辟元宇宙炒作新战场!
    FW: Flow区块链门票NFT开发实战【含源码】
    放弃学术研究,做投资大获成功
  • 原文地址:https://www.cnblogs.com/quxiangjia/p/12270832.html
Copyright © 2011-2022 走看看