guyanqing 10 months ago
parent
commit
9cf0f73ddf

BIN
employee.dat


+ 0 - 1
fox.txt

@@ -1 +0,0 @@
-zxcvbbn

+ 0 - 1
fox_bak.txt

@@ -1 +0,0 @@
-zxcvbbn

+ 0 - 2
jdbc.properties

@@ -1,2 +0,0 @@
-user_name = admin;
-pwd = 123456;

+ 0 - 3
out.txt

@@ -1,3 +0,0 @@
-a:1
-f:2
-h:1

+ 0 - 3
outbak.txt

@@ -1,3 +0,0 @@
-a:1
-f:2
-h:1

+ 38 - 0
src/main/java/com/sf/javase/day19/Person.java

@@ -0,0 +1,38 @@
+package com.sf.javase.day19;
+
+public class Person {
+    private int id;
+    public char  gender;
+
+    public Person() {
+    }
+
+    public Person(int id, char gender) {
+        this.id = id;
+        this.gender = gender;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public char getGender() {
+        return gender;
+    }
+
+    public void setGender(char gender) {
+        this.gender = gender;
+    }
+
+    @Override
+    public String toString() {
+        return "Person{" +
+                "id=" + id +
+                ", gender=" + gender +
+                '}';
+    }
+}

+ 38 - 0
src/main/java/com/sf/javase/day19/Student.java

@@ -0,0 +1,38 @@
+package com.sf.javase.day19;
+
+public class Student  extends Person{
+    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;
+    }
+
+    @Override
+    public String toString() {
+        return "Student{" +
+                "name='" + name + '\'' +
+                ", age=" + age +
+                '}';
+    }
+}

+ 245 - 0
src/main/java/com/sf/javase/day19/T.java

@@ -0,0 +1,245 @@
+package com.sf.javase.day19;
+
+
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.ElementType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * 反射  class
+ */
+public class T {
+    /**
+     * 获取class对象
+     */
+    @Test
+    public void t1() throws ClassNotFoundException {
+        // 一
+        Class<String> stringClass = String.class;
+        System.out.println(stringClass);
+       //二
+        Student student = new Student();
+        Class<? extends Student> aClass = student.getClass();
+        //三
+        Class<?> aClass1 = Class.forName("com.sf.javase.day19.Student");
+
+        //四   通过类的加载器
+        ClassLoader classLoader = this.getClass().getClassLoader();
+        Class<?> aClass2 = classLoader.loadClass("com.sf.javase.day19.Student");
+    }
+
+    @Test
+    public void t2() throws ClassNotFoundException {
+        Class c1 = String.class;
+        String obj = new String();
+        Class c2 = obj.getClass();
+        Class c3 = Class.forName("java.lang.String");
+        Class c4 = ClassLoader.getSystemClassLoader().loadClass("java.lang.String");
+        System.out.println("c1 = " + c1);
+        System.out.println("c2 = " + c2);
+        System.out.println("c3 = " + c3);
+        System.out.println("c4 = " + c4);
+        System.out.println(c1 == c2); //true
+        System.out.println(c1 == c3); //true
+    }
+
+    /**
+     * java 数据类型 的class类
+     */
+    @Test
+    public void t3(){
+        Class<Object> objectClass = Object.class;
+        Class<Comparable> comparableClass = Comparable.class;
+        Class<int[]> aClass = int[].class;
+        Class<int[][]> aClass1 = int[][].class;
+        Class<Integer> integerClass = int.class;
+        Class<ElementType> elementTypeClass = ElementType.class;
+        Class<Override> overrideClass = Override.class;
+        Class<Void> voidClass = void.class;
+        Class<Class> classClass = Class.class;
+        int[] ints = new int[10];
+        int[] ints1 = new int[100];
+        int[][] ints2 = new int[2][3];
+        System.out.println(ints.getClass()  == ints1.getClass());
+        System.out.println(ints.getClass().equals(ints2.getClass()));
+    }
+
+    /**
+     * java 数据类型 的class类 的常用方法
+     */
+    @Test
+    public void t4() throws InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException {
+        Student student = new Student();
+        Class<? extends Student> aClass = student.getClass();
+        System.out.println("获取student对象的class对象="+student.getClass());
+        Student student1 = aClass.newInstance();
+        System.out.println("student1 = "+student1);
+        String name = aClass.getName();
+        System.out.println("name = "+name);
+        Class<?> superclass = aClass.getSuperclass();
+        System.out.println("superclass = "+superclass);
+        Class<?>[] interfaces = aClass.getInterfaces();
+        System.out.println("aClass.getInterfaces() = "+ Arrays.toString(interfaces));
+        Constructor<? extends Student> constructor = aClass.getConstructor();
+        System.out.println("aClass.getConstructor()  = "+constructor);
+        Field[] declaredField = aClass.getDeclaredFields();
+        System.out.println("aClass.getDeclaredFields() = "+Arrays.toString( declaredField));
+        Field[] fields = aClass.getFields();   //  获取公共的属性(包含父类) -
+        System.out.println("fileds = "+Arrays.toString(fields));
+        Field age = aClass.getDeclaredField("age");
+        System.out.println("age ="+age);
+        Method[] methods = aClass.getMethods();
+        System.out.println("aClass.getMethods() = "+Arrays.toString(methods));
+        Method method = aClass.getMethod("setName",String.class);
+        System.out.println("method = "+method);
+    }
+
+    /**
+     * 获取类的加载器的方式
+     */
+    @Test
+    public void t5() throws ClassNotFoundException {
+        //  获取系统类的加载器
+        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
+        //  获取某个类的类的加载
+        ClassLoader classLoader = Class.forName("java.lang.String").getClassLoader();
+        //获取String加载器的父加载器
+        ClassLoader parent = classLoader.getParent();
+    }
+
+    /**
+     * 使用ClassLoader获取流
+     */
+    @Test
+    public void t6() throws ClassNotFoundException, IOException {
+//        InputStream resourceAsStream = Class.forName("java.lang.String").getClassLoader().getResourceAsStream("jdbc.properties");
+//        //框架的使用    获取流
+        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
+        Properties properties = new Properties();
+        properties.load(resourceAsStream);
+        String user_name = properties.getProperty("user_name");
+        String pwd = properties.getProperty("pwd");
+        System.out.println(user_name);
+        System.out.println(pwd);
+    }
+
+    @Test
+    public void t7() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+        //  通过newInstance()方法获取当前类的对象
+        Object o = Class.forName("com.sf.javase.day19.Student").newInstance();
+        if(o instanceof Student){
+            Student student = (Student) o;
+            System.out.println(student);
+        }
+    }
+
+    @Test
+    public void t8() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
+        Class<?> aClass = Class.forName("com.sf.javase.day19.Student");
+        //  通过构造器方式进行创建该类的对象
+        Student  student = (Student) aClass.getDeclaredConstructor().newInstance();
+        Student student1 = (Student) aClass.getDeclaredConstructor(String.class, int.class).newInstance("admin",12);
+        System.out.println(student1);
+    }
+
+    @Test
+    public void t9(){
+        Class<Student> clazz = Student.class;
+        //获取当前运行时类中声明的所有属性
+        //  clazz.getDeclaredFields()  获取当前类的所有声明的属性
+        for (Field declaredField : clazz.getDeclaredFields()) {
+//            System.out.println(declaredField);
+        }
+
+        //     clazz.getFields()  获取当前类(包含父类)中所有公共的属性
+//        System.out.println(Arrays.toString(clazz.getFields()));
+//        获取当前类所有公共的方法(包含父类)
+//        System.out.println(Arrays.toString(clazz.getMethods()));
+        //   获取当前类的所有声明的方法
+        System.out.println(Arrays.toString(clazz.getDeclaredMethods()));
+
+        // 方法自己练习~~~~
+    }
+
+    /**
+     * 操作class类内的属性   filed
+     */
+    @Test
+    public void t10() throws ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
+        Class<?> clazz = Class.forName("com.sf.javase.day19.Student");
+        Field name = clazz.getDeclaredField("name");
+        name.setAccessible(true); //取消java语言访问检查
+        // 获取student的实例
+        Student stu = (Student)clazz.newInstance();
+        name.set(stu,"admin");
+        Object o = name.get(stu);
+        System.out.println(o);
+        System.out.println(stu);
+        // 静态属性  设置值
+        //   name.set(null,"admin");
+    }
+
+    /**
+     * 调用指定的方法
+     */
+    @Test
+    public void t11() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
+        Class<?> clazz = Class.forName("com.sf.javase.day19.Student");
+        //  获取指定的方法
+        Method method = clazz.getDeclaredMethod("setName", String.class);
+        Method getName = clazz.getDeclaredMethod("getName");
+        //  获取实例
+        Object obj = clazz.newInstance();
+        method.setAccessible(true);
+        method.invoke(obj,"admin");
+        Object invoke = getName.invoke(obj);
+        System.out.println(invoke);
+        System.out.println((Student)obj);
+    }
+
+    @Test
+    public void t12() throws IOException, ClassNotFoundException, NoSuchFieldException, InstantiationException, IllegalAccessException {
+        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("user.properties");
+        Properties properties = new Properties();
+        properties.load(resourceAsStream);
+        String user_name = properties.getProperty("user_name");
+        String user_id = properties.getProperty("user_id");
+        Class<?> clazz = Class.forName("com.sf.javase.day19.User");
+        Field name = clazz.getDeclaredField("name");
+        Object obj = clazz.newInstance();
+        name.setAccessible(true);
+        name.set(obj,user_name);
+        Field id = clazz.getDeclaredField("id");
+        id.setAccessible(true);
+        id.set(obj,Integer.valueOf(user_id));
+        System.out.println(obj);
+    }
+
+
+    /**
+     * 现在有一个List<String>   像当前集合中添加整型数据   [abc,bcs,123]
+     */
+    @Test
+    public void t13() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
+        List<String> list = new ArrayList<>();
+        Class<? extends List> listClass = list.getClass();
+        Method addMethod = listClass.getMethod("add", Object.class);
+        addMethod.invoke(list,"abc");
+        addMethod.invoke(list,"bcd");
+        addMethod.invoke(list,123);
+        addMethod.invoke(list,"123");
+        System.out.println(list);
+
+
+    }
+}

+ 38 - 0
src/main/java/com/sf/javase/day19/User.java

@@ -0,0 +1,38 @@
+package com.sf.javase.day19;
+
+public class User {
+    private Integer id;
+    private String name;
+
+    public User() {
+    }
+
+    public User(Integer id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                '}';
+    }
+}

+ 2 - 0
src/main/resources/jdbc.properties

@@ -0,0 +1,2 @@
+user_name = admin
+pwd = 123

+ 2 - 0
src/main/resources/user.properties

@@ -0,0 +1,2 @@
+user_name = zs
+user_id = 123

BIN
target/classes/com/sf/javase/day19/Person.class


BIN
target/classes/com/sf/javase/day19/Student.class


BIN
target/classes/com/sf/javase/day19/T.class


BIN
target/classes/com/sf/javase/day19/User.class


+ 2 - 0
target/classes/jdbc.properties

@@ -0,0 +1,2 @@
+user_name = admin
+pwd = 123

+ 2 - 0
target/classes/user.properties

@@ -0,0 +1,2 @@
+user_name = zs
+user_id = 123