1.Class的簡(jiǎn)單介紹
Class類的類表示正在運(yùn)行的Java應(yīng)用程序中的類和接口。 枚舉是一種類官册,一個(gè)注解是一種接口生兆, 每個(gè)數(shù)組也屬于一個(gè)反映為類對(duì)象的類,該對(duì)象由具有相同元素類型和維數(shù)的所有數(shù)組共享攀隔。 原始Java類型(boolean皂贩,byte,char昆汹,short明刷,int,long满粗,float和double)辈末,和關(guān)鍵字void也表示為類對(duì)象。
摘自jdk1.8中文版映皆,剛開始看可能不懂挤聘,現(xiàn)在逐句來(lái)解釋一下。
第一句話:一個(gè)類被加載以后捅彻,JVM就會(huì)在內(nèi)存中給創(chuàng)建一個(gè)對(duì)應(yīng)類的Class對(duì)象组去。
第二句話:類型相同的對(duì)象,維數(shù)相同的數(shù)組(不管長(zhǎng)度)共享的是同一個(gè)內(nèi)存中的Class對(duì)象步淹。
第三句話:上面這些原始的類型从隆,也會(huì)在內(nèi)存中有一個(gè)與之對(duì)象的Class對(duì)象诚撵。
package com.dingyu;import org.junit.Test;/** * Class的簡(jiǎn)單使用方法 *? * @author 70241 * */public class ClassDemo {
? ? @Test? ? public void classTest1() {
? ? ? ? try {
Class class1 = Class.forName("com.dingyu.User");// 第一種獲取Class對(duì)象的方法? ? User user = new User();
?Class class2 = user.getClass();// 第二種獲取Class對(duì)象的方法
Class class3=User.class;//第三種獲取Class對(duì)象的方法
System.out.println("接下來(lái)判斷到底同一類的不同對(duì)象的Class對(duì)象是不是同一個(gè):"? ? ? ? ? ? ? ? ? ? + (class1.hashCode() == class2.hashCode()&&class1.hashCode() == class3.hashCode()));? ? ? ? }
catch (ClassNotFoundException e) {
e.printStackTrace();? ? ? ? }? ? }
@Test? ? public void classTest2() {
String[] s1 = new String[10];
String[] s2 = new String[30];
String[][] s3 = new String[3][30];? ? ? ? System.out.println(s1.getClass().hashCode()==s2.getClass().hashCode());? ? ? ? System.out.println(s1.getClass().hashCode()==s3.getClass().hashCode());
? ? ? ? ? ? }}
2.Class獲取類的屬性,構(gòu)造器键闺,方法和注解的簡(jiǎn)單使用
package com.dingyu;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import org.junit.Test;/** * Class的簡(jiǎn)單用法 *? * @author dingyu * */public class ClassDemo02 {??
@Test? ? public void usingClass() throws Exception {? ? ? ?
Class userClass = Class.forName("com.dingyu.User");? ? ? ? // 獲得類名? ? ? ? System.out.println(userClass.getName());// 獲得全類名? ? ? ? System.out.println(userClass.getSimpleName());// 獲得類名
? ? ? // 獲得屬性
? ? ? ? Field[] fields = userClass.getDeclaredFields();// 獲得所有的屬性
? ? ? ? for (Field field : fields) {
? ? ? ? ? ? System.out.println(field.getName());? ? ? ? }? ? ? ? System.out.println(userClass.getDeclaredField("id").getName());// 獲得指定的屬性
? ? ? ? // 獲得方法
? ? ? Method[] methods = userClass.getDeclaredMethods();// 獲得所有的方法
? ? ? ? for (Method method : methods) {
? ? ? ? ? ? System.out.println(method.getName());
? ? ? ? }
? ? ? Method method = userClass.getDeclaredMethod("setId", int.class);// 獲得指定的方法寿烟,前面方法名,后面方法的參數(shù)
? ? ? System.out.println(method.getName());
? ? ? // 獲得構(gòu)造器
? ? ? Constructor[] constructors = userClass.getDeclaredConstructors();? ? ? ? System.out.println(constructors.length);
? ? ? Constructor constructor = userClass.getDeclaredConstructor(int.class, String.class, int.class);// 獲得指定的構(gòu)造器辛燥,需要指定構(gòu)造的參數(shù)? ? ? ? System.out.println(constructor.getName());
? ? ? ? // 獲得注解
? ? ? ? Annotation[] annotations = userClass.getAnnotations();
? ? ? ? for (Annotation annotation : annotations) {
? ? ? ? ? ? System.out.println(annotation);
? ? ? ? }
? ? ? // 指定注解名
? ? ? ? MyAnnotation annotation = (MyAnnotation)userClass.getDeclaredAnnotation(MyAnnotation.class);? ? ? ? System.out.println(annotation);
? }}
3.Class動(dòng)態(tài)的調(diào)用構(gòu)造器筛武,方法,修改屬性
package com.dingyu;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import org.junit.Test;
/** * 使用反射動(dòng)態(tài)的調(diào)用構(gòu)造器挎塌,方法徘六,修改屬性? * @author 70241 * */
public class ClassDemo03 {
? @Test? ? @SuppressWarnings("all")
? ? public void usingClass() throws Exception {
? ? ? Class class1 = Class.forName("com.dingyu.User");
? ? ? ? ? ? ? //使用反射去調(diào)用構(gòu)造器
? ? ? ? User user1 = (User) class1.newInstance();//調(diào)用的是無(wú)參的
Constructor constructor = class1.getDeclaredConstructor(int.class,String.class,int.class);
//獲得有參的構(gòu)造器
? ? ? ? User user2 = (User) constructor.newInstance(04,"dingyu",20);
//動(dòng)態(tài)生成對(duì)象
? ? ? ? ? ? ? //使用反射去調(diào)用方法
? ? ? ? Method methodSetId = class1.getDeclaredMethod("setId",int.class);? ? ? ? methodSetId.invoke(user1, 02)
;//執(zhí)行user1中的setId,后面是給的參數(shù)
? ? ? ? System.out.println(user1.getId());
? ? ? ? ? ? ? ? //使用反射去修改屬性的值
? ? ? Field field = class1.getDeclaredField("age");
? ? ? ? field.setAccessible(true);//因?yàn)閍ge是私有的勃蜘,加上這句就表示這個(gè)屬性不需要做安全檢查
? ? ? field.set(user1, 20);
? ? ? System.out.println(field.get(user1));
? ? ? System.out.println(user1.getAge());
? ? ? ? ? ? ? ? ? ? ? ? ? }}
4.反射獲得帶泛型的參數(shù)或返回值里泛型的的類型
package com.dingyu;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.Map;
/** * 反射獲得帶泛型的參數(shù)或返回值里泛型的的類型 *
@author dingyu * */public class ClassDemo04 {
? public void test01(Map map, String s) {
? ? }
? ? public Map test02()
{? ? ? ? return null;
? ? }
? ? public static void main(String[] args) throws Exception {
? ? ? ? //參數(shù)中帶泛型的
? ? ? ? Method method = ClassDemo04.class.getDeclaredMethod("test01", Map.class, String.class);
? ? ? ? Type[] types = method.getGenericParameterTypes();
// 返回一個(gè) Type對(duì)象的數(shù)組硕噩, Type以聲明順序表示由該對(duì)象表示的可執(zhí)行文件的形式參數(shù)類型
? ? ? ? // 打印這些參數(shù)的類型
? ? ? ? for (Type type : types) {
? ? ? ? ? ? System.out.println(type.getTypeName());
? ? ? ? ? ? if (type instanceof ParameterizedType) {
// 如果是泛型的參數(shù)
? ? ? ? ? ? ? ? Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
// 獲得泛型的的類型
? ? ? ? ? ? ? ? for (Type type2 : actualTypeArguments) {
? ? ? ? ? ? ? ? ? ? System.out.println(type2.getTypeName());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? }
? ? ? ? ? ? ? //返回值中帶泛型的
? ? ? ? Method method02 = ClassDemo04.class.getDeclaredMethod("test02");
? ? ? ? Type type = method02.getGenericReturnType();
// 返回的類型
? ? ? ? // 打印這些返回的類型
? ? ? ? ? ? ? ? ? ? System.out.println(type.getTypeName());
? ? ? ? if (type instanceof ParameterizedType) {// 如果是泛型的參數(shù)
? ? ? ? ? Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();// 獲得泛型的的類型
? ? ? ? ? ? for (Type type2 : actualTypeArguments) {? ? ? ? ? ? System.out.println(type2.getTypeName());
? ? ? ? ? ? }
? ? ? }
? ? ? ? ? ? ? }}
原作者:DingYu
原文:https://www.cnblogs.com/dddyyy/p/10009678.html