类加载器相同,则类相同,否则false package com.java.classLoader; import java.io.IOException; import java.io.InputStream; public class ClassLoaderTest { public static void main(String[] args) { ClassLoader myLoad = new ClassLoader() { @Override public Class<?> loadClass(String name)throws ClassNotFoundException { String fileName = name.substring(name.lastIndexOf(".")+1)+".class"; InputStream is = getClass().getResourceAsStream(fileName); if (null == is) { return super.loadClass(name); } try { byte[] b= new byte[is.available()]; is.read(b); return defineClass(name,b,0,b.length); } catch (IOException e) { throw new ClassNotFoundException(); } } }; try { Object obj = myLoad.loadClass("com.java.classLoader.ClassLoaderTest").newInstance(); System.out.println(obj.getClass()); // 这个obj使用的是自定义的classLoad 与 虚拟机自带的不是一个类加载器,所以返回false System.out.println(obj instanceof ClassLoaderTest); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { e.printStackTrace(); } } } ———————————————— 版权声明:本文为CSDN博主「朝闻道_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u011402896/article/details/79065896
原文:https://www.cnblogs.com/silyvin/p/12197809.html