zoukankan      html  css  js  c++  java
  • java映射

    虚拟机通过得到类的字节码文件,从而创建对象。在java代码中可用Class类中的方法进行实现,

     1 package javase;
     2 
     3 import java.lang.reflect.Constructor;
     4 import java.lang.reflect.InvocationTargetException;
     5 
     6 public class ReflextDemo {
     7     
     8     public static void method2() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
     9         
    10         String name = "test.Person";//把类名的路径存到字符串
    11         Class clazz = Class.forName(name);//通过路径拿到字节码文件,创建对象
    12         Constructor<Person> con = clazz.getConstructor(String.class,int.class);//获取构造器
    13         Object obj = con.newInstance("小强",10);//创建实例
    14         
    15     }
    16 
    17     public static void method1() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    18         
    19         String name = "test.Person";//把类名的路径存到字符串
    20         Class clazz = Class.forName(name);//通过路径拿到字节码文件,创建对象
    21         Object obj = clazz.newInstance();//创建实例
    22         
    23     }
    24     
    25     public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    26 
    27         method2();
    28         
    29     }
    30 
    31 }
  • 相关阅读:
    移动端rem屏幕设置
    封装ajax库,post请求
    获取浏览器url参数
    身份证验证
    jq封装插件
    页面分享功能,分享好友、朋友圈判断,用share_type做标记 这里用的是jweixin-1.3.2.js
    @RequestParam和@RequestBody区别
    400报错
    IDEA中用Maven构建SSM项目环境入门
    Eclipse搭建Spring开发环境
  • 原文地址:https://www.cnblogs.com/lsy-lsy/p/10926938.html
Copyright © 2011-2022 走看看