【JVM】Java雙親委派、類加載器這塊算是玩明白了
2023-07-21 15:41 作者:推薦算法從入門到入土 | 我要投稿

import java.io.InputStream; import java.sql.SQLOutput; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class mytest { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { ClassLoader classLoader = new ClassLoader() { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { try { String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class"; InputStream is = getClass().getResourceAsStream(fileName); if(is == null){ return super.loadClass(name); } byte[] bytes = new byte[is.available()]; is.read(bytes); return defineClass(name,bytes,0,bytes.length); }catch (Exception e ) { e.printStackTrace(); throw new ClassNotFoundException(name); } } }; Object user = classLoader.loadClass("User").newInstance(); System.out.println(user.getClass()); System.out.println(user instanceof User); } public static String getNowTime(Long time){ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time); } }
標(biāo)簽: