长春免费网站建站模板,无形资产 网站建设,dw简易网站怎么做,株洲关键词优化Java程序在运行时#xff0c;Java运行时系统一直对所有的对象进行所谓的运行时类型标识。 这项信息纪录了每个对象所属的类。虚拟机通常使用运行时类型信息选准正确方法去执行#xff0c;用来保存这些类型信息的类是Class类。Class类封装一个对象和接口运行时的状态#xff… Java程序在运行时Java运行时系统一直对所有的对象进行所谓的运行时类型标识。 这项信息纪录了每个对象所属的类。虚拟机通常使用运行时类型信息选准正确方法去执行用来保存这些类型信息的类是Class类。Class类封装一个对象和接口运行时的状态当装载类时Class类型的对象自动创建。 一、类定义
public final class ClassT implements java.io.Serializable,GenericDeclaration,Type,AnnotatedElement {}Serializable可被序列化的标志接口GenericDeclaration可以声明定义范型变量Type声明用的表示这是一个表示Type的东西AnnotatedElement表了在当前JVM中的一个“被注解元素”
二、常用方法
Class类没有公共的构造方法无法通过new运算符实例化只能通过对象的getClass方法或是通过Class.forName(…)来获得实例。
1、getName()
一个Class对象描述了一个特定类的属性Class类中最常用的方法getName以 String 的形式返回此 Class 对象所表示的实体类、接口、数组类、基本类型或 void名称。 public String getName() {String name this.name;if (name null)this.name name getName0();return name;}// cache the name to reduce the number of calls into the VMprivate transient String name;private native String getName0();2、newInstance()
可以为类创建一个实例
CallerSensitive
public T newInstance()throws InstantiationException, IllegalAccessException
{if (System.getSecurityManager() ! null) {checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), false);}// NOTE: the following code may not be strictly correct under// the current Java memory model.// Constructor lookupif (cachedConstructor null) {if (this Class.class) {throw new IllegalAccessException(Can not call newInstance() on the Class for java.lang.Class);}try {Class?[] empty {};final ConstructorT c getConstructor0(empty, Member.DECLARED);// Disable accessibility checks on the constructor// since we have to do the security check here anyway// (the stack depth is wrong for the Constructors// security check to work)java.security.AccessController.doPrivileged(new java.security.PrivilegedActionVoid() {public Void run() {c.setAccessible(true);return null;}});cachedConstructor c;} catch (NoSuchMethodException e) {throw (InstantiationException)new InstantiationException(getName()).initCause(e);}}ConstructorT tmpConstructor cachedConstructor;// Security check (same as in java.lang.reflect.Constructor)int modifiers tmpConstructor.getModifiers();if (!Reflection.quickCheckMemberAccess(this, modifiers)) {Class? caller Reflection.getCallerClass();if (newInstanceCallerCache ! caller) {Reflection.ensureMemberAccess(caller, this, null, modifiers);newInstanceCallerCache caller;}}// Run constructortry {return tmpConstructor.newInstance((Object[])null);} catch (InvocationTargetException e) {Unsafe.getUnsafe().throwException(e.getTargetException());// Not reachedreturn null;}
}
private volatile transient ConstructorT cachedConstructor;
private volatile transient Class? newInstanceCallerCache;3、getClassLoader()
返回该类的类加载器。
CallerSensitive
public ClassLoader getClassLoader() {ClassLoader cl getClassLoader0();if (cl null)return null;SecurityManager sm System.getSecurityManager();if (sm ! null) {ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());}return cl;4、getComponentType()
返回表示数组组件类型的 Class。
public native Class? getComponentType();5、getSuperclass()
返回表示此 Class 所表示的实体类、接口、基本类型或 void的超类的 Class。
public native Class? super T getSuperclass();6、isArray()
判定此 Class 对象是否表示一个数组类。
public native boolean isArray();7、getSimpleName(
返回源代码中给出的基础类的简称。string
public String getSimpleName() {if (isArray())return getComponentType().getSimpleName()[];String simpleName getSimpleBinaryName();if (simpleName null) { // top level classsimpleName getName();return simpleName.substring(simpleName.lastIndexOf(.)1); // strip the package name}int length simpleName.length();if (length 1 || simpleName.charAt(0) ! $)throw new InternalError(Malformed class name);int index 1;while (index length isAsciiDigit(simpleName.charAt(index)))index;// Eventually, this is the empty string iff this is an anonymous classreturn simpleName.substring(index);
}8、getDeclaredFields()
返回 Field 对象的一个数组这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。
CallerSensitive
public Field[] getDeclaredFields() throws SecurityException {checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);return copyFields(privateGetDeclaredFields(false));
}java.lang.reflect.Field主要是用于反射类的字段。反射经常用于工具类的开发或中间件的开发。
三、总结 Class 没有公共构造方法。Class 对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass 方法自动构造的因此不能显式地声明一个Class对象。 虚拟机为每种类型管理一个独一无二的Class对象。也就是说每个类型都有一个Class对象。运行程序时Java虚拟机(JVM)首先检查是否 所要加载的类对应的Class对象是否已经加载。如果没有加载JVM就会根据类名查找.class文件并将其Class对象载入。 基本的 Java 类型boolean、byte、char、short、int、long、float 和 double和关键字 void 也都对应一个 Class 对象。 每个数组属于被映射为 Class 对象的一个类所有具有相同元素类型和维数的数组都共享该 Class 对象。 一般某个类的Class对象被载入内存它就用来创建这个类的所有对象。