public abstract class FatherObject implements Runnable{
public void doSomething(){
System.out.println("做事情......");
}
}
public class ExampleObject extends FatherObject{
public int age = 30;
public String name = "byhieg";
private Integer score = 60;
public void printName(){
System.out.println(name);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public ExampleObject(){
}
public ExampleObject(String name){
}
public ExampleObject(int age,Integer score){
}
@Override
public void doSomething() {
super.doSomething();
}
@Override
public void run() {
System.out.println("run......");
}
}
String fullClassName = exampleObjectClass.getName(); String simpleClassName = exampleObjectClass.getSimpleName(); System.out.println(fullClassName); System.out.println(simpleClassName);
cn.byhieg.reflectiontutorial.ExampleObject ExampleObject
//得到包信息 Package aPackage = exampleObjectClass.getPackage(); System.out.println(aPackage); //得到父类 Class superClass = exampleObjectClass.getSuperclass(); System.out.println(superClass.getSimpleName());
package cn.byhieg.reflectiontutorial FatherObject
Modifier.isAbstract(int modifiers); Modifier.isFinal(int modifiers); Modifier.isInterface(int modifiers); Modifier.isNative(int modifiers); Modifier.isPrivate(int modifiers); Modifier.isProtected(int modifiers); Modifier.isPublic(int modifiers); Modifier.isStatic(int modifiers); Modifier.isStrict(int modifiers); Modifier.isSynchronized(int modifiers); Modifier.isTransient(int modifiers); Modifier.isVolatile(int modifiers);
//得到接口
Class[] classes = superClass.getInterfaces();
System.out.println("父类的接口" + classes[0]);
//构造器
Constructor[] constructors = exampleObjectClass.getConstructors();
for (Constructor constructor : constructors){
System.out.println(constructor.toString());
}
public cn.byhieg.reflectiontutorial.ExampleObject(int,java.lang.Integer) public cn.byhieg.reflectiontutorial.ExampleObject(java.lang.String) public cn.byhieg.reflectiontutorial.ExampleObject()
Constructor constructor = exampleObjectClass.getConstructor(String.class); System.out.println(constructor.toString());
Constructor constructor = exampleObjectClass.getConstructor(int.class,Integer.class); System.out.println(constructor.toString());
Constructor[] constructors = exampleObjectClass.getConstructors();
for (Constructor constructor : constructors){
Class[] parameterTypes = constructor.getParameterTypes();
System.out.println("构造器参数如下========================");
for (Class clz : parameterTypes){
System.out.println("参数类型 " + clz.toString());
}
}
构造器参数如下======================== 参数类型 class java.lang.String 构造器参数如下======================== 参数类型 int 参数类型 class java.lang.Integer
Object object = constructor.newInstance(1,100); System.out.println(object.toString());
Field[] fields = exampleObjectClass.getFields();
for (Field field : fields){
System.out.println("变量为: " + field.toString());
}
变量为: public int cn.byhieg.reflectiontutorial.ExampleObject.age 变量为: public java.lang.String cn.byhieg.reflectiontutorial.ExampleObject.name
Field field = exampleObjectClass.getField("age");
System.out.println("变量为:" + field.toString());
ExampleObject object = ((ExampleObject) constructor1.newInstance("byhieg"));
System.out.println("原先的age是 " + object.age);
field.set(object,10);
System.out.println("更改之后的age是" + object.age);
原先的age是 30 更改之后的age是10
//输出类的public方法
Method[] methods = exampleObjectClass.getMethods();
for (Method method : methods){
System.out.println("method = "+ method.getName());
}
Method method = exampleObjectClass.getMethod("setAge",int.class);
System.out.println(method.getName());
Method method = exampleObjectClass.getMethod("setAge",int.class);
System.out.println(method.getName());
for (Class clz : method.getParameterTypes()){
System.out.println("方法的参数" + clz.getName());
}
setAge 方法的参数int
@MyAnnotation(name="byhieg",value = "hello world")
public class AnnotationObject {
@MyAnnotation(name="field",value = "变量")
public String field;
@MyAnnotation(name="method",value = "方法")
public void doSomeThing(){
System.out.println("做一些事情");
}
public void doOtherThing(@MyAnnotation(name="param",value = "参数") String param){
}
}
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public String name();
public String value();
}
Class clz = AnnotationObject.class; Annotation[] annotations = clz.getAnnotations(); Annotation annotation = clz.getAnnotation(AnnotationObject.class);
for (Annotation annotation : annotations){
if (annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation)annotation;
System.out.println("name: " + myAnnotation.name());
System.out.println("value:" + myAnnotation.value());
}
}
Method method1 = clz.getMethod("doOtherThing",String.class);
Annotation[][] annotationInParam = method1.getParameterAnnotations();
Class[] params = method1.getParameterTypes();
int i = 0;
for (Annotation[] annotations: annotationInParam){
Class para = params[i++];
for (Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println("param: " + para.getName());
System.out.println("name : " + myAnnotation.name());
System.out.println("value :" + myAnnotation.value());
}
}
}
public class GenericObject {
public List<String> lists;
public List<String> getLists() {
return lists;
}
public void setLists(List<String> lists) {
this.lists = lists;
}
}
Class clz = GenericObject.class;
Method method = clz.getMethod("getLists");
Type genericType = method.getGenericReturnType();
if(genericType instanceof ParameterizedType){
ParameterizedType parameterizedType = ((ParameterizedType) genericType);
Type[] types = parameterizedType.getActualTypeArguments();
for (Type type : types){
Class actualClz = ((Class) type);
System.out.println("参数化类型为 : " + actualClz);
}
}
Method setMethod = clz.getMethod("setLists", List.class);
Type[] genericParameterTypes = setMethod.getGenericParameterTypes();
for (Type genericParameterType: genericParameterTypes){
System.out.println("GenericParameterTypes为 : " + genericParameterType.getTypeName());
if(genericParameterType instanceof ParameterizedType){
ParameterizedType parameterizedType = ((ParameterizedType) genericParameterType);
System.out.println("ParameterizedType为 :" + parameterizedType.getTypeName());
Type types[] = parameterizedType.getActualTypeArguments();
for (Type type : types){
System.out.println("参数化类型为 : " + ((Class) type).getName());
}
}
}
GenericParameterTypes为 : java.util.List<java.lang.String> ParameterizedType为 :java.util.List<java.lang.String> 参数化类型为 : java.lang.String
Field field = clz.getField("lists");
Type type = field.getGenericType();
if (type instanceof ParameterizedType){
ParameterizedType parameterizedType = ((ParameterizedType) type);
Type [] types = parameterizedType.getActualTypeArguments();
for (Type type1 : types) {
System.out.println("参数化类型 : " + ((Class) type1).getTypeName());
}
}
//创建一个int类型的数组,长度为3
int[] intArray = (int[])Array.newInstance(int.class,3);
//通过反射的形式,给数组赋值
for (int i = 0 ;i < intArray.length;i++){
Array.set(intArray,i,i + 2);
}
//通过反射的形式,得到数组中的值
for (int i = 0 ; i < intArray.length;i++){
System.out.println(Array.get(intArray,i));
}
Class clz = Class.forName("[I");
System.out.println(clz.getTypeName());
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有