一、获取对象
创建一个测试的实体类
package day02;
/**
* @author qx
* @date 2023/10/11
* @des
*/
public class Student {
public Long id;
public String name;
public Student() {
}
public Student(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
}
package day02;
import java.lang.reflect.Constructor;
/**
* @author qx
* @date 2023/10/27
* @des
*/
public class ReflectTest {
public static void main(String[] args) throws Exception {
Class> aClass = Class.forName("day02.Student");
// 1.通过无参构造器创建一个对象
Student student = (Student) aClass.newInstance();
System.out.println(student);
// 2.通过构造器创建对象
Constructor> declaredConstructor = aClass.getDeclaredConstructor(Long.class, String.class);
Student student1 = (Student) declaredConstructor.newInstance(1L, "qx");
System.out.println(student1);
}
}
输出:
Student{id=null, name='null'}
Student{id=1, name='qx'}
二、调用普通方法
package day02;
import java.lang.reflect.Method;
/**
* @author qx
* @date 2023/10/27
* @des
*/
public class ReflectTest {
public static void main(String[] args) throws Exception {
Class> aClass = Class.forName("day02.Student");
Student student = (Student) aClass.newInstance();
// 通过反射调用基本方法
Method setNameMethod = aClass.getDeclaredMethod("setName", String.class);
setNameMethod.invoke(student, "admin");
System.out.println(student.getName());
}
}
输出:
admin
三、通过反射获取属性
package day02;
import java.lang.reflect.Field;
/**
* @author qx
* @date 2023/10/27
* @des
*/服务器托管网
public class ReflectTest {
public static void main(String[] args) throws Exception {
Class> aClass = Class.forName("day02.Student");
Student student = (Student) aClass.newInstance();
// 通过反射获取操作属性
Field name = aClass.getDeclaredField("name");
name.setAccessible(true);
name.set(student,"lisi");
System.out.println(student.getName());
}
}
输出:
lisi
四、通过配置文件动态调用方法
1.创建一个配置文件
2.新建一个Person类
package day03;
/**
* @author qx
* @date 2023/10/27
* @des
*/
public class Person {
private Long id;
private String name;
public Person(){}
public Person(Long id, String name) {
this.id = id;
this.name = name;
}
public static void getPerson(){
System.out.println("i am a person");
}
}
3.通过反射获取配置文件中内容调用类中方法
package day03;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* @author qx
* @date 2023/10/27
* @des
*/
public class ReflectTest {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileReader("D:reflect.txt"));
String classname = properties.getProperty("classname");
String methodname = properties.getProperty("methodname");
Class> aClass = Class.forName(classname);
// 获取方法
Method method = aClass.getMethod(methodname);
// 通过反射执行方法
method.invoke(aClass.newInstance());
}
}
输出:
i a服务器托管网m a person
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 本地部署Jellyfin影音服务器 – 公网远程影音库
1. 前言 随着移动智能设备的普及,各种各样的使用需求也被开发出来,从最早的移动听音乐、看图片(MP3时代),到之后的移动视频需求(MP4时代)到现在的移动流媒体需求(智能手机看视频)。但当我们习惯这些需求后,忽然发现自己不知不觉间成了待割的韭菜(3台设备就要…