zoukankan      html  css  js  c++  java
  • 【通过反射去获取有参构造方法并使用】

    package com.yjf.esupplier.common.test;
    
    import java.lang.reflect.Constructor;
    
    /**
     * @author shusheng
     * @description 通过反射去获取有参构造方法并使用
     * @Email shusheng@yiji.com
     * @date 2018/12/29 13:41
     */
    public class ReflectDemo {
    
        public static void main(String[] args) throws Exception {
            // 获取字节码文件对象
            Class c = Class.forName("com.yjf.esupplier.common.test.Person");
            // 获取带参构造方法对象
            // public Constructor<T> getConstructor(Class<?>... parameterTypes)
            Constructor con = c.getConstructor(String.class,int.class,String.class);
    
            // 通过带参构造方法对象创建对象
            // public T newInstance(Object... initargs)
            Object obj = con.newInstance("林青霞", 27, "北京");
    
            System.out.println(obj);
        }
    
    }
    package com.yjf.esupplier.common.test;
    
    /**
     * @author shusheng
     * @description
     * @Email shusheng@yiji.com
     * @date 2018/12/29 13:42
     */
    public class Person {
    
            private String name;
            int age;
            public String address;
    
            public Person() {
            }
    
            private Person(String name) {
                this.name = name;
            }
    
            Person(String name, int age) {
                this.name = name;
                this.age = age;
            }
    
            public Person(String name, int age, String address) {
                this.name = name;
                this.age = age;
                this.address = address;
            }
    
            public void show() {
                System.out.println("show方法的输出");
            }
    
            public void method(String s) {
                System.out.println("method方法的输出: " + s);
            }
    
            public String getString(String s, int i) {
                return s + "---" + i;
            }
    
            private void function() {
                System.out.println("function方法的输出");
            }
    
            @Override
            public String toString() {
                return "Person [name=" + name + ", age=" + age + ", address="
                        + address
                        + "]";
            }
    
    }
    终身学习者
  • 相关阅读:
    COM组件开发实践(七)---多线程ActiveX控件和自动调整ActiveX控件大小(上)
    A Complete ActiveX Web Control Tutorial
    C++使用VARIANT实现二维数组的操作
    用户自定义结构数据与VARIANT转换
    VS2008中使用JSONCPP方法小结
    HDOJ 2030 汉字统计
    HDOJ 1312 (POJ 1979) Red and Black
    POJ 1503 Integer Inquiry 简单大数相加
    POJ 1936 All in All
    枚举法
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/11275221.html
Copyright © 2011-2022 走看看