反射试题

来源:互联网 发布:武汉人工智能公司 编辑:程序博客网 时间:2024/06/05 23:51

1.ArrayList list = new ArrayList();
这个泛型为Integer的ArrayList中存放一个String类型的对象

package com.itheima.tests;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;/* * 1.ArrayList<Integer> list = new ArrayList<Integer>();     为这个泛型为Integer的ArrayList中存放一个String类型的对象 */public class Test01 {    public static void main(String[] args) throws Exception {        ArrayList<Integer> list = new ArrayList<Integer>();         list.add(10);        System.out.println(list);        //通过反射获取ArrayList集合的字节码对象        Class clazz = Class.forName("java.util.ArrayList");        //通过反射获取add方法        Method addMethod = clazz.getMethod("add", Object.class);        //通过反射调用addMethod方法        addMethod.invoke(list, "reflect is very good!");        System.out.println(list);//      HashMap<Integer,Integer> hm = new HashMap<>();    }}

2.用反射去创建一个对象,有2种方式,尽量用代码去体现

package com.itheima.tests;import java.lang.reflect.Constructor;/** * 2.用反射去创建一个对象,有2种方式,尽量用代码去体现 * @author JX * */public class Test02 {    public static void main(String[] args) throws Exception {        //获取Student类的字节码对象        Class clazz = Class.forName("com.itheima.tests.Student");        //1.利用反射创建一个空的对象        Student student = (Student)clazz.newInstance();        /*//2.获取字段        Field ageField = clazz.getDeclaredField("age");        Field nameField = clazz.getDeclaredField("name");        //取出私有属性        ageField.setAccessible(true);        nameField.setAccessible(true);        //3.给字段设置值        ageField.set(student, 30);        nameField.set(student, "张三");        System.out.println(student);*/        /*Method setAgeMethod = clazz.getMethod("setAge", int.class);        Method setNameMethod = clazz.getMethod("setName", String.class);        setAgeMethod.invoke(student, 38);        setNameMethod.invoke(student, "柳岩");        System.out.println(student);*/        //获取有参构造        Constructor constructor = clazz.getConstructor(int.class,String.class);        Student stu = (Student)constructor.newInstance(30,"张亮");        System.out.println(stu);    }}class Student {    private int age;    private String name;    public Student() {    }    public Student(int age, String name) {        super();        this.age = age;        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Student [age=" + age + ", name=" + name + "]";    }   }
  1. 编写一个类,增加一个实例方法用于打印一条字符串。
    并使用反射手段创建该类的对象, 并调用该对象中的方法。

    package com.itheima.tests;
    import java.lang.reflect.Method;

    /*

      1. 编写一个类,增加一个实例方法用于打印一条字符串。
        并使用反射手段创建该类的对象, 并调用该对象中的方法。
        */
        public class Test03 {
        public static void main(String[] args) throws Exception {
        //获取Demo.java的字节码对象
        Class clazz = Class.forName(“com.itheima.tests.Demo”);//obj.getClass(); Student.class
        //利用反射创建对象
        Demo demo = (Demo)clazz.newInstance();
        //利用反射获取print方法
        Method printMethod = clazz.getMethod(“print”,String.class);
        printMethod.invoke(demo,”Android”);
        }
        }
        class Demo {
        public Demo() {
        }
        public void print(String str ){
        System.out.println(“Hello “+str);
        }
        }

4.编写一个类A,增加一个实例方法showString,用于打印一条字符串,
在编写一个类TestA ,作为客户端,用键盘输入一个字符串,该字符串就是类A的全名,使用反射机制创建该类的对象,
并调用该对象中的方法showString

package com.itheima.tests;import java.lang.reflect.Method;import java.util.Scanner;/* * 4.编写一个类A,增加一个实例方法showString,用于打印一条字符串,    在编写一个类TestA ,作为客户端,用键盘输入一个字符串,该字符串就是类A的全名,使用反射机制创建该类的对象,    并调用该对象中的方法showString */public class Test04 {    public static void main(String[] args) throws Exception{        Scanner sc = new Scanner(System.in);        System.out.println("请输入一个类的全类名,用.隔开:");        String className = sc.nextLine();        Class clazz = Class.forName(className);//obj.getClass(); Student.class        //利用反射创建对象        A a = (A)clazz.newInstance();        //利用反射获取print方法        Method printMethod = clazz.getMethod("showString");        printMethod.invoke(a);          }}class A {    public void showString() {        System.out.println(" e m t f 明天一定通过!");    }}
  1. 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value.
    public void setProperty(Object obj, String propertyName, Object value){
    }

    package com.itheima.tests;
    import java.lang.reflect.Field;

    /**

      1. 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value.
        public void setProperty(Object obj, String propertyName, Object value){

      }

    • @author JX
      *
      */
      public class Test05 {
      public static void main(String[] args) throws Exception {
      Student student = new Student(30,”张三”);
      setProperty(student, “age”, 25);
      System.out.println(student);

      Object obj = getProperty(student, "name");System.out.println(obj);

      }
      //给对象obj的名字为propertyName的属性设置为value
      public static void setProperty(Object obj, String propertyName, Object value) throws Exception{
      //1.获取obj的字节码对象
      Class clazz = obj.getClass();
      //2.获取propertyName属性对应的Field对象
      Field propertyNameField = clazz.getDeclaredField(propertyName);
      //3.设置成可访问的
      propertyNameField.setAccessible(true);
      //4.调用set方法给对象赋值
      propertyNameField.set(obj, value);

      }
      //给对象obj的名字为propertyName的属性设置为value
      public static Object getProperty(Object obj, String propertyName) throws Exception{
      //1.获取obj的字节码对象
      Class clazz = obj.getClass();
      //2.获取propertyName属性对应的Field对象
      Field propertyNameField = clazz.getDeclaredField(propertyName);
      //3.设置成可访问的
      propertyNameField.setAccessible(true);
      //4.调用get方法获取该对象对应属性的值
      return propertyNameField.get(obj);
      }
      }

6.定义一个标准的JavaBean,名叫Person,包含属性name、age。
使用反射的方式创建一个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,
不使用setAge方法直接使用反射方式对age赋值。

package com.itheima.tests;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;/* * 6.定义一个标准的JavaBean,名叫Person,包含属性name、age。    使用反射的方式创建一个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,    不使用setAge方法直接使用反射方式对age赋值。 */public class Test06 {    public static void main(String[] args) throws Exception, SecurityException {        //1.获取Person类的字节码对象        Class clazz = Person.class;        //2.利用反射获取有参构造方法        Constructor constructor  = clazz.getConstructor(int.class,String.class);        //3.调用构造方法,给属性初始化        Person person =  (Person)constructor.newInstance(30,"灭绝师太");        System.out.println(person);        //4.使用反射方式调用setName方法对名称进行设置        Method setNameMethod = clazz.getMethod("setName", String.class);        setNameMethod.invoke(person, "张三丰");        //5.不使用setAge方法直接使用反射方式对age赋值。        Field ageField = clazz.getDeclaredField("age");        ageField.setAccessible(true);        ageField.set(person, 50);        System.out.println(person);    }}class Person {    private int age;    private String name;    public Person() {    }    public Person(int age, String name) {        super();        this.age = age;        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "Person [age=" + age + ", name=" + name + "]";    }   }

7.已知一个类,定义如下:
package com.itheima;
public class DemoClass {
public void run() {
System.out.println(“welcome to heima!”);
}
}
(1)写一个Properties格式的配置文件,配置类的完整名称。
(2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,
(3)用反射 的方式运行run方法。

package com.itheima.tests;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileReader;import java.lang.reflect.Method;import java.util.Properties;/** * 7.已知一个类,定义如下:     package com.itheima.tests;     public class DemoClass {         public void run() {             System.out.println("welcome to heima!");         }     }     (1)写一个Properties格式的配置文件,配置类的完整名称。    (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,    (3)用反射 的方式运行run方法。 * @author JX * */public class Test07 {    public static void main(String[] args) throws Exception {        /*Properties props = new Properties();        props.load(new FileInputStream("src/config.properties"));        String className = (String) props.get("className");*/        //定义字符缓冲输入流        BufferedReader br = new BufferedReader(new FileReader("src/config.properties"));        String line = br.readLine();        String className = line.split("=")[1];//      System.out.println(className);        Class clazz = Class.forName(className);        //利用反射创建一个对象        Object obj = clazz.newInstance();        //利用反射获取run方法        Method runMethod = clazz.getMethod("run");        //利用反射调用run方法        runMethod.invoke(obj);          }}
  1. 写一个方法,此方法可以获取obj对象中名为propertyName的属性的值
    public static Object getProperty(Object obj, String propertyName, Object value){

    }

    package com.itheima.tests2;
    import java.lang.reflect.Field;
    public class Test08 {
    public static void main(String[] args) throws Exception {
    Person person = new Person(20,”张三”);
    String name = (String)getProperty(person, “name”);
    System.out.println(name);

    }public static Object getProperty(Object obj, String propertyName) throws Exception{     //获取obj对象的字节码文件对象    Class clazz = obj.getClass();    //获取propertyName属性所对应的字段    Field field = clazz.getDeclaredField(propertyName);    //去掉私有属性    field.setAccessible(true);    return field.get(obj);}

    }

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩流清鼻涕怎么办最简单方法 孕3个月胎盘低置怎么办 孩子判逆不听家长的话该怎么办 香港购物超5000被海关扣怎么办 浅色衣服被深色衣服染色了怎么办 金立手机微信不能发语音怎么办 吃鸡买的账号密码邮箱忘记了怎么办 氩弧焊枪管带里进水了怎么办 绝地求生穿头盔的时候连衣帽怎么办 开车不小心把光缆线给挂断了怎么办 脚刺到了生锈钢钉没打针怎么办 一加3t背壳螺丝掉了怎么办 30万美金美金中国被扣怎么办 电脑使用迅雷变的很卡怎么办 优盘拷贝过程中失去优盘路径怎么办 用百度云上传视频文件太慢了怎么办 网易云音乐云盘电脑上传很慢怎么办 路由器的宽带账号密码忘记了怎么办 蚂蚁邦路由器管理密码忘记了怎么办 红米2a刷机失败怎么办 小米手机开机图案锁忘记了怎么办 小米6进水无限闪屏开机重启怎么办 红米手机一直卡在开机画面怎么办 红米4卡在开机画面怎么办 红米手机一直在开机画面怎么办 红米手机一直跳开机画面怎么办 红米note3锁屏密码忘记怎么办 红米手机忘记锁屏密码怎么办 红米4锁屏密码忘了怎么办 红米note忘记锁屏密码怎么办 红米note2锁屏密码忘了怎么办 机打发票抬头名字少写一个字怎么办 卷式发票名字写错了怎么办 发票丢失了销售方不给补手续怎么办 总是把单词归不成句孑怎么办 白色踏板摩托车把漆刮了怎么办 苹果手机用流量缓冲很难怎么办 谷歌浏览器安卓手机版打不开怎么办 怀孕四个月检查高型半氨酸高怎么办 猎豹cs9怎么打不开车门怎么办 孩子在学校被老师冤枉打板子怎么办