反射&模块化
1.类加载器
1.1 类加载
1.2 类加载器
ClassLoaderDemo
/*
ClassLoader 中的两个方法
static ClassLoader getSystemClassLoader():返回用于委派的系统类加载器
ClassLoader getParent():返回父类加载器进行委派
*/
public class ClassLoaderDemo {
public static void main(String[] args) {
//static ClassLoader getSystemClassLoader():返回用于委派的系统类加载器
ClassLoader c = ClassLoader.getSystemClassLoader();
System.out.println(c); //AppClassLoader
//ClassLoader getParent():返回父类加载器进行委派
ClassLoader c2 = c.getParent();
System.out.println(c2); //PlatformClassLoader
ClassLoader c3 = c2.getParent();
System.out.println(c3); //null
}
}
2.反射
2.1 反射概述
2.2 获取class类的对象
Student
public class Student {
//成员变量:一个私有,一个默认,一个公共
private String name;
int age;
public String address;
//构造方法:一个私有,一个默认,两个公共
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//成员方法:一个私有,四个公共
private void function() {
System.out.println("function");
}
public void method1() {
System.out.println("method");
}
public void method2(String s) {
System.out.println("method:" + s);
}
public String method3(String s, int i) {
return s + "," + i;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
ReflectDemo
/*
三种方式获取Class对象
1:使用类的class属性来获取该类对应的Class对象。举例:Student.class将会返回Student类对应的Class对象
2:调用对象的getClass()方法,返回该对象所属类对应的Class对象
该方法是Object类中的方法,所有的Java对象都可以调用该方法
3:使用Class类中的静态方法forName(String className),该方法需要传入字符串参数,该字符串参数的值是某个类的全路径,也就是完整包名的路径
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException {
//使用类的class属性来获取该类对应的Class对象
Class<Student> c1 = Student.class;
System.out.println(c1);
Class<Student> c2 = Student.class;
System.out.println(c1 == c2);
System.out.println("--------");
//调用对象的getClass()方法,返回该对象所属类对应的Class对象
Student s = new Student();
Class<? extends Student> c3 = s.getClass();
System.out.println(c1 == c3);
System.out.println("--------");
//使用Class类中的静态方法forName(String className)
Class<?> c4 = Class.forName("com.itheima_02.Student");
System.out.println(c1 == c4);
}
}
2.3 反射获取构造方法并使用
public class Student {
//成员变量:一个私有,一个默认,一个公共
private String name;
int age;
public String address;
//构造方法:一个私有,一个默认,两个公共
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//成员方法:一个私有,四个公共
private void function() {
System.out.println("function");
}
public void method1() {
System.out.println("method");
}
public void method2(String s) {
System.out.println("method:" + s);
}
public String method3(String s, int i) {
return s + "," + i;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
ReflectDemo01
/*
反射获取构造方法并使用
*/
public class ReflectDemo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//Constructor<?>[] getConstructors() 返回一个包含 Constructor对象的数组, Constructor对象反映了由该 Class对象表示的类的所有公共构造函数
// Constructor<?>[] cons = c.getConstructors();
//Constructor<?>[] getDeclaredConstructors() 返回反映由该 Class对象表示的类声明的所有构造函数的 Constructor对象的数组
Constructor<?>[] cons = c.getDeclaredConstructors();
for(Constructor con : cons) {
System.out.println(con);
}
System.out.println("--------");
//Constructor<T> getConstructor(Class<?>... parameterTypes) 返回一个 Constructor对象,该对象反映由该 Class对象表示的类的指定公共构造函数
//Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 返回一个 Constructor对象,该对象反映由此 Class对象表示的类或接口的指定构造函数
//参数:你要获取的构造方法的参数的个数和数据类型对应的字节码文件对象
Constructor<?> con = c.getConstructor();
//Constructor提供了一个类的单个构造函数的信息和访问权限
//T newInstance(Object... initargs) 使用由此 Constructor对象表示的构造函数,使用指定的初始化参数来创建和初始化构造函数的声明类的新实例
Object obj = con.newInstance();
System.out.println(obj);
// Student s = new Student();
// System.out.println(s);
}
}
2.3.1 反射获取构造方法并使用(练习1)
Student
package com.itheima_02;
public class Student {
//成员变量:一个私有,一个默认,一个公共
private String name;
int age;
public String address;
//构造方法:一个私有,一个默认,两个公共
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//成员方法:一个私有,四个公共
private void function() {
System.out.println("function");
}
public void method1() {
System.out.println("method");
}
public void method2(String s) {
System.out.println("method:" + s);
}
public String method3(String s, int i) {
return s + "," + i;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
ReflectDemo02
/*
通过反射实现如下的操作:
Student s = new Student("林青霞",30,"西安");
System.out.println(s);
*/
public class ReflectDemo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//public Student(String name, int age, String address)
//Constructor<T> getConstructor(Class<?>... parameterTypes)
Constructor<?> con = c.getConstructor(String.class, int.class, String.class);
//基本数据类型也可以通过.class得到对应的Class类型
//T newInstance(Object... initargs)
Object obj = con.newInstance("林青霞", 30, "西安");
System.out.println(obj);
}
}
2.3.2 反射获取构造方法并使用(练习2)
/*
通过反射实现如下的操作:
Student s = new Student("林青霞");
System.out.println(s);
*/
public class ReflectDemo03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//private Student(String name)
//Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
Constructor<?> con = c.getDeclaredConstructor(String.class);
//暴力反射,可以使用私有构造方法创建对象
//public void setAccessible(boolean flag):值为true,取消访问检查
con.setAccessible(true);
Object obj = con.newInstance("林青霞");
System.out.println(obj);
}
}
2.4 反射获取成员变量并使用
/*
反射获取成员变量并使用
*/
public class ReflectDemo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//Field[] getFields() 返回一个包含 Field对象的数组, Field对象反映由该 Class对象表示的类或接口的所有可访问的公共字段
//Field[] getDeclaredFields() 返回一个 Field对象的数组,反映了由该 Class对象表示的类或接口声明的所有字段
// Field[] fields = c.getFields();
Field[] fields = c.getDeclaredFields();
for(Field field : fields) {
System.out.println(field);
}
System.out.println("--------");
//Field getField(String name) 返回一个 Field对象,该对象反映由该 Class对象表示的类或接口的指定公共成员字段
//Field getDeclaredField(String name) 返回一个 Field对象,该对象反映由该 Class对象表示的类或接口的指定声明字段
Field addressField = c.getField("address");
//获取无参构造方法创建对象
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
// obj.addressField = "西安";
//Field提供有关类或接口的单个字段的信息和动态访问
//void set(Object obj, Object value) 将指定的对象参数中由此 Field对象表示的字段设置为指定的新值
addressField.set(obj,"西安"); //给obj的成员变量addressField赋值为西安
System.out.println(obj);
// Student s = new Student();
// s.address = "西安";
// System.out.println(s);
}
}
2.4.1 反射获取成员变量并使用(练习)
/*
练习:通过反射实现如下操作
Student s = new Student();
s.name = "林青霞";
s.age = 30;
s.address = "西安";
System.out.println(s);
*/
public class ReflectDemo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//Student s = new Student();
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
System.out.println(obj);
//s.name = "林青霞";
// Field nameField = c.getField("name"); //NoSuchFieldException: name
Field nameField = c.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(obj, "林青霞");
System.out.println(obj);
//s.age = 30;
Field ageField = c.getDeclaredField("age");
ageField.setAccessible(true);
ageField.set(obj,30);
System.out.println(obj);
//s.address = "西安";
Field addressField = c.getDeclaredField("address");
addressField.setAccessible(true);
addressField.set(obj,"西安");
System.out.println(obj);
}
}
2.5 反射获取成员方法并使用
Student
package com.itheima_02;
public class Student {
//成员变量:一个私有,一个默认,一个公共
private String name;
int age;
public String address;
//构造方法:一个私有,一个默认,两个公共
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//成员方法:一个私有,四个公共
private void function() {
System.out.println("function");
}
public void method1() {
System.out.println("method");
}
public void method2(String s) {
System.out.println("method:" + s);
}
public String method3(String s, int i) {
return s + "," + i;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
ReflectDemo01
/*
反射获取成员方法并使用
*/
public class ReflectDemo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//Method[] getMethods() 返回一个包含 方法对象的数组, 方法对象反映由该 Class对象表示的类或接口的所有公共方法,包括由类或接口声明的对象以及从超类和超级接口继承的类
//Method[] getDeclaredMethods() 返回一个包含 方法对象的数组, 方法对象反映由 Class对象表示的类或接口的所有声明方法,包括public,protected,default(package)访问和私有方法,但不包括继承方法
// Method[] methods = c.getMethods();
Method[] methods = c.getDeclaredMethods();
for(Method method : methods) {
System.out.println(method);
}
System.out.println("--------");
//Method getMethod(String name, Class<?>... parameterTypes) 返回一个 方法对象,该对象反映由该 Class对象表示的类或接口的指定公共成员方法
//Method getDeclaredMethod(String name, Class<?>... parameterTypes) 返回一个 方法对象,它反映此表示的类或接口的指定声明的方法 Class对象
//public void method1()
Method m = c.getMethod("method1");
//获取无参构造方法创建对象
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
// obj.m();
//在类或接口上提供有关单一方法的信息和访问权限
//Object invoke(Object obj, Object... args) 在具有指定参数的指定对象上调用此 方法对象表示的基础方法
//Object:返回值类型
//obj:调用方法的对象
//args:方法需要的参数
m.invoke(obj);
// Student s = new Student();
// s.method1();
}
}
2.5.1 反射获取成员方法并使用
/*
练习:通过反射实现如下操作
Student s = new Student();
s.method1();
s.method2("林青霞");
String ss = s.method3("林青霞",30);
System.out.println(ss);
s.function();
*/
public class ReflectDemo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.itheima_02.Student");
//Student s = new Student();
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
//s.method1();
Method m1 = c.getMethod("method1");
m1.invoke(obj);
//s.method2("林青霞");
Method m2 = c.getMethod("method2", String.class);
m2.invoke(obj,"林青霞");
// String ss = s.method3("林青霞",30);
// System.out.println(ss);
Method m3 = c.getMethod("method3", String.class, int.class);
Object o = m3.invoke(obj, "林青霞", 30);
System.out.println(o);
//s.function();
// Method m4 = c.getMethod("function"); //NoSuchMethodException: com.itheima_02.Student.function()
Method m4 = c.getDeclaredMethod("function");
m4.setAccessible(true);//取消访问检查
m4.invoke(obj);
}
}
2.6 反射练习
2.6.1 越过泛型检查(通过Object.class实现)
/*
练习1:我有一个ArrayList<Integer>集合,现在我想在这个集合中添加一个字符串数据,如何实现?
*/
public class ReflectTest01 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//创建集合
ArrayList<Integer> array = new ArrayList<Integer>();
// array.add(10);
// array.add(20);
// array.add("hello");
Class<? extends ArrayList> c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array,"hello");
m.invoke(array,"world");
m.invoke(array,"java");
System.out.println(array);
}
}
2.6.2 运行配置文件指定内容
Student
public class Student {
public void study() {
System.out.println("好好学习天天向上");
}
}
Teacher
public class Teacher {
public void teach() {
System.out.println("用爱成就学员");
}
}
ReflectTest02
/*
练习2:通过配置文件运行类中的方法
*/
public class ReflectTest02 {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// Student s = new Student();
// s.study();
// Teacher t = new Teacher();
// t.teach();
/*
class.txt
className=xxx
methodName=xxx
*/
//加载数据
Properties prop = new Properties();
FileReader fr = new FileReader("myReflect\\class.txt");
prop.load(fr);
fr.close();
/*
className=com.itheima_06.Student
methodName=study
*/
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
//通过反射来使用
Class<?> c = Class.forName(className);//com.itheima_06.Student
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName);//study
m.invoke(obj);
}
}
3.模块化
3.1 模块化概述
3.2 模块的基本使用
3.3 模块服务的使用
myOne
Student
package com.itheima_01;
public class Student {
public void study() {
System.out.println("好好学习天天向上");
}
}
Teacher
package com.itheima_02;
public class Teacher {
public void teach() {
System.out.println("用爱成就学员");
}
}
MyService
package com.itheima_03;
public interface MyService {
void service();
}
Czxy
package com.itheima_03.impl;
import com.itheima_03.MyService;
public class Czxy implements MyService {
@Override
public void service() {
System.out.println("上大学");
}
}
Itheima
package com.itheima_03.impl;
import com.itheima_03.MyService;
public class Itheima implements MyService {
@Override
public void service() {
System.out.println("学IT");
}
}
module-info.java
import com.itheima_03.MyService;
import com.itheima_03.impl.Czxy;
import com.itheima_03.impl.Itheima;
module myOne {
exports com.itheima_01;
exports com.itheima_03;
// provides MyService with Itheima;
provides MyService with Czxy;
}
myTwo
module-info.java
import com.itheima_03.MyService;
module myTwo {
requires myOne;
uses MyService;
}
Test01
package cn.itcast;
import com.itheima_01.Student;
//import com.itheima_02.Teacher;
public class Test01 {
public static void main(String[] args) {
Student s = new Student();
s.study();
// Teacher t = new Teacher();
}
}
Test02
package cn.itcast;
import com.itheima_03.MyService;
import java.util.ServiceLoader;
public class Test02 {
public static void main(String[] args) {
//加载服务
ServiceLoader<MyService> myServices = ServiceLoader.load(MyService.class);
//遍历服务
for(MyService my : myServices) {
my.service();
}
}
}