JAVA 的反射机制
学习链接 :
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
package Learn; public class Main { public static void main(String[] args) throws InstantiationException, IllegalAccessException { try { Class c = Class.forName(args[0]); Stand stand = (Stand) c.newInstance(); stand.start(); } catch (ClassNotFoundException e) { System.err.println("something is wrong!!!"); e.printStackTrace(); } } } interface Stand{ void start(); } class B implements Stand{ private int i = 0; public void start() { System.out.println(i); System.out.println("B...start"); } } class C implements Stand{ public void start() { System.out.println("C...start"); } }动态加载类

更多精彩