zoukankan      html  css  js  c++  java
  • 反射动态的获取方法

    import java.lang.reflect.Method;
    
    public class ReflectDemo4 {
    
        public static void main(String[] args) throws Exception {
            
    //        getMethodDemo();
            
    //        getMethodDemo2();
            
            getMethodDemo3();
            
        }
        //反射方法,非静态,有参数的show方法
        public static void getMethodDemo3() throws Exception{
            
            String className = "cn.qujianlei.domain.Person";
            Class clazz = Class.forName(className);
            
            Method method = clazz.getMethod("paramShow", String.class,int.class);
            
            Object obj = clazz.newInstance();
            
            method.invoke(obj, "xiaoqiang",40);
            
        }
        
        //反射方法,静态,无参数的show方法
        public static void getMethodDemo2() throws Exception{
            
            String className = "cn.qujianlei.domain.Person";
            Class clazz = Class.forName(className);
            
            Method method = clazz.getMethod("staticShow", null);
            
            method.invoke(null, null);
            
            
        }
        
        //反射方法,非静态,无参数的show方法
        public static void getMethodDemo() throws Exception{
            
            String className = "cn.qujianlei.domain.Person";
            Class clazz = Class.forName(className);
            
            Method method = clazz.getMethod("show", null);
            
            Object obj = clazz.newInstance();
            method.invoke(obj, null);
            
        }
    
    }
    public class Person {
            
        private String name;
        private int age;
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        
        public Person() {
            super();
            System.out.println("person run");
            
        }
        
        public void show(){
            System.out.println("person show run");
        }
        
        public static void staticShow(){
            System.out.println("person static show run");
        }
        
        public void paramShow(String name,int age){
            System.out.println("show:"+name+"---"+age);
        }
        
        
    }
  • 相关阅读:
    (网页)中的简单的遮罩层
    (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
    (网页)jQuery的时间datetime控件在AngularJs中使用实例
    Maven Myeclipse 搭建项目
    MyBatis 环境搭建 (一)
    java 常用方法
    XML 基础
    JS BOM
    js 事件
    js 的使用原则
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5929472.html
Copyright © 2011-2022 走看看