类加载:
类加载器
package com.FunctionInterface;
public class ClassLoaderDemo {
public static void main(String[] args) {
ClassLoader c = ClassLoader.getSystemClassLoader();
System.out.println(c);//jdk.internal.loader.ClassLoaders$AppClassLoader@2f0e140b
ClassLoader c2 = c.getParent();
System.out.println(c2);//jdk.internal.loader.ClassLoaders$PlatformClassLoader@16b98e56
ClassLoader c3 = c2.getParent();
System.out.println(c3);//null
}
}
反射
反射概述
三种获取class对象的方法:
1.使用类的class属性来获取该类对应的class对象
2.调用对象的getClass()方法,返回该对象所属类对应的Class对象,该方法是Object类中的方法,所有的Java对象都可以调用该方法
3.使用Class类中的静态方法forName(String className),该方法需要传入字符串参数,该字符串参数是某个类的全路径
package com.FunctionInterface;
public class ReflctDemo {
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);
Student s = new Student();
Class<? extends Student> c3 = s.getClass();
System.out.println(c1==c3);
Class<?> c4 = Class.forName("com.FunctionInterface.Student");
System.out.println(c1==c4);
}
}
反射获取构造方法并使用
package com.FunctionInterface;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取Class对象
Class<?> c = Class.forName("com.FunctionInterface.Student");
//Constructor<?>[] cons = c.getConstructors();
Constructor<?>[] cons = c.getDeclaredConstructors();
for(Constructor con:cons){
System.out.println(con);
}
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
System.out.println(obj);
}
}
反射获取构造方法并使用练习
package com.FunctionInterface;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("com.FunctionInterface.Student");
Constructor<?> con = c.getConstructor(String.class, int.class, String.class);
Object obj = con.newInstance("zayn", 25, "changchun");
System.out.println(obj);
}
}
基本数据类型也可以通过.class得到对应的Class类型
package com.FunctionInterface;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("com.FunctionInterface.Student");
Constructor<?> con = c.getDeclaredConstructor(String.class);
//暴力反射
//public void setAccessible(boolean flag):值为true,取消访问检查
con.setAccessible(true);
Object obj = con.newInstance("zayn");
System.out.println(obj);
}
}
public void setAccessible(boolean flag):值为true,取消访问检查。
反射获取成员变量并使用
package com.FunctionInterface;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class ReflectDemo4 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("com.FunctionInterface.Student");
// Field[] fies = c.getDeclaredFields();
// for(Field fie:fies){
// System.out.println(fie);
// }
// Field address = c.getField("address");
// Constructor<?> con = c.getConstructor();
// Object obj = con.newInstance();
// address.set(obj,"长春");
// System.out.println(obj);
Constructor<?> con = c.getConstructor();
Field name = c.getDeclaredField("name");
name.setAccessible(true);
Object obj = con.newInstance();
name.set(obj,"zayn");
System.out.println(obj);
Field age = c.getDeclaredField("age");
age.setAccessible(true);
Object obj2 = con.newInstance();
age.set(obj2,24);
System.out.println(obj2);
Field address = c.getDeclaredField("address");
address.setAccessible(true);
Object obj3 = con.newInstance();
address.set(obj3,"zayn");
System.out.println(obj3);
}
}
反射获取成员方法并使用
package com.FunctionInterface;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectDemo5 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> c = Class.forName("com.FunctionInterface.Student");
Method[] methods = c.getDeclaredMethods();
for(Method method:methods){
System.out.println(method);
}
Method method1 = c.getMethod("method1");
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
method1.invoke(obj);
System.out.println(method1);
}
}
反射练习
练习1:我有一个ArrayList<Integer>集合,现在我想在这个集合中添加一个字符串的数据,如何实现?
package com.FunctionInterface;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class ReflectDemo6 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ArrayList<Integer> array = new ArrayList<>();
Class<? extends ArrayList> c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array,"hello");
m.invoke(array,"java");
m.invoke(array,10);
System.out.println(array);
System.out.println(array.get(2)+9);
}
}
练习2:通过配置文件运行类中的方法
配置文件如下:
package com.Reflect;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class ReflectDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//加载数据
Properties prop = new Properties();
FileReader fr = new FileReader("E:\\InteliJ IDEA workspace\\idea_test\\src\\com\\class.txt");
prop.load(fr);
fr.close();
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
Class<?> c = Class.forName(className);
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName);
m.invoke(obj);
}
}
模块
模块化概述
模块的基本使用
模块的基本使用步骤:
1.创建模块(按照以前的方式创建包,创建类,定义方法),为了体现模块的使用,我们创建2个模块,一个是myOne,一个是myTwo
2.在模块的src目录下新建一个名为module-info.java的描述文件,改文件专门定义模块名,访问权限,模块依赖等信息,描述文件中使用模块导出和模块依赖来进行配置使用
3.模块中所有未导出的包都是模块私有的,他们不能在模块之外被访问,在myOne这个模块下的描述文件中配置模块导出,模块导出格式:exports 包名;
4.一个模块想要访问其他模块,必须明确指定依赖哪些模块,未明确指定依赖的模块不能访问,在myTwo这个模块下的描述文件中配置模块依赖,模块依赖格式:requires 模块名;
注意:写模块名报错,需要按下Alt+Enter提示,然后选择模块依赖
5.在myTwo这个模块的类中使用依赖模块下的内容
模块服务的使用
使用步骤:
1.在myOne模块下创建一个包com.test,在该包下提供一个接口,接口中定义一个抽象方法
public interface MyService{
void service();
}
2.在com.test包下创建一个包impl,在该包下提供接口的两个实现类,impl1,impl2。
3.在myOne这个模块下的描述文件中添加如下配置:
模块导出:exports com.test;
服务提供:provides MyService with impl1; 指定MyService的服务实现类是impl2
4.在myTwo这个模块下的描述文件中,添加如下配置
声明服务接口:uses MyService;
5.在myTwo 这个模块中使用MyService接口提供服务,ServiceLoader:一种加载服务实现的工具。