image.png
image.png
image.png
image.png
————————————————————————————————
多態(tài)表示一個類型(父類或者接口)的變量的方法的具體實現(xiàn)行為由變量指向的具體對象確定。
//多態(tài)
package com.company;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Displayable displayable = new Video();
displayable.display();
//多態(tài)
News news2 = read(new FileNewsReader(),"path_sample");
News news3 = read(new UrlNewsReader(),"path_sample");
}
//多態(tài)
private static News read(NewsReader reader,String path){
return reader.readNews(path);
}
}
多態(tài)提供的便利:減少重復(fù)代碼。