JavaSE---Class
1、概述
Instances of the class {@code Class} represent classes and interfaces in a running Java application.
Class 代表 Java运行时 中 类和接口的实例。
An enum is a kind of class and an annotation is a kind of interface.
枚举 是一种特殊的类,注解 是一种特殊的接口。
2、常用方法-Field
2.1、getDeclaredFields()
Returns an array of {@code Field} objects reflecting all the fields
* declared by the class or interface represented by this
* {@code Class} object.
This includes public, protected, default
* (package) access, and private fields, but excludes inherited fields.
返回 在类或接口 声明的所有字段。
包括 public、protected、default、private字段,排除 继承的字段。
If this {@code Class} object represents a class or interface with no
* declared fields, then this method returns an array of length 0.
类或接口 中没有声明字段 ,将返回空数组。
If this {@code Class} object represents an array type, a primitive
* type, or void, then this method returns an array of length 0.
如果是 数组类型 或 基本类型 或 void ,将返回 空数组。
The elements in the returned array are not sorted and are not in any
* particular order.
返回的数组内容是 没有排序的。
@CallerSensitive
public Field[] getDeclaredFields() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyFields(privateGetDeclaredFields(false));
}
2.2、getFields()
Returns an array containing {@code Field} objects reflecting all
* the accessible public fields of the class or interface represented by
* this {@code Class} object.
返回 类或接口 中声明的 可访问的public字段。
If this {@code Class} object represents a class or interface with no
* no accessible public fields, then this method returns an array of length
* 0.
如果 类或接口中 没有声明public 字段,将返回空数组。
If this {@code Class} object represents a class, then this method
* returns the public fields of the class and of all its superclasses.
如果 Class对象 代表的是 类的,将返回 该类 和 它所有父类的 public字段。
If this {@code Class} object represents an interface, then this
* method returns the fields of the interface and of all its
* superinterfaces.
如果 Class对象 代表的是 接口的,将返回 该接口 和 它所有父接口的 public字段。
If this {@code Class} object represents an array type, a primitive
* type, or void, then this method returns an array of length 0.
如果 代表的是数组 或 基本类型 或void ,将返回 空数组。
The elements in the returned array are not sorted and are not in any
* particular order.
返回的数组 没有排序。
@CallerSensitive
public Field[] getFields() throws SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return copyFields(privateGetPublicFields(null));
}
3、常用方法-Constructor
3.1、getConstructors()
Returns an array containing {@code Constructor} objects reflecting
* all the public constructors of the class represented by this
* {@code Class} object.
返回 声明在类中的所有 public 构造器。
An array of length 0 is returned if the
* class has no public constructors, or if the class is an array class, or
* if the class reflects a primitive type or void.
如果Class对象 代表的是 数组、基本类型、void、没有public构造器,将返回空数组。
@CallerSensitive
public Constructor<?>[] getConstructors() throws SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return copyConstructors(privateGetDeclaredConstructors(true));
}
3.2、getDeclaredConstructors()
Returns an array of {@code Constructor} objects reflecting all the
* constructors declared by the class represented by this
* {@code Class} object.
These are public, protected, default
* (package) access, and private constructors.
返回 在类中声明的所有的构造器。
包括 public、protected、default、private 构造器。
The elements in the array
* returned are not sorted and are not in any particular order.
返回的构造器 没有排序。
If the
* class has a default constructor, it is included in the returned array.
如果 类有一个默认的构造器,将返回默认构造器。
This method returns an array of length 0 if this {@code Class}
* object represents an interface, a primitive type, an array class, or
* void.
对于 接口、基本类型、数组、void ,将返回空数组。
@CallerSensitive
public Constructor<?>[] getDeclaredConstructors() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyConstructors(privateGetDeclaredConstructors(false));
}
4、name
Returns the name of the entity (class, interface, array class,
* primitive type, or void) represented by this {@code Class} object,
* as a {@code String}.
public String getName() {
String name = this.name;
if (name == null)
this.name = name = getName0();
return name;
}
5、package
Gets the package for this class.
The class loader of this class is used
* to find the package.
该类的 类加载器 被用来 找到这个package。
If the class was loaded by the bootstrap class
* loader the set of packages loaded from CLASSPATH is searched to find the
* package of the class.
如果 该类 被 根类加载器 加载,将会从Classpath 找到该类的package。
Null is returned if no package object was created
* by the class loader of this class.
如果 该类的类加载器 没有 package ,将返回null。
public Package getPackage() {
return Package.getPackage(this);
}
6、getSimpleName
Returns the simple name of the underlying class as given in the source code. Returns an empty string if the underlying class is anonymous. 返回 源码中给定的类的简单名称。
7、getModifiers
Returns the Java language modifiers for this class or interface, encoded
* in an integer.
返回 类或接口 的修饰符,编码为int类型。
The modifiers consist of the Java Virtual Machine's
* constants for {@code public}, {@code protected},
* {@code private}, {@code final}, {@code static},
* {@code abstract} and {@code interface}; they should be decoded
* using the methods of class {@code Modifier}.
JVM的修饰符 包含 public、protected、private、final、static、abstract、interface。
可以使用 Modifier进行处理 返回的int内容。
public native int getModifiers();
8、getSuperclass
Returns the {@code Class} representing the superclass of the entity
* (class, interface, primitive type or void) represented by this
* {@code Class}.
返回 代表 该类、接口、基本类型、void 的 直接父类。
public native Class<? super T> getSuperclass();
9、getInterfaces
Determines the interfaces implemented by the class or interface
* represented by this object.
返回 该类或接口 实现的接口。
10、getGenericSuperclass
Returns the {@code Type} representing the direct superclass of
* the entity (class, interface, primitive type or void) represented by
* this {@code Class}.
返回 直接父类。
11、getGenericInterfaces
Returns the {@code Type}s representing the interfaces
* directly implemented by the class or interface represented by
* this object.
返回直接 实现的接口。
12、getEnumConstants
Returns the elements of this enum class or null if this
* Class object does not represent an enum type.
返回 枚举类的元素 ,如果不是枚举类,返回null。
13、getDeclaringClass
If the class or interface represented by this {@code Class} object
* is a member of another class, returns the {@code Class} object
* representing the class in which it was declared.
返回 成员内部类 所在的类。
14、getDeclaredClasses
Returns an array of {@code Class} objects reflecting all the
* classes and interfaces declared as members of the class represented by
* this {@code Class} object.
返回 该类 内部声明的所有成员class。
This includes public, protected, default
* (package) access, and private classes and interfaces declared by the
* class, but excludes inherited classes and interfaces.
15、getDeclaredAnnotations
获取 所有声明的注解。
16、getClasses
Returns an array containing {@code Class} objects representing all
* the public classes and interfaces that are members of the class
* represented by this {@code Class} object.
返回 该类 的所有public的成员class。
17、method
17.1、getDeclaredMethods
Returns an array containing {@code Method} objects reflecting all the
* declared methods of the class or interface represented by this {@code
* Class} object, including public, protected, default (package)
* access, and private methods, but excluding inherited methods.
返回 该类声明的所有方法。
包括 public、private...
@CallerSensitive
public Method[] getDeclaredMethods() throws SecurityException {
checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
return copyMethods(privateGetDeclaredMethods(false));
}
17.2、getMethods
Returns an array containing {@code Method} objects reflecting all the
* public methods of the class or interface represented by this {@code
* Class} object, including those declared by the class or interface and
* those inherited from superclasses and superinterfaces.
返回 该类 声明的所有public方法。
包括父类。
@CallerSensitive
public Method[] getMethods() throws SecurityException {
checkMemberAccess(Member.PUBLIC, Reflection.getCallerClass(), true);
return copyMethods(privateGetPublicMethods());
}
18、getCanonicalName
返回 该类的 全限定名。
浙公网安备 33010602011771号