标签:add mil 数列 名称 opened 括号 def sed public
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(title+":"+artist); } }
package dome; public class DVD { private String title; private String director; private int playingTime; private boolean gotIt = false; private String comment; public void print() { System.out.println(title+":"+director); } 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 自动生成的方法存根 } }
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) { DataBase db = new DataBase(); db.add(new CD("abc","abc",4,60,"............")); db.add(new CD("def","def",4,60,"............")); db.add(new DVD("hijk","hijk",30,"...........")); db.add(new DVD("lmno","lmno",30,"...........")); db.list(); } }
protected关键字
package dome; public class Item { protected String title; protected int playingTime; protected boolean gotIt = false; protected String comment; // public Item(String title, int playingTime, String comment) { // super(); // this.title = title; // this.playingTime = playingTime; // this.comment = comment; // } public static void main(String[] args) { // TODO 自动生成的方法存根 } public void print() { // TODO 自动生成的方法存根 } }
package dome; public class CD extends Item{ //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(title+":"+artist); } }
package dome; public class DVD extends Item{ //private String title; private String director; //private int playingTime; //private boolean gotIt = false; //private String comment; public void print() { System.out.println(title+":"+director); } 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 自动生成的方法存根 } }
package dome; import java.util.ArrayList; public class DataBase { //private ArrayList<CD> listCD = new ArrayList<CD>(); //private ArrayList<DVD> listDVD = new ArrayList<DVD>(); private ArrayList<Item> listItem = new ArrayList<Item>(); // public void add(CD cd) { // listCD.add(cd); // } // public void add(DVD dvd) { // listDVD.add(dvd); // } public void add(Item item) { listItem.add(item); } // public void list() { // for(CD cd:listCD) { // cd.print(); // } // for(DVD dvd:listDVD) { // dvd.print(); // } // } public void list() { for(Item item:listItem) { item.print(); } } public static void main(String[] args) { DataBase db = new DataBase(); db.add(new CD("abc","abc",4,60,"............")); db.add(new CD("def","def",4,60,"............")); db.add(new DVD("hijk","hijk",30,"...........")); db.add(new DVD("lmno","lmno",30,"...........")); db.list(); } }
子类和子类型
向上造型
Vechicle v;
Car c = new Car();
v = c; //可以
c = v; //编译错误
可以用造型:
c = (Car)v;
(只有当v这个变量实际管理的是Car才行)
函数调用的绑定
所有的类都是继承自Object的。
标签:add mil 数列 名称 opened 括号 def sed public
原文地址:https://www.cnblogs.com/homelessdog/p/10544523.html