Java反射——对类内部函数、私有变量和构造函数的使用举例

1 篇文章 0 订阅

对于java反射机制的描述可参考其他网站的解释,这里主要是对其中的对内部函数、私有变量和构造函数的用法举例

package main;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by bigzhou on 2016/10/23 0023.
 */
public class Person {
    public static void main(String[]arg) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, ClassNotFoundException {
        System.out.println("\n***************练习内部函数调用*****************");
        System.out.println("有一个参数的:");
        Class class11 = Person.class;
        Method method11 = class11.getDeclaredMethod("showOutName", String.class);
        method11.invoke(class11.newInstance(),"chenzhen");

        System.out.println("有多个参数的:");
        Class class12 = Person.class;
        Method method12 = class12.getDeclaredMethod("showOut",new Class[]{String.class,int.class});
        method12.invoke(class12.newInstance(),"chenzhen",23);

        System.out.println("静态内部函数:");
        Class class13 = Person.class;
        Method method13 = class13.getDeclaredMethod("staticFunc",String.class);
        method13.invoke(class13,"statictest");  //这里注意下调用的不是类实例而是类对象

        System.out.println("**************练习私有变量调用*****************");
        Class class21 = Person.class;
        Person person21 = (Person) class21.newInstance();
        Field field21 = class21.getDeclaredField("name");
        field21.setAccessible(true);        //这个是专门处理私有类型的,需要设置为true
        field21.set(person21,"huoyuanjia");
        person21.showName();

        System.out.println("**************练习私有构造函数***************");

        System.out.println("无参数的:");
        Constructor con31 = Person.class.getDeclaredConstructor();
        con31.setAccessible(true);
        Person f31 = (Person)con31.newInstance();
        f31.showAll();

        System.out.println("有多个参数的:");
        Constructor con32 = Person.class.getDeclaredConstructor(new Class[]{int.class,String.class});
        con32.setAccessible(true);
        Person f2 = (Person) con32.newInstance(new Object[]{23,"zhou"});
        f2.showAll();

        System.out.println("有一个参数的:");
        Constructor con3 = Person.class.getDeclaredConstructor(int.class);
        con3.setAccessible(true);
        Person f3 = (Person) con3.newInstance(23);
        f3.showAll();
    }
    private int age = 0;
    private String name = "asdf";
    Person(){}
    Person(int age, String name){
        this.age = age;
        this.name = name;
    }
    Person(int age){
        this.age = age;
    }
    void showAll(){
        System.out.println("name :"+name+"\naget :"+age+"\n");
    }
    public void showAge(){
        System.out.println("age :"+age);
    }
    public void showName(){
        System.out.println("name :"+name+"\n");
    }
    public void showOutName(String outname){
        System.out.println("name :"+outname+"\n");
    }
    public void showOut(String outname, int outage){
        System.out.println("name :"+outname+"\nage :"+outage+"\n");
    }
    public static void staticFunc(String str){
        System.out.println("static function: "+str+"\n");
    }
}

运行结果:

***************练习内部函数调用*****************
有一个参数的:
name :chenzhen

有多个参数的:
name :chenzhen
age :23

静态内部函数:
static function: statictest

**************练习私有变量调用*****************
name :huoyuanjia

**************练习私有构造函数***************
无参数的:
name :asdf
aget :0

有多个参数的:
name :zhou
aget :23

有一个参数的:
name :asdf
aget :23


Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值