大数据学习2019.03.16
Java EE进阶——面向对象程序设计01
资料库的故事
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 自动生成的方法存根 } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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关键字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 自动生成的方法存根 } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 自动生成的方法存根 } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(); } }
多态变量
- Java的对象变量是多态的,它们能保存不止一种类型;
- 它们可以保存的是声明类型的对象,或声明类型的子类的对象;
- 当把子类的对象赋给父类的变量的时候,就发生了向上造型。
子类和子类型
- 类定义了类型
- 子类定义了子类型
- 子类的对象可以被当做父类的对象来使用
- 赋值给父类的变量
- 传递给需要父类对象的函数
- 放进存放父类对象的容器里
向上造型
- 子类的对象可以赋值给父类的变量。
- 注意!Java中不存在对象对对象的赋值。
- 父类对象不能赋值给子类的变量!
- 拿一个子类的对象,当做父类的对象来用;
- 向上造型是默认的,不需要运算符
- 向上造型总是安全的
Vechicle v;
Car c = new Car();
v = c; //可以
c = v; //编译错误
可以用造型:
c = (Car)v;
(只有当v这个变量实际管理的是Car才行)
造型
- 用括号围起类型放在值的前面
- 对象本身并没有发生任何变化
- 所以不是“类型转换”
- 运行时有机制来检查这样的转化是否合理(不合理,会抛出ClassCastException异常)。
多态
函数调用的绑定
- 当通过对象变量调用函数的时候,调用哪个函数这件事情叫做绑定;
- 静态绑定:根据变量的声明类型来决定;
- 动态绑定:根据变量的动态类型来决定;
- 在成员函数中调用其他成员函数也是通过this这个对象变量来调用的。
覆盖(override)
- 子类和父类中存在名称相同和参数列表完全相同的函数,这一对函数构成覆盖关系;
- 通过父类的变量调用存在覆盖关系的函数时,会调用变量当时管理的对象所属的类的函数。
Object类
所有的类都是继承自Object的。
Object类的函数
- toString()
- equals()