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 2019/1/5 16:30
     */
    public class ReflectDemo1 {
    
        public static void main(String[] args) throws Exception {
    
            // 获取字节码文件对象
            Class c = Class.forName("com.yjf.esupplier.common.test.Person");
    
            Constructor con = c.getDeclaredConstructor(String.class);
            //暴力访问,值为true则指示反射的对象在使用时应该取消Java语言访问检查
            con.setAccessible(true);
            Object obj = con.newInstance("张三");
    
            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
                        + "]";
            }
    
    }
    终身学习者
  • 相关阅读:
    C语言之回调函数&模块化
    680. 验证回文字符串 Ⅱ
    C++指针数组和数组指针
    345. 反转字符串中的元音字母
    633.平方数之和
    IDM使用介绍篇
    路由器无线桥接WDS
    约数的个数
    密码翻译
    查找学生信息
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/11275223.html
Copyright © 2011-2022 走看看