• Java实验十三


    第一题

    定义一个 Person 类(实现了接口 China,Person 类定义了一个参数的构造方法,两个参数的构造方法以及 setter 和 getter 方法),通过反射机制取得类的结构。
    (1)取得 Person 类所实现的全部接口;
    (2)取得父类;
    (3)取得全部构造方法,并使用 Modifier 还原修饰符;
    (4)取得全部方法,并使用 Modifier 还原修饰符;
    (5)取得全部属性,并使用 Modifier 还原修饰符。

    参考:https://blog.csdn.net/yuan465887375/article/details/8806045
    《Java开发实战经典》

    接口及Person类

    interface China {    // 定义China接口
        public static final String NATIONAL = "China";
        public static final String AUTHOR = "Star";
    
        public void sayChina();
    
        public String sayHello(String name, int age);
    }
    
    public class Person implements China {
        private String name;
        private int age;
    
        public Person() {
        }
    
        public Person(String name) {
            this.name = name;
        }
    
        public Person(String name, int age) {
            this(name);
            this.age = age;
        }
    
        public void sayChina() {
            System.out.println("作者:" + AUTHOR + ",国籍:" + NATIONAL);
        }
    
        public String sayHello(String name, int age) {
            return name + ",你好!我今年:" + age + "岁了!";
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getName() {
            return this.name;
        }
    
        public int getAge() {
            return this.age;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    取得 Person 类所实现的全部接口

    public class GetInterfaceDemo {
        public static void main(String args[]) {
            Class<?> c1 = null;
            try {
                c1 = Class.forName("Person");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Class<?> c[] = c1.getInterfaces();    // 以数组形式返回实现的全部接口
            for (int i = 0; i < c.length; i++) {
                System.out.println("实现的接口名称:" + c[i].getName());    // 输出接口名称
            }
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    取得父类

    public class GetSuperClassDemo {
        public static void main(String args[]) {
            Class<?> c1 = null;  // 声明Class对象
            try {
                c1 = Class.forName("Person"); // 实例化对象
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Class<?> c2 = c1.getSuperclass(); // 取得父类
            System.out.println("父类名称:" + c2.getName());
        }
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    取得全部构造方法,并使用 Modifier 还原修饰符

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Modifier;
    
    public class GetConstructorDemo03 {
        public static void main(String args[]) {
            Class<?> c1 = null;        // 声明Class对象
            try {
                c1 = Class.forName("Person");    // 实例化对象
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Constructor<?> con[] = c1.getConstructors();    // 取得一个类中的全部构造
            for (int i = 0; i < con.length; i++) {
                Class<?> p[] = con[i].getParameterTypes();        // 得到构造方法中的全部参数
                System.out.print("构造方法:");     // 输出构造,直接打印
                int mo = con[i].getModifiers(); // 得到所要的访问权限
                System.out.print(Modifier.toString(mo) + " ");    // 得到修饰符
                System.out.print(con[i].getName());    // 取得构造方法的名字
                System.out.print("(");
                for (int j = 0; j < p.length; j++) {
                    System.out.print(p[j].getName() + " arg" + i);
                    if (j < p.length - 1) {
                        // 判断此是否是最后一个参数
                        System.out.print(",");    // 输出“,”
                    }
                }
                System.out.println("){}");
            }
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    取得全部方法,并使用 Modifier 还原修饰符

    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    
    public class GetMethodDemo {
        public static void main(String args[]) {
            Class<?> c1 = null;        // 声明Class对象
            try {
                c1 = Class.forName("Person");    // 实例化对象
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Method m[] = c1.getMethods();    // 取得全部方法
            for (int i = 0; i < m.length; i++) {
                Class<?> r = m[i].getReturnType();    // 得到返回值类型
                Class<?> p[] = m[i].getParameterTypes();    // 取得全部参数的类型
                int xx = m[i].getModifiers();    // 得到修饰符
                System.out.print(Modifier.toString(xx) + " ");    // 输出修饰符
                System.out.print(r + " ");
                System.out.print(m[i].getName());
                System.out.print("(");
                for (int j = 0; j < p.length; j++) {
                    System.out.print(p[j].getName() + " " + "arg" + j);
                    if (j < p.length - 1) {
                        System.out.print(",");
                    }
                }
                Class<?> ex[] = m[i].getExceptionTypes();    // 取出异常
                if (ex.length > 0) {
                    System.out.print(") throws ");
                } else {
                    System.out.print(")");
                }
                for (int j = 0; j < ex.length; j++) {
                    System.out.print(ex[j].getName());
                    if (j < p.length - 1) {
                        System.out.print(",");
                    }
                }
                System.out.println();
            }
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在这里插入图片描述

    取得全部属性,并使用 Modifier 还原修饰符

    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;
    
    public class GetFieldDemo {
        public static void main(String args[]) {
            Class<?> c1 = null;  // 声明Class对象
            try {
                c1 = Class.forName("Person"); // 实例化对象
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            { // 本类属性
                Field f[] = c1.getDeclaredFields(); // 取得本类中的属性
                for (int i = 0; i < f.length; i++) {
                    Class<?> r = f[i].getType(); // 得到属性类型
                    int mo = f[i].getModifiers(); // 得到修饰符的数字
                    String priv = Modifier.toString(mo); // 还原修饰符
                    System.out.print("本类属性:");
                    System.out.print(priv + " ");
                    System.out.print(r.getName() + " "); // 得到属性类型
                    System.out.print(f[i].getName()); // 输出属性名称
                    System.out.println(" ;");
                }
            }
            { // 公共属性
                Field f[] = c1.getFields(); // 取得本类中的公共属性
                for (int i = 0; i < f.length; i++) {
                    Class<?> r = f[i].getType(); // 得到属性类型
                    int mo = f[i].getModifiers(); // 得到修饰符的数字
                    String priv = Modifier.toString(mo); // 还原修饰符
                    System.out.print("公共属性:");
                    System.out.print(priv + " ");
                    System.out.print(r.getName() + " "); // 得到属性类型
                    System.out.print(f[i].getName()); // 输出属性名称
                    System.out.println(" ;");
                }
            }
        }
    };
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    在这里插入图片描述

    第二题

    定义一个学生类,其中包括姓名、年龄、成绩的属性,之后通过键盘输入学生的内容,并将内容保存在文件中,所有的操作要求全部使用反射机制完成,即不能使用通过关键字 new 创建学生类对象的操作。

    Student类

    public class Student {
        private String name;
        private int age;
        private double score;
    
        public Student(String name, int age, double score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }
    
        public String toString() {
            return "name:" + name + " age:" + age + " score:" + score;
        }
    
        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 double getScore() {
            return score;
        }
    
        public void setScore(double score) {
            this.score = score;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    测试类:

    import java.io.PrintStream;
    import java.lang.reflect.Constructor;
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Scanner sc = new Scanner(System.in);
            PrintStream file = new PrintStream("Student.txt");  //创建文件
            System.out.println("输入学生姓名:");
            String name = sc.nextLine();
            System.out.println("输入学生年龄:");
            int age = sc.nextInt();
            System.out.println("输入学生成绩:");
            double score = sc.nextDouble();
            Class<?> c = null;
            try {
                c = Class.forName("Student");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Student per = null;
            Constructor<?> cons[] = null;
            cons = c.getConstructors();
            try {
                per = (Student) cons[0].newInstance(name, age, score);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(per);
            file.println(per.toString());  //写入文件
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述
    student.txt文件内容:
    在这里插入图片描述

  • 相关阅读:
    matlab simulink响应谱计算
    c++11新特性--variadic templates(详细)
    点云从入门到精通技术详解100篇-基于 LSTM 的点云降采样(下)
    OpenCV-Python学习(3)—— OpenCV 图像色彩空间转换
    部署基于docker和cri-dockerd的Kubernetes v1.25.3
    论文阅读:“基于快速特征点提取和描述算法与色调、饱和度和明度的图像特征点匹配算法”
    『牛客|每日一题』模板队列
    I.MX6U ALPHA裸机开发
    医院管理系统数据库,课程设计,SQLserver,纯代码设计
    DML(插入 更新 删除),附代码理解
  • 原文地址:https://blog.csdn.net/fjdep/article/details/124948350