Java反射机制对方法的基本操作

java反射机制的基本操作(在不创建对象的情况下,对该类所有方法的操作)

----------对公有无参书方法的操作-----------
TYT类

public class TYT {
    public void uu(){
        System.out.println("在不创建对象的情况下,对公有方法的操作");
    }
}

test类
下面的注意重点为------getMethod(“方法名”,“参数类型(该方法的参数类型有几种写几种,没有就写null)”),而invoke(调用)就是调用Method类代表的方法。可以实现动态调用,例如可以动态的传人参数,可以把方法参数化。
invoke(Class的对象名.newInstance(),所要传入的参数(没有就为null))

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

public class test {
    public static void main(String[] args)  {

        Class c=TYT.class;
        try {
            Method method=c.getMethod("uu",null);
            method.invoke(c.newInstance(),null);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}

----------对私有无参数方法的操作-----------
TYT类

public class TYT {
    private void su(){
        System.out.println("在不创建对象的情况下,对私有方法的操作");
    }
}

test类

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

public class test {
    public static void main(String[] args)  {

        Class c=TYT.class;

//        在不创建对象的情况下,对私有方法的操作
        try {
            Method method=c.getDeclaredMethod("su",null);
            //设置方法的访问权限,对于调用私有方法来说,这一步非常重要(必须有)
            method.setAccessible(true);
            method.invoke(c.newInstance(),null);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}

---------对公有有参方法的操作---------
TYT类

public class TYT {
    public void ou(String ty,int qw,double yu){
        System.out.println(ty+","+qw+","+yu);
    }

}

test类

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

public class test {
    public static void main(String[] args) {

        Class c=TYT.class;
         Method method= null;
        try {
            method = c.getMethod("ou", String.class, int.class, double.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            method.invoke(c.newInstance(),"张三",21,4999.9);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

}

---------对私有有参数方法的操作---------
TYT类

public class TYT {
  
    private void cu(String ty,int qw,double yu){
        System.out.println(ty+","+qw+","+yu);
    }

}

test类

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

public class test {
    public static void main(String[] args) {

        Class c=TYT.class;
         Method method= null;
        try {
            method = c.getDeclaredMethod("cu", String.class, int.class, double.class);
            //设置方法的访问权限,对于调用私有方法来说,这一步非常重要(必须有)
            method.setAccessible(true);
            method.invoke(c.newInstance(),"李四",22,5123.4);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
 }

里面有很多细节,需要仔细观看,这就是Java反射机制对一些方法的基本操作。如果有哪里不对的,欢迎大佬们指证。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值