首页 > 其他分享 >反射之类的加载器

反射之类的加载器

时间:2022-09-29 19:22:29浏览次数:48  
标签:反射 String age println 之类 加载 public name

反射之类的加载器

@Test
    public void test1(){
        //对于自定义类,使用系统类加载器进行加载
        ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
        System.out.println(classLoader);//系统的加载器
        //调用系统类加载器的getParent():获取扩展类加载器
        ClassLoader classLoader1 = classLoader.getParent();
        System.out.println(classLoader1);//扩展类加载器

        //调用扩展类加载器的getParent():无法获取引导类加载器
        //引导类加载器主要负责加载java的核心类库,无法加载自定义类的。
        ClassLoader classLoader2 = classLoader1.getParent();
        System.out.println(classLoader2);//引导类加载器,不能拿到

        ClassLoader loader = String.class.getClassLoader();
        System.out.println(loader);//引导类加载器,不能拿到

    }

    /*
    Properties:用来读取配置文件。
     */
    @Test
    public void test2() throws Exception {
        Properties pros = new Properties();
        //此时的文件默认在当前的module下
        //读取配置文件的方式一:
//        FileInputStream fis = new FileInputStream("src\\jdbc1.properties");
//        pros.load(fis);
        //读取配置文件的方式二:
        //配置文件默认识别为当前module下的src下
//        ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
//        InputStream ips = classLoader.getResourceAsStream("jdbc1.properties");
//        pros.load(ips);

        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        System.out.println("user=" + user + ",password=" + password );

    }

反射的动态性的体会

@Test
public void test2(){

    for(int i = 0;i < 100;i++){
        int num = new Random().nextInt(3);//0,1,2
        String classPath = "";
        switch(num){
            case 0:
                classPath = "java.util.Date";
                break;
            case 1:
                classPath = "java.lang.Object";
                break;
            case 2:
                classPath = "com.atguigu.java.Person";
                break;
        }

        try {
            Object obj = getInstance(classPath);
            System.out.println(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}

/*
创建一个指定类的对象。
classPath:指定类的全类名
 */
public Object getInstance(String classPath) throws Exception {
    Class clazz =  Class.forName(classPath);
    return clazz.newInstance();
}
Person类
public class Person {
    private String name;
    public int age;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person() {
        System.out.println("无参构造器");
    }

    private Person(String name) {
        this.name = name;
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void show(){
        System.out.println("hello i am people");
    }

    private String showNation(String nation){
        System.out.println("i am from " + nation);
        return nation;
    }
}

标签:反射,String,age,println,之类,加载,public,name
From: https://www.cnblogs.com/blwx/p/16742710.html

相关文章

  • 反射类
    反射类一,关于java.lang.Class类的理解关于java.lang.Class类的理解1.类的加载过程:程序经过javac.exe命令以后,会生成一个或多个字节码文件(.class结尾)。接着我们使用j......
  • 关于IDEA发出基于APR的本地库加载失败错误的解决------->求解决!
    问题描述在没有使用Maven项目启动该Project时,Tomcat可以正常使用,但在这里会显示这样的错误:这个错误,已经查了两天了,相关文件以及解决方法已经翻烂了,还没有解决,放出来......
  • # 安卓中实现分页加载方案总结(一)——基于SmartRefreshLayout
    安卓中实现分页加载方案总结(一)——基于SmartRefreshLayout背景项目中多场景使用到了列表分页加载的操作,比如消息列表,先总结一下基于SmartRefreshLayout的分页加载方案。......
  • 找不到或无法加载主类
       我从来不相信什么懒洋洋的自由。我向往的自由是通过勤奋和努力实现的更广阔的人生。我要做一个自由又自律的人,靠势必实现的决心认真地活着。......
  • 并发反射服务器
    #include<stdio.h>#include<stdlib.h>#include<sys/socket.h>#include<sys/wait.h>#include<netinet/in.h>#include<arpa/inet.h>#include<time.h>#include......
  • mapboxgl加载tiff
    缘起近期在项目中遇到这么一个需求,需要在地图上展示一组格网数据,格网大小为2m*2m,地图api用的mapboxgl。起初拿到这个需要感觉很easy,在地图上添加一个fill图层就好啦。把格......
  • mybatis在spring mvc中的加载过程
    本地搭建了一套工程,把spring-5.2.x源码与mybatis-3.5.11源码做了整合,debug了一下mybatis-spring在springmvc中的加载过程。画了下面的图,删减了一些说明,图比较简练。后续......
  • vue组件中如何首次加载就触发watch
    watch:{"val":{immediate:true,//首次加载的时候执行函数deep:true,//深入观察数组值的变化,inputVal:function(){}}} ......
  • SpringBoot系列——加载自定义配置文件
    Java开发过程中,一些配置信息不想写到application.properties里面去,想自己弄一个配置文件,然后加载。例子如下:Employee.java类核心代码:@Configuration//用来标注一个自定......
  • 反射快速入门
    创建Cat模型packagecom.reflection.domain;publicclassCat{privateStringname="小喵";publicvoidhi(){System.out.println("hi"+nam......