Comparable 重写 compareTo两个参数报错—排序

  1. public class ComparatorDemo {
  2. public static void main(String[] args) {
  3. ArrayList<String> arr = new ArrayList<>();
  4. arr.add("aaa");
  5. arr.add("bb");
  6. arr.add("cccc");
  7. arr.add("d");
  8. System.out.println(arr);
  9. //Collections.sort(arr,getComparator());
  10. System.out.println(arr);
  11. }
  12. private static Comparable<String> getComparator(){
  13. // return new Comparable<String>() {
  14. // @Override
  15. // public int compareTo(String s2,String s1) {
  16. // return s2.length()-s1.length();
  17. // }
  18. // };
  19. //不知道为啥报错,它不允许两个参数
  20. return (s1,s2) -> s1.length()-s2.length();
  21. }
  22. }

Supplier函数式接口

image.png

  1. import java.util.function.Supplier;
  2. public class SupplierDemo {
  3. public static void main(String[] args) {
  4. String s = getString(() -> "柳夜熙") ;
  5. System.out.println(s);
  6. //>>>柳夜熙
  7. Integer i = getInteger(()->80);
  8. System.out.println(i);
  9. //>>>80
  10. }
  11. private static String getString(Supplier<String> s) {
  12. return s.get();
  13. }
  14. private static Integer getInteger(Supplier<Integer> s) {
  15. return s.get();
  16. }
  17. }
  1. import java.util.function.Supplier;
  2. public class SuplierTest {
  3. public static void main(String[] args) {
  4. int[] arr = {88, 1, 2, 3, 5, 8, 13, 21, 34};
  5. int Max = getMax(() -> {
  6. int max = arr[0];
  7. for (int i : arr) {
  8. if (i > max) {
  9. max = i;
  10. }
  11. }
  12. return max;
  13. });
  14. System.out.println(Max);
  15. }
  16. private static int getMax(Supplier<Integer> sup) {
  17. return sup.get();
  18. }
  19. }

Consumer函数式接口

image.png

  1. import java.util.function.Consumer;
  2. public class ConsumerDemo {
  3. public static void main(String[] args) {
  4. operatorString("柳夜熙", s -> System.out.println(s));
  5. operatorString("青染", System.out::println);
  6. operatorString("反着读", s -> System.out.println(new StringBuilder(s).reverse().toString()));
  7. operatorString("牙刷", s -> System.out.println(s), s -> System.out.println(new StringBuilder(s).reverse().toString()));
  8. }
  9. private static void operatorString(String name, Consumer<String> con) {
  10. con.accept(name);
  11. }
  12. private static void operatorString(String name, Consumer<String> con1, Consumer<String> con2) {
  13. /* 方式一:
  14. con1.accept(name);
  15. con2.accept(name);
  16. */
  17. //方式二:
  18. con1.andThen(con2).accept(name);
  19. /* 源码 andThen
  20. default Consumer<T> andThen(Consumer<? super T> after) {
  21. Objects.requireNonNull(after);
  22. return (T t) -> { accept(t); after.accept(t); };
  23. }
  24. */
  25. }
  26. }
  1. import java.util.function.Consumer;
  2. public class ConsumerTest {
  3. public static void main(String[] args) {
  4. String[] strArr = {"张飞,35", "武则天,28", "孙悟空,500"};
  5. printInfo(strArr, s -> {
  6. String name = s.split(",")[0];
  7. System.out.print(name);
  8. }, s -> {
  9. Integer age = Integer.parseInt(s.split(",")[1]);
  10. System.out.println(age);
  11. });
  12. }
  13. //>>> 张飞35
  14. // 武则天28
  15. // 孙悟空500
  16. private static void printInfo(String[] strArr, Consumer<String> con1, Consumer<String> con2) {
  17. for (String s : strArr) {
  18. con1.andThen(con2).accept(s);
  19. }
  20. }
  21. }

Predicate函数式接口

image.png

  1. import java.util.function.Predicate;
  2. public class PredicateDemo {
  3. public static void main(String[] args) {
  4. Boolean b1 = checkString("Hello", s -> s.length() > 8);
  5. System.out.println(b1);
  6. //>>> false
  7. Boolean b2 = checkString("Hello", s -> s.length() < 8);
  8. System.out.println(b2);
  9. //>>> true
  10. boolean b3 = checkString("world", s -> s.length() > 5, s -> s.length() < 5);
  11. System.out.println(b3);
  12. //>>> false
  13. }
  14. private static boolean checkString(String s, Predicate<String> pre1,Predicate<String> pre2) {
  15. /*boolean b1 = pre1.test(s);
  16. boolean b2 = pre2.test(s);
  17. boolean b = b1 && b2;
  18. return b;*/
  19. //return pre1.and(pre2).test(s); //逻辑与
  20. return pre1.or(pre2).test(s); //逻辑或
  21. /**
  22. * default Predicate<T> or(Predicate<? super T> other) {
  23. * Objects.requireNonNull(other);
  24. * return (t) -> test(t) || other.test(t);
  25. * }
  26. */
  27. }
  28. private static boolean checkString(String s, Predicate<String> pre) {
  29. //return pre.test(s);
  30. //取反
  31. //return !pre.test(s);
  32. return pre.negate().test(s); //逻辑非
  33. /*
  34. default Predicate<T> negate() {
  35. return (t) -> !test(t);
  36. }
  37. */
  38. }
  39. }

predicate条件筛选

  1. import java.util.ArrayList;
  2. import java.util.function.Predicate;
  3. /**
  4. * 条件筛选 --> 名字长度大于2,年龄大于30
  5. */
  6. public class PredicateTest {
  7. public static void main(String[] args) {
  8. String[] strArr = {"张伟,40", "曾小贤,38", "胡一菲,28", "唐悠悠,25"};
  9. ArrayList<String> arr = myFilter(strArr, s -> s.split(",")[0].length() > 2, s -> Integer.parseInt(s.split(",")[1]) > 30);
  10. System.out.println(arr);
  11. //>>> [曾小贤,38]
  12. }
  13. private static ArrayList<String> myFilter(String[] strArr, Predicate<String> pre1, Predicate<String> pre2) {
  14. ArrayList<String> arr = new ArrayList<>();
  15. for (String s : strArr) {
  16. if (pre1.and(pre2).test(s)) {
  17. arr.add(s);
  18. }
  19. }
  20. return arr;
  21. }
  22. }

Function函数式接口

image.png

  1. import java.util.function.Function;
  2. public class FunctionDemo {
  3. public static void main(String[] args) {
  4. convert("100", s -> Integer.parseInt(s));
  5. convert("100", Integer::parseInt);
  6. convert(888, i -> i.toString());
  7. convert(888, i -> String.valueOf(i + 666));
  8. convert("2", s -> Integer.parseInt(s) * 10, i -> i.toString());
  9. }
  10. //字符串转int类型
  11. private static void convert(String s, Function<String, Integer> func) {
  12. Integer i = func.apply(s);
  13. System.out.println(i);
  14. //>>> 100
  15. // 100
  16. }
  17. //int转字符串
  18. private static void convert(int i, Function<Integer, String> func) {
  19. String s = func.apply(i);
  20. System.out.println(s);
  21. //>>> 888
  22. // 1554
  23. }
  24. //字符串转int类型,做乘法后,再转字符串
  25. private static void convert(String s, Function<String, Integer> func1, Function<Integer, String> func2) {
  26. Integer i = func1.apply(s);
  27. String ss = func2.apply(i);
  28. System.out.println(ss);
  29. //>>> 20
  30. //方式二
  31. String ss1 = func1.andThen(func2).apply(s);
  32. System.out.println(ss1);
  33. //>>> 20
  34. /**
  35. * default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
  36. * Objects.requireNonNull(after);
  37. * return (T t) -> after.apply(apply(t));
  38. * }
  39. */
  40. }
  41. }
  1. import java.util.function.Function;
  2. public class FunctionTest {
  3. public static void main(String[] args) {
  4. useFunction("租八戒,300",s -> Integer.parseInt(s.split(",")[1])+700,i -> new StringBuilder("租八戒,").append(i).toString());
  5. }
  6. private static void useFunction(String s, Function<String,Integer> fun1,Function<Integer,String> fun2) {
  7. String ss = fun1.andThen(fun2).apply(s);
  8. System.out.println(ss);
  9. //>>> 租八戒,1000
  10. }
  11. }

Stream流

image.png
普通方式

  1. import java.util.ArrayList;
  2. public class StreamDemo {
  3. public static void main(String[] args) {
  4. String[] strArr = {"张国荣", "张伟", "李张洋", "张飞", "王红", "张三", "张惠妹"};
  5. ArrayList<String> arrZ = new ArrayList<>(); //筛选姓张的
  6. for (String s : strArr) {
  7. // if (s.substring(0,1).equals("张")){
  8. // arrZ.add(s);
  9. // }
  10. if (s.startsWith("张")){
  11. arrZ.add(s);
  12. }
  13. }
  14. System.out.println(arrZ);
  15. ArrayList<String> arrZ3 = new ArrayList<>();
  16. for (String s: arrZ){
  17. if (s.length()>2){
  18. arrZ3.add(s);
  19. }
  20. }
  21. System.out.println(arrZ3);
  22. }
  23. }

使用Stream流

  1. public class StreamTest {
  2. public static void main(String[] args) {
  3. ArrayList<String> strArr = new ArrayList<>();
  4. //String[] strArr = {"张国荣", "张伟", "李张洋", "张飞", "王红", "张三", "张惠妹"};
  5. strArr.add("张国荣");
  6. strArr.add("张伟");
  7. strArr.add("李张洋");
  8. strArr.add("张飞");
  9. strArr.add("王红");
  10. strArr.add("张三");
  11. strArr.add("张惠妹");
  12. strArr.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() > 2).forEach(s -> System.out.println(s));
  13. System.out.println("----");
  14. strArr.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() > 2).forEach(System.out::println);
  15. }
  16. }
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.stream.Stream;
  4. public class StreamTest {
  5. public static void main(String[] args) {
  6. //Collections 体系 直接生成
  7. ArrayList<String> strArr = new ArrayList<>();
  8. strArr.add("张国荣");
  9. strArr.add("张伟");
  10. strArr.add("李张洋");
  11. strArr.add("张飞");
  12. strArr.add("王红");
  13. strArr.add("张三");
  14. strArr.add("张惠妹");
  15. strArr.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() > 2).forEach(s -> System.out.println(s));
  16. System.out.println("----");
  17. strArr.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() > 2).forEach(System.out::println);
  18. System.out.println("----");
  19. //数组
  20. String[] strArr1 = {"张国荣", "张伟", "李张洋", "张飞", "王红", "张三", "张惠妹"};
  21. Stream.of(strArr1).filter(s -> s.startsWith("张")).filter(s -> s.length() > 2).forEach(System.out::println);
  22. //Map体系集合 间接生成流
  23. HashMap<Integer, String> map = new HashMap<>();
  24. map.put(1, "张国荣");
  25. map.put(2, "张伟");
  26. map.put(3, "李张洋");
  27. map.put(4, "张飞");
  28. map.put(5, "王红");
  29. map.put(6, "张三");
  30. map.put(7, "张惠妹");
  31. //map.keySet().stream().filter(i -> i>3).forEach(System.out::println);
  32. map.values().stream().filter(s -> s.startsWith("张")).filter(s -> s.length() > 2).forEach(System.out::println);
  33. }
  34. }

image.png

Stream 方法

  1. import java.util.ArrayList;
  2. import java.util.stream.Stream;
  3. public class StreamDemo01 {
  4. public static void main(String[] args) {
  5. ArrayList<String> strArr = new ArrayList<>();
  6. strArr.add("张国荣");
  7. strArr.add("张伟");
  8. strArr.add("李张洋");
  9. strArr.add("张飞");
  10. strArr.add("王红");
  11. strArr.add("张三");
  12. strArr.add("张惠妹");
  13. //取 前5个元素
  14. Stream<String> limit = strArr.stream().limit(5);
  15. //limit.forEach(System.out::println);
  16. //跳过 前2个元素
  17. Stream<String> skip = strArr.stream().skip(2);
  18. //skip.forEach(System.out::println);
  19. System.out.println("----");
  20. //concat 连接两个流
  21. //Stream.concat(limit, skip).forEach(System.out::println);
  22. //>>> 抛出异常:stream has already been operated upon or closed
  23. //原因 : 前方的limit和skip流 已使用了forEach关闭.
  24. //distinct 去重
  25. Stream.concat(limit, skip).distinct().forEach(System.out::println);
  26. //int[] arr = {7, 5, 9, 2, 6, 3, 5, 1, 2, 8, 4}; -->
  27. // Redundant 'sorted' call: stream contains at most one element
  28. Integer[] arr = {7, 5, 9, 2, 6, 3, 5, 1, 8, 4};
  29. Stream.of(arr).sorted().forEach(System.out::print);
  30. System.out.println();
  31. String[] sarr = {"b", "d", "c", "f", "a", "e", "g"};
  32. Stream.of(sarr).sorted().forEach(System.out::print);
  33. System.out.println();
  34. Stream.of(sarr).map(s -> stringToAscii(s)).forEach(s -> System.out.print(s+"+"));
  35. //求和
  36. int sum = Stream.of(sarr).map(s -> stringToAscii(s)).mapToInt(Integer::parseInt).sum();
  37. System.out.println("\n"+sum);
  38. //统计个数
  39. long count = Stream.of(sarr).count();
  40. System.out.println(count);
  41. }
  42. /**
  43. * 字符串转换为Ascii
  44. *
  45. * @param value
  46. * @return
  47. */
  48. public static String stringToAscii(String value) {
  49. StringBuffer sbu = new StringBuffer();
  50. char[] chars = value.toCharArray();
  51. for (int i = 0; i < chars.length; i++) {
  52. if (i != chars.length - 1) {
  53. sbu.append((int) chars[i]).append(",");
  54. } else {
  55. sbu.append((int) chars[i]);
  56. }
  57. }
  58. return sbu.toString();
  59. }
  60. }

Stream收集操作

image.png

类加载

image.png
image.png
image.png
image.png

  1. public class ClassloaderDemo {
  2. public static void main(String[] args) {
  3. ClassLoader c = ClassLoader.getSystemClassLoader();
  4. System.out.println(c);
  5. //>>> Launcher$AppClassLoader 系统类加载器
  6. ClassLoader c1 = c.getParent(); //返回父类加载器
  7. System.out.println(c1);
  8. //>>> Launcher$ExtClassLoader 平台类加载器
  9. System.out.println(c1.getParent());
  10. //null 内置类加载器Bootstrap,通常表示为null
  11. }
  12. }

反射

image.png

Student

  1. public class Student {
  2. private String name;
  3. int age;
  4. public String address;
  5. public Student() {
  6. }
  7. private Student(String name, String address) {
  8. this.name = name;
  9. this.address = address;
  10. }
  11. public Student(String name, int age, String address) {
  12. this.name = name;
  13. this.age = age;
  14. this.address = address;
  15. }
  16. private void method1() {
  17. System.out.println("method1");
  18. }
  19. public void method2() {
  20. System.out.println("method2");
  21. }
  22. protected void method3() {
  23. System.out.println("method3");
  24. }
  25. void method4() {
  26. System.out.println("method4");
  27. }
  28. public String method5(String ss) {
  29. return ss;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Student{" +
  34. "name='" + name + '\'' +
  35. ", age=" + age +
  36. ", address='" + address + '\'' +
  37. '}';
  38. }
  39. }

反射的三种方式

  1. /**
  2. * 反射的三种方式
  3. */
  4. public class ReflectDemo {
  5. public static void main(String[] args) throws ClassNotFoundException {
  6. /**
  7. * 第一种:最简洁,最方便 .Class
  8. */
  9. Class<Student> sc = Student.class;
  10. System.out.println(sc);
  11. //>>> class org.itheima_01.Student
  12. Class<Student> sc1 = Student.class;
  13. System.out.println(sc1);
  14. System.out.println(sc==sc1);
  15. /**
  16. * 第二种:通过类方法的 getClass()
  17. */
  18. Student s = new Student();
  19. Class<? extends Student> sc2 = s.getClass();
  20. System.out.println(sc2);
  21. /**
  22. * 第三种:可配置,最灵活 Class.forName 需抛出ClassNotFoundException异常
  23. */
  24. Class<?> sc3 = Class.forName("org.itheima_01.Student");
  25. System.out.println(sc3);
  26. }
  27. }

通过反射 获取类构造方法,创建对象

  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.InvocationTargetException;
  3. public class ReflectDemo01 {
  4. public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  5. //获取所有的 公共构造方法 getConstructors
  6. Class<?> sc = Class.forName("org.itheima_01.Student");
  7. Constructor<?>[] constructors = sc.getConstructors();
  8. for (Constructor c:constructors){
  9. //System.out.println(c);
  10. }
  11. /*
  12. >>> public org.itheima_01.Student(java.lang.String,int,java.lang.String)
  13. public org.itheima_01.Student()
  14. */
  15. //获取所有的构造方法 getDeclaredConstructors
  16. Constructor<?>[] dc = sc.getDeclaredConstructors();
  17. for (Constructor c:dc){
  18. //System.out.println(c);
  19. }
  20. /**
  21. * 通过反射创建对象
  22. */
  23. //公共无参
  24. Constructor<?> con = sc.getConstructor(); //不写参数就是默认的无参构造方法
  25. Object o = con.newInstance();
  26. System.out.println(o);
  27. //私有带参 编译时异常-报错:java.lang.NoSuchMethodException
  28. /*Constructor<?> con1 = sc.getConstructor(String.class, String.class);
  29. Object o1 = con1.newInstance("秦苒", "武汉");
  30. System.out.println(o1);*/
  31. //公共带参
  32. Constructor<?> con2 = sc.getConstructor(String.class,int.class, String.class);
  33. Object o2 = con2.newInstance("秦苒",18, "武汉");
  34. System.out.println(o2);
  35. /**
  36. * 通过new关键字创建对象
  37. */
  38. //公共无参
  39. Student s = new Student();
  40. System.out.println(s);
  41. //私有带参 运行时异常-报错:'Student(java.lang.String, java.lang.String)' has private access in 'org.itheima_01.Student'
  42. /*Student s1 = new Student("林风","舟山");
  43. System.out.println(s1);*/
  44. //公共带参
  45. Student s2 = new Student("林风",25,"舟山");
  46. System.out.println(s2);
  47. }
  48. }

暴力反射

  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.InvocationTargetException;
  3. public class ReflectDemo02 {
  4. public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException {
  5. Class<?> c = Class.forName("org.itheima_01.Student");
  6. //无法使用私有构造方法 私有报错:IllegalAccessException
  7. Constructor<?> dc = c.getDeclaredConstructor(String.class, int.class, String.class);
  8. Object o = dc.newInstance("陈尽", 21, "长春");
  9. System.out.println(o);
  10. //>>>Student{name='陈尽', age=21, address='长春'}
  11. /**
  12. * 暴力反射 setAccessible - 可访问私有构造方法
  13. */
  14. Constructor<?> con = c.getDeclaredConstructor(String.class, String.class);
  15. con.setAccessible(true); //取消访问检查
  16. Object o1 = con.newInstance("青岚", "成都");
  17. System.out.println(o1);
  18. //>>> Student{name='青岚', age=0, address='成都'}
  19. }
  20. }

反射获取成员变量

  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.InvocationTargetException;
  4. public class ReflectDemo03 {
  5. public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  6. Class<?> c = Class.forName("org.itheima_01.Student");
  7. /**
  8. * 获取所有成员变量
  9. */
  10. Field[] dfs = c.getDeclaredFields();
  11. for (Field f:dfs){
  12. System.out.println(f);
  13. }
  14. Constructor<?> con = c.getConstructor();
  15. Object o = con.newInstance();
  16. /**
  17. * 暴力私有变量访问 name
  18. */
  19. //方式一通用
  20. Field name = c.getDeclaredField("name");
  21. name.setAccessible(true);
  22. Field address = c.getField("address");
  23. Field age = c.getDeclaredField("age");
  24. name.set(o,"张白亮");
  25. age.set(o,18);
  26. address.set(o,"长寿");
  27. System.out.println(o);
  28. //>>> Student{name='张白亮', age=18, address='长寿'}
  29. }
  30. }

反射获取成员方法

  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. public class ReflectDemo04 {
  5. public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  6. Class<?> c = Class.forName("org.itheima_01.Student");
  7. //所有-包括继承的方法
  8. Method[] methods = c.getMethods();
  9. for (Method m:methods){
  10. //System.out.println(m);
  11. }
  12. //获取本类的所有方法,只是本类的
  13. Method[] dms = c.getDeclaredMethods();
  14. for (Method m:dms){
  15. //System.out.println(m);
  16. }
  17. //创建对象
  18. Constructor<?> con = c.getConstructor();
  19. Object o = con.newInstance();
  20. /**
  21. * 调用方法
  22. */
  23. //private 修饰的需使用setAccessible暴力访问
  24. Method method1 = c.getDeclaredMethod("method1"); //NoSuchMethodException
  25. method1.setAccessible(true);
  26. method1.invoke(o);
  27. //只有public 修饰的才可通过getMethod 访问
  28. Method method2 = c.getMethod("method2"); //NoSuchMethodException
  29. method2.invoke(o);
  30. //protected 修饰
  31. Method method3 = c.getDeclaredMethod("method3"); //NoSuchMethodException
  32. method3.invoke(o);
  33. //默认修饰符
  34. Method method4 = c.getDeclaredMethod("method4"); //NoSuchMethodException
  35. method4.invoke(o);
  36. //带参带返回值 的方法
  37. Method method5 = c.getMethod("method5", String.class);
  38. System.out.println(method5.invoke(o,"带参带返回值"));
  39. String s = (String)method5.invoke(o, "我是参数");
  40. System.out.println(s);
  41. }
  42. }

反射越过泛型检查

  1. /**
  2. * 通过反射越过泛型检查
  3. */
  4. public class ReflectDemo05 {
  5. public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
  6. /**
  7. * 通过反射在ArrayList<Integer>中添加一个字符串
  8. */
  9. ArrayList<Integer> arr = new ArrayList<>();
  10. //arr.add("字符串");
  11. arr.add(666);
  12. Class<? extends ArrayList> aClass = arr.getClass();
  13. Method add = aClass.getMethod("add", Object.class);
  14. add.invoke(arr,"字符串");
  15. System.out.println(arr);
  16. //>>> [666, 字符串]
  17. }
  18. }

反射读取配置文件

  1. import java.io.FileReader;
  2. import java.io.IOException;
  3. import java.lang.reflect.Constructor;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.Properties;
  7. public class ReflectDemo06 {
  8. public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  9. Teacher t = new Teacher();
  10. t.study();
  11. /**
  12. * 通过反射读取配置文件调用函数,灵活性更高
  13. * 配置 类名 与 方法名
  14. * class.txt 文件内容如下:
  15. * ClassName = org.itheima_01.Teacher
  16. * MethodName = study
  17. */
  18. Properties properties = new Properties();
  19. //Ctrl+Shift+C 复制文件绝对路径
  20. FileReader fr = new FileReader("D:\\Graduation\\TestStudy\\src\\org\\itheima_01\\class.txt");
  21. properties.load(fr);
  22. fr.close();
  23. String className = properties.getProperty("ClassName");
  24. String methodName = properties.getProperty("MethodName");
  25. Class<?> c = Class.forName(className);
  26. Constructor<?> con = c.getConstructor();
  27. Object obj = con.newInstance();
  28. Method method = c.getMethod(methodName);
  29. method.invoke(obj);
  30. }
  31. }