Java基础之—反射(三)
【摘要】
Java基础之—反射(三)
一、概述
介绍Class类属性获得类的构造器、方法、属性、注解
二、反射获取类的构造器
import java.lang.reflect.Constructor; class Employee{ private String name; private int age; private Emplo...
Java基础之—反射(三)
一、概述
介绍Class类属性获得类的构造器、方法、属性、注解
二、反射获取类的构造器
import java.lang.reflect.Constructor;
class Employee{
private String name;
private int age;
private Employee() {
}
Employee(String name) {
}
public Employee(String name,Integer age) {
}
}
public class ConstructorDemo {
public static void main(String[] args) throws Exception {
//得到Employee的所有的构造器
Class<Employee> clz = Employee.class;
//类的所有公共构造方法
Constructor<Employee>[] cs = (Constructor<Employee>[]) clz.getConstructors();
for (Constructor<Employee> c : cs) {
System.out.println(c);
}
//得到public指定的构造器
//Employee(String name,int age)
//Constructor<Employee> con=clz.getConstructor(String.class,int.class);
//Employee(String name,Integer age)
Constructor<Employee> con=clz.getConstructor(String.class,Integer.class);
System.out.println(con);
//===========================================
/**
* 访问不受访问权限控制:
* Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
返回一个 Constructor 对象,该对象反映此 Class 对象所表示的类或接口的指定构造方法。
Constructor<?>[] getDeclaredConstructors()
*/
cs = (Constructor<Employee>[]) clz.getDeclaredConstructors();
for (Constructor<Employee> c : cs) {
System.out.println("-->"+c);
}
con = clz.getDeclaredConstructor();//无参
System.out.println(con);
}
}
三、反射获取类的方法
import java.lang.reflect.Method;
public class MethodDemo {
void show(){}
public void say(){}
public static void main(String[] args) throws Exception {
Class<MethodDemo> clz = MethodDemo.class;
//拿到子类及其父类里所有的public修饰的方法
Method[] ms = clz.getMethods();
for (Method m : ms) {
System.out.println(m);
}
//拿到子类及其父类里所有的public修饰的指定的方法
Method m = clz.getMethod("main", String[].class);
System.out.println(m);
//=============================================
System.out.println("==================================");
//获取自己类里的所有方法或指定的一个方法,和访问权限无关
ms = clz.getDeclaredMethods();
for (Method method : ms) {
System.out.println(method);
}
m = clz.getDeclaredMethod("show");
System.out.println(m);
//===================================
m = clz.getMethod("toString");
System.out.println(m);
//Exception in thread "main" java.lang.NoSuchMethodException: base.MethodDemo.toString()
m = clz.getDeclaredMethod("toString");
System.out.println("-->"+m);
}
}
四、反射获取类的字段
import java.lang.reflect.Field;
class A{
protected String name;
private int age;
public char c;
}
public class FieldDemo extends A {
private String hahha;
public boolean s;
public static void main(String[] args) throws Exception {
Class<FieldDemo> clz = FieldDemo.class;
//获得所有的public 字段,包括继承
Field[] fs = clz.getFields();
for (Field field : fs) {
System.out.println(field);
}
//指定的一个 public的,包阔继承
Field f = clz.getField("c");
System.out.println(f);
//得到所有的字段,只能获取当期类里面的,和访问权限无关
fs = clz.getDeclaredFields();
for (Field field : fs) {
System.out.println("-->"+field);
}
//获得当前类指定一个字段,和访问权限无关
f = clz.getDeclaredField("hahha");
System.out.println(f);
}
}
五、反射获取类的注解
import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited//可继承的
@Retention(RetentionPolicy.RUNTIME)
@interface Tag{
}
@Tag
class Super1{
}
@Deprecated
@SuppressWarnings("all")
public class AnonationDemo extends Super1{
public static void main(String[] args) {
Class<AnonationDemo> clz = AnonationDemo.class;
//所有的标签,但是是RUNTIME类型,可以获取继承过来的
Annotation[] as = clz.getAnnotations();
System.out.println(as.length);
for (Annotation a : as) {
System.out.println(a);
}
//<A extends Annotation> A getAnnotation(Class<A> annotationClass)
//获得类上时候有执行的Annotation标准
Deprecated d = clz.getAnnotation(Deprecated.class);
// Annotation[] getDeclaredAnnotations() 返回直接存在于此元素上的所有注释不包括继承的。
System.out.println(clz.getDeclaredAnnotations().length);
//表示 某个类上是有有annotationClass标签标注
// boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
//包括继承的
boolean b = clz.isAnnotationPresent(Tag.class);
System.out.println(b);
if(b){
clz.getAnnotation(Tag.class);
}
}
}
文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。
原文链接:brucelong.blog.csdn.net/article/details/95516288
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)