public Video getVideoByClass(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; }
调用类:
1 2 3 4 5 6 7
publicclassTest{ publicstaticvoidmain(String[] args){ VideoProduceFactory factory = new VideoProduceFactory(); Video video = factory.getVideoByClass(PythonVideo.class); video.produce(); } }