java反射 练习题

为了回忆起来,于是重新找了几道练习题做了下

package cn.incast.homework30;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

public class Reflect {
    public static void main(String[] args) throws Exception {
        // fanxingDemo();
        // newObject1();
        // newObject2();
        // printStr();
        // showObj();
        // setProperty(new Person(), "name", "张三");
        // beanDemo();
        // run();
//      System.out.println(getProperty(new Person(), "age"));
        
    }

    // 8. 写一个方法,此方法可以获取obj对象中名为propertyName的属性的值
    public static Object getProperty(Object obj, String propertyName)throws Exception {
            Class c =obj.getClass();
            Field f =c.getDeclaredField(propertyName);
            f.setAccessible(true);
            Object fValue = f.get(obj);
//          System.out.println(fValue);
            return fValue;
    }

    // 7.(1)写一个Properties格式的配置文件,配置类的完整名称。
    // (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,
    // (3)用反射 的方式运行run方法。
    private static void run() throws Exception {
        Properties pro = new Properties();
        // 注意:如果是用Eclise的话,切记要右键工程名字,新建一个file文件,因为如果file文件不在工程根目录的话,它是找不到的。
        FileReader fr = new FileReader("config.properties");
        pro.load(fr);
        fr.close();

        String className = pro.getProperty("className");
        String MethodName = pro.getProperty("MethodName");
        Class c = Class.forName(className);
        Object obj = c.newInstance();
        Method method = c.getMethod(MethodName);
        method.invoke(obj);

    }

    // 6.定义一个标准的JavaBean,名叫Person,包含属性name、age。
    // 使用反射的方式创建一个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,
    // 不使用setAge方法直接使用反射方式对age赋值。
    private static void beanDemo() throws Exception {
        Class c = Class.forName("cn.incast.homework30.Person");
        Constructor con = c.getConstructor(String.class, int.class);
        Object obj = con.newInstance("小明", 16);
        c.getMethod("setName", String.class).invoke(obj, "小丽");// 反射方式调用setName方法对名称进行设置
        Field f = c.getDeclaredField("name");
        f.setAccessible(true);
        System.out.println(obj);

    }

    // 5. 写一个方法,此方法可将obj对象中名为propertyName的属性的值设置为value.
    public static void setProperty(Object obj, String propertyName, Object value)
            throws Exception {
        Class c = obj.getClass();
        Field f = c.getDeclaredField(propertyName);// 获得包括私有和公有的属性
        f.setAccessible(true);// 还要配合这个,取消权限检查,才能设置属性
        f.set(obj, value);

        System.out.println(obj);
    }

    // 4.编写一个类A,增加一个实例方法showString,用于打印一条字符串,在编写一个类TestA
    // ,作为客户端,用键盘输入一个字符串,该字符串就是类A的全名,
    // 使用反射机制创建该类的对象,并调用该对象中的方法showString
    private static void showObj() throws Exception {
        Scanner s = new Scanner(System.in);
        System.out.println("输出一个类的全名");
        Class c = Class.forName(s.nextLine());
        Object obj = c.newInstance();
        Method method = c.getMethod("showString");
        method.invoke(obj);
    }

    // 3. 编写一个类,增加一个实例方法用于打印一条字符串。并使用反射手段创建该类的对象, 并调用该对象中的方法。
    private static void printStr() throws Exception {
        Class c = Class.forName("cn.incast.homework30.Person");
        // Person p = new Person();
        Object p = c.newInstance();

        Method method = c.getMethod("print");

        method.invoke(p);
    }

    // 2. 用反射去创建一个对象,有2种方式,尽量用代码去体现
    private static void newObject1() throws Exception {
        Class c = Class.forName("cn.incast.homework30.Person");
        Constructor cons = c.getConstructor(String.class, int.class);
        Object obj = cons.newInstance("小明", 20);
        System.out.println(obj);
    }

    private static void newObject2() throws Exception {
        Class c = Class.forName("cn.incast.homework30.Person");
        Object obj = c.newInstance();
        System.out.println(obj);
    }

    // 1.泛型类型擦除
    private static void fanxingDemo() throws Exception {
        ArrayList<String> array = new ArrayList<String>();
        array.add("begin");
        Class c = Class.forName("java.util.ArrayList");
        Method method = c.getMethod("add", Object.class);
        method.invoke(array, 20);

        for (Object obj : array) {
            System.out.println(obj);
        }
    }

}

转载于:https://www.cnblogs.com/czy16/p/8365452.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值