类加载
package com.ding;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 10:39
* @Version 1.0
*/
public class ClassLoaderDemo {
public static void main(String[] args) throws IOException {
//获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
/**
* 利用加载器去加载一个指定文件
* 参数:文件路径
* 返回值:字节流
*/
InputStream is = systemClassLoader.getResourceAsStream("prop.properties");
Properties prop = new Properties();
prop.load(is);
System.out.println(prop);
is.close();
}
}
name=zhangsan
age=13
sex=nan
反射
1.结构图
reflect1
className=com.ding.reflect1.Student
methodName=study
package com.ding.reflect1;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 11:07
* @Version 1.0
*/
public class ReflectDemo1 {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("prop.properties");
//创建Properties集合
Properties properties = new Properties();
//将文件中的数据读取到集合当中
properties.load(is);
is.close();
//利用反射创建对象并调用方法
//获取字节码文件对象
Class<?> clazz = Class.forName(properties.getProperty("className"));
//获取构造器对象
Constructor constructor = clazz.getConstructor();
//利用构造器对象创建一个对象
Object o = constructor.newInstance();
//获取方法对象
Method method = clazz.getMethod(properties.getProperty("methodName"));
//运行方法
method.invoke(o);
}
}
package com.ding.reflect1;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 11:26
* @Version 1.0
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void study(){
System.out.println("学生在学习");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.ding.reflect1;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 11:27
* @Version 1.0
*/
public class Teacher {
private String name;
private int age;
public Teacher() {
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void teach(){
System.out.println("老师在上课");
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.ding.reflect1;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 11:27
* @Version 1.0
*/
public class Worker {
private String name;
private int age;
public Worker() {
}
public Worker(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void work(){
System.out.println("工人在工作");
}
@Override
public String toString() {
return "Worker{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
reflect2(获取class对象的三种方式)
package com.ding.reflect2;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 11:26
* @Version 1.0
*/
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void study(){
System.out.println("学生在学习");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.ding.reflect2;
/**
* @Description 获取class对象的三种方式
* @Author 丁帅帅
* @Date 21/07/23 11:38
* @Version 1.0
*/
public class ReflectDemo {
public static void main(String[] args) throws Exception {
//1.Class类中的静态方法forName("全类名)
//全类名=包名+类名
Class clazz = Class.forName("com.ding.reflect2.Student");
System.out.println(clazz);
//2.通过class属性来获取
Class<Student> clazz2 = Student.class;
System.out.println(clazz2);
//3.通过对象的getClass方法来获取class对象
//getClass方法是定义在Object类中
Student student = new Student();
Class clazz3 = student.getClass();
System.out.println(clazz3);
System.out.println(clazz == clazz2);
System.out.println(clazz2 == clazz3);
}
}
reflect3(获取1Constructor对象并创建对象)
package com.ding.reflect3;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 16:22
* @Version 1.0
*/
public class Student {
private String name;
private int age;
//私有的有参构造方法
private Student(String name){
System.out.println("name的值为:"+name);
System.out.println("private ..Student..有参构造方法");
}
//公共的无参构造方法
public Student() {
System.out.println("public...Student...无参构造方法");
}
//公共的有参构造方法
public Student(String name, int age) {
System.out.println("name的值为:" + name + "age的值为:" + age);
System.out.println("public...Student...有参构造方法");
}
}
package com.ding.reflect3;
import java.lang.reflect.Constructor;
/**
* @Description 获取Constructor对象
* @Author 丁帅帅
* @Date 21/07/23 16:26
* @Version 1.0
*/
public class ReflectDemo1 {
public static void main(String[] args) throws Exception{
// method1();
//method2();
//method3();
//返回单个构造方法对象
Class clazz = Class.forName("com.ding.reflect3.Student");
Constructor constructor = clazz.getDeclaredConstructor(String.class);
System.out.println(constructor);
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException {
// 返回单个公共构造方法对象
Class clazz = Class.forName("com.ding.reflect3.Student");
Constructor constructor1 = clazz.getConstructor();
System.out.println(constructor1);
Constructor constructor2 = clazz.getConstructor(String.class, int.class);
System.out.println(constructor2);
}
private static void method2() throws ClassNotFoundException {
//返回所有构造方法对象的数组
Class clazz = Class.forName("com.ding.reflect3.Student");
Constructor[] constructors = clazz.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
private static void method1() throws ClassNotFoundException {
// 返回所有公共构造方法对象的数组
Class clazz = Class.forName("com.ding.reflect3.Student");
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
}
}
package com.ding.reflect3;
import java.lang.reflect.Constructor;
/**
* @Description 获取1Constructor对象并创建对象
* @Author 丁帅帅
* @Date 21/07/23 16:45
* @Version 1.0
*/
public class ReflectDemo2 {
public static void main(String[] args) throws Exception {
// method1();
//method2();
//method3();
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
//获取一个私有的构造方法并创建对象
Class<?> clazz = Class.forName("com.ding.reflect3.Student");
Constructor<?> constructor = clazz.getDeclaredConstructor(String.class);
//被private修饰的成员,不能直接使用
//如果用反射强行获取并使用,需要临时取消访问检查
constructor.setAccessible(true);
Student stu = (Student) constructor.newInstance("lisi");
System.out.println(stu);
}
private static void method2() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
Class<?> clazz = Class.forName("com.ding.reflect3.Student");
Constructor<?> constructor = clazz.getConstructor();
Student stu = (Student) constructor.newInstance();
System.out.println(stu);
}
private static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
//1.获取class对象
Class<?> clazz = Class.forName("com.ding.reflect3.Student");
//2.获取构造方法对象
Constructor<?> constructor = clazz.getConstructor(String.class, int.class);
//3.利用newInstance创建Student对象
Student student = (Student) constructor.newInstance("zhangsan", 23);
System.out.println(student);
}
}
reflect4(获取Field对象)
package com.ding.reflect4;
/**
* @Description TODO
* @Author 丁帅帅
* @Date 21/07/23 16:43
* @Version 1.0
*/
public class Student {
public String name;
public int age;
public String gender;
private int money = 300;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", money=" + money +
'}';
}
}
package com.ding.reflect4;
import java.lang.reflect.Field;
/**
* @Description 获取Field对象
* @Author 丁帅帅
* @Date 21/07/23 17:01
* @Version 1.0
*/
public class ReflectDemo1 {
public static void main(String[] args) throws Exception {
// method1();
//method2();
//method3();
//返回单个成员变量对象
Class<?> clazz = Class.forName("com.ding.reflect4.Student");
Field money = clazz.getDeclaredField("money");
System.out.println(money);
}
private static void method3() throws ClassNotFoundException, NoSuchFieldException {
//返回单个公共成员变量对象
Class<?> clazz = Class.forName("com.ding.reflect4.Student");
Field name = clazz.getField("name");
System.out.println(name.getName());
}
private static void method2() throws ClassNotFoundException {
//返回所有成员变量对象的数组
Class<?> clazz = Class.forName("com.ding.reflect4.Student");
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
}
private static void method1() throws ClassNotFoundException {
//Field[] getFields():返回所有公共成员变量对象的数组
Class<?> clazz = Class.forName("com.ding.reflect4.Student");
Field[] field = clazz.getFields();
for (Field field1 : field) {
System.out.println(field1);
}
}
}
package com.ding.reflect4;
import java.lang.reflect.Field;
/**
* @Description 利用Field对象,获取值或者修改值
* @Author 丁帅帅
* @Date 21/07/23 17:12
* @Version 1.0
*/
public class ReflectDemo2 {
public static void main(String[] args) throws Exception{
// method1();
Class<?> clazz = Class.forName("com.ding.reflect4.Student");
Field field = clazz.getDeclaredField("money");
field.setAccessible(true);
Student stu = (Student) clazz.newInstance();
Object o = field.get(stu);
field.set(stu,400);
System.out.println(o);
System.out.println(stu);
}
private static void method1() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
//给obj对象的成员变量赋值为value
//1.获取class对象
Class<?> clazz = Class.forName("com.ding.reflect4.Student");
//2.获取name这个Field对象
Field field = clazz.getField("name");
//3.利用set方法进行赋值
//3.1先创建一个Student对象
Student stu = (Student) clazz.newInstance();
//3.2对指定对象进行赋值
field.set(stu,"ding");
System.out.println(stu);
}
}