标签:sys oid public 视频 bsp err instance ignore bst
public abstract class Video { public abstract void produce(); }
public class JavaVideo extends Video { @Override public void produce() { System.out.println("录制Java课程视频"); } }
public class PythonVideo extends Video {
@Override
public void produce() {
System.out.println("录制Python课程视频");
}
}
public class VideoFactory { // 反射实现 public Video getVideo(Class c){ Video video = null; try { video = (Video) Class.forName(c.getName()).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return video; } // 基础实现 public Video getVideo(String type){ if("java".equalsIgnoreCase(type)){ return new JavaVideo(); }else if("python".equalsIgnoreCase(type)){ return new PythonVideo(); } return null; } }
public class Test { public static void main(String[] args) {
// 方式一: // VideoFactory videoFactory = new VideoFactory(); // Video video = videoFactory.getVideo("java"); // if(video == null){ // return; // } // video.produce();
// 方式二: VideoFactory videoFactory = new VideoFactory(); Video video = videoFactory.getVideo(JavaVideo.class); if(video == null){ return; } video.produce(); }
标签:sys oid public 视频 bsp err instance ignore bst
原文地址:https://www.cnblogs.com/thiaoqueen/p/11169102.html