大家好,又见面了,我是你们的朋友全栈君。
package com.atguigu.java;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Person {
private String name;
public int age;
public Person() {
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private Person(String name) {
this.name = name;
}
public void show(){
System.out.println("您好,我是一个人");
}
private String showNation(String nation){
System.out.println("我的国籍是" + nation);
return nation;
}
public static void main(String[] args) throws Exception {
Class clazz = Person.class;
// 1.通过反射创建对象
Constructor cons = clazz.getConstructor(String.class,int.class);
Object obj = cons.newInstance("Tom",12);
Person p = (Person) obj;
System.out.println(obj.toString());
// 2.通过反射,使用对象指定的方法和属性
Field age = clazz.getDeclaredField("age");
age.set(person,10);
System.out.println(p.toString());
// 3.调用方法
Method show = clazz.getMethod("show");
show.invoke(p);
}
}
通过反射可以调用私有的构造器和方法
public static void main(String[] args) throws Exception {
//私有属性
Class clazz = Person.class;
Constructor cons1 = clazz.getDeclaredConstructor(String.class);
cons1.setAccessible(true);
Person p = (Person) cons1.newInstance("Jerry");
System.out.println(p);
//私有方法
Method showNation = clazz.getDeclaredMethod("showNation", String.class);
showNation.setAccessible(true);
String nation = (String) showNation.invoke(p,"中国");
System.out.println(nation);
}
public static void main(String[] args) throws Exception {
// 1.
Class clazz1 = Person.class;
System.out.println(clazz1);
// 2.
Person p1 = new Person();
Class clazz2 = p1.getClass();
System.out.println(clazz2);
// 3.
Class clazz3 = Class.forName("com.atguigu.java.Person");
System.out.println(clazz3);
// 4.
ClassLoader classLoader = Person.class.getClassLoader();
Class clazz4 = classLoader.loadClass("com.atguigu.java");
System.out.println(clazz4);
}
类(外部类,内部类,匿名内部类等等),接口,数组,enum,注解,基本数据类型 ,void都算Class对象
public static void main(String[] args) throws Exception {
Properties pros = new Properties();
ClassLoader classLoader = Person.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("jdbc.properties");
pros.load(is);
String user = pros.getProperty("user");
System.out.println(user);
}
public static void main(String[] args) {
Class clazz = Person.class;
//获取当前类和其父类的所生命的public属性
Field[] fields = clazz.getFields();
for(Field field : fields){
System.out.println(field);
}
//获取当前运行时类的所有属性,不包含父类。
Field[] declaredFields = clazz.getDeclaredFields();
for (Field f :declaredFields){
System.out.println(f);
}
//权限修饰符 数据变量 变量名
Field[] declaredFields1 = clazz.getDeclaredFields();
for (Field f : declaredFields1){
//1.权限修饰符
int modifier = f.getModifiers();
System.out.println(Modifier.toString(modifier));
//2.数据类型
Class type = f.getType();
System.out.println(type.getName());
//变量名
System.out.println(f.getName());
}
}
public static void main(String[] args) {
//当前类和其父类所有的public方法
Class clazz = Person.class;
Method[] methods = clazz.getMethods();
for(Method m : methods){
System.out.println(m);
}
System.out.println();
//获取当前类所声明的所有方法:
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
System.out.println(m);
}
}
public static void main(String[] args) {
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for(Method m : declaredMethods){
//获取方法的注解
Annotation[] annos = m.getAnnotations();
for(Annotation a : annos){
System.out.println(a);
}
//权限修饰符
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
//返回值类型
System.out.print(m.getReturnType().getName() + "\t");
//方法名
System.out.print(m.getName());
System.out.print("(");
//形参列表
Class[] parameterTypes = m.getParameterTypes();
if(!(parameterTypes == null && parameterTypes.length != 0))
{
for(int i = 0;i < parameterTypes.length;i ++){
if(i == parameterTypes.length - 1){
System.out.print(parameterTypes[i].getName() + " args_" + i );
}
System.out.print(parameterTypes[i].getName() + " args_" + i + " , ");
}
}
System.out.print(")");
//抛出异常
Class[] exceptionTypes = m.getExceptionTypes();
if(!(exceptionTypes == null || exceptionTypes.length == 0)){
System.out.print(" throus ");
for(int i = 0;i < exceptionTypes.length;i ++) {
if (i == exceptionTypes.length - 1) {
System.out.print(exceptionTypes[i].getName());
break;
}
System.out.print(exceptionTypes[i].getName() + " , ");
}
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
Class clazz = Person.class;
//当前运行时类中声明为public构造器
Constructor[] constructors = clazz.getConstructors();
for(Constructor c : constructors){
System.out.println(c);
}
System.out.println();
//获取当前运行时类中声明的所有构造器
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for(Constructor c : declaredConstructors){
System.out.println(c);
}
}
public static void main(String[] args) throws Exception {
Class clazz = Person.class;
Type genericSuperclass = clazz.getGenericSuperclass();
System.out.println(genericSuperclass);
Type genericSuperclass1 = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType) genericSuperclass1;
//获取泛型类型
Type[] actualTypeArguments = paramType.getActualTypeArguments();
System.out.println(((Class)actualTypeArguments[0]).getName());
}
public static void main(String[] args) throws Exception {
Class clazz = Person.class;
Class[] interfaces = clazz.getInterfaces();
for(Class c : interfaces){
System.out.println(c);
}
System.out.println();
//获取运行时类的父类实现的接口
Class[] interfaces1 = clazz.getSuperclass().getInterfaces();
for(Class c : interfaces1){
System.out.println(c);
}
//运行时类所在包
Package pack = clazz.getPackage();
//运行时类的注解
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annos : annotations){
System.out.println(annos);
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168826.html原文链接:https://javaforall.cn