jdk源代碼,以及流程圖以下java
模擬代碼app
package test.com.shareinfo.app; public abstract class AbstrFather { public AbstrFather parent; protected String loadClass(String str) throws Exception { String var4 = null; try { if (this.parent != null) { System.out.println(this.getClass() + " 擁有父加載器,移交給" + this.parent.getClass() + "加載"); var4 = this.parent.loadClass(str); } else { var4 = this.findBootstrapClassOrNull(str); } } catch (Exception var10) { System.out.println(var10.getMessage()); ; } if (var4 == null) { var4 = this.findClass(str); } return var4; } protected abstract String findClass(String str) throws Exception; public String findBootstrapClassOrNull(String str) throws Exception { throw new Exception(this.getClass() + " find class error"); } public AbstrFather getParent() { return parent; } public void setParent(AbstrFather parent) { this.parent = parent; } }
三個加載類的代碼ide
public class BootClass extends AbstrFather { @Override protected String findClass(String str) throws Exception { throw new Exception(this.getClass() + " findClass error"); } } public class ExtClass extends AbstrFather { @Override protected String findClass(String str) throws Exception { throw new Exception(this.getClass() + " findClass error"); } } public class AppClass extends AbstrFather { @Override protected String findClass(String str) throws Exception { System.out.println(this.getClass() + " findClass success"); return "myload"; } }
測試代碼測試
public class LoadTest { public static void main(String[] args) throws ClassNotFoundException { AbstrFather my = new AppClass(); BootClass bc = new BootClass(); ExtClass ec = new ExtClass(); ec.setParent(bc); my.setParent(ec); try { my.loadClass("1"); } catch (Exception e) { e.printStackTrace(); } } }
結果打印this
class test.com.shareinfo.app.AppClass 擁有父加載器,移交給class test.com.shareinfo.app.ExtClass加載 class test.com.shareinfo.app.ExtClass 擁有父加載器,移交給class test.com.shareinfo.app.BootClass加載 class test.com.shareinfo.app.BootClassbootStrp find class error class test.com.shareinfo.app.BootClass findClass error class test.com.shareinfo.app.ExtClass findClass error class test.com.shareinfo.app.AppClass findClass success