• 反射有关的定义


    package com.hu;

    public class Person {
    public String name;
    private int 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;
    }
    @Override
    public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
    }

    public Person() {
    super();
    }

    public Person(String name) {
    super();
    this.name = name;
    }

    public Person(int age) {
    super();
    this.age = age;
    }
    public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }
    public void show(){
    System.out.println("我是一个人");
    }
    public void nation(String nation){
    System.out.println("我的国际是"+nation);
    }


    }

    *************************************************

    package com.hu;

    import static org.junit.Assert.*;

    import java.beans.FeatureDescriptor;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.text.FieldPosition;
    import java.util.Properties;

    import org.junit.Test;

    public class TestReflection {

    @Test
    public void test() {
    //反射之前的调用
    Person person=new Person();
    person.setAge(11);
    person.setName("Muyongyuan");
    person.show();
    person.nation("HK");
    System.out.println(person);
    }
    //有了反射可以通过反射创建一个类的对象 ,并调用其中的方法
    @Test
    public void test2() throws Exception{
    //创建clazz运行时类对应的Person类对象
    //如果知道类型就是用泛型,如果不知道需要强转
    //Class clazz=Person.class;
    //Person p=(Person)clazz.newInstance();
    Class<Person> clazz=Person.class;
    Person person=clazz.newInstance();
    Field f1=clazz.getField("name");
    f1.set(person, "Liyunlong");
    System.out.println(person);
    //下面一段代码是用反射调用私有属性的方案
    Field f2=clazz.getDeclaredField("age");
    f2.setAccessible(true);
    f2.set(person, 23);
    System.out.println(person);

    //通过calzz 调用运行时类的方法
    Method method=clazz.getMethod("show");
    method.invoke(person);
    //含有参数的调用
    Method m2=clazz.getMethod("nation", String.class);
    m2.invoke(person, "Erop");

    }
    @Test
    public void test34(){
    //java.lang.Calss是反射的源头
    Person p=new Person();
    Class clazz=p.getClass();
    System.out.println(clazz);//通过运行时类的对象,调用getclass()返回运行时类
    }


    //如何获取Class事例(3种方法)
    @Test
    public void test5() throws ClassNotFoundException{
    //1,直接对用运行时类本身的.class方法
    Class<Person> clazz=Person.class;
    System.out.println(clazz.getName());
    //2,通过运行时类的对象来创建
    Person p =new Person();
    Class clazzClass=p.getClass();
    System.out.println(clazzClass.getName());
    //3通过Class的静态方法
    String classSt="com.hu.Person";
    Class clazz4=Class.forName(classSt);
    System.out.println(clazz4);
    //4,了解通过类的加载器
    ClassLoader classLoader=this.getClass().getClassLoader();
    classLoader.loadClass(classSt);
    System.out.println(classLoader.loadClass(classSt));
    }
    //类的加载器
    @Test
    public void test323() throws IOException{
    ClassLoader classLoader=ClassLoader.getSystemClassLoader();
    System.out.println(classLoader);

    ClassLoader classLoader2=classLoader.getParent();
    System.out.println(classLoader2);

    ClassLoader classLoader3=classLoader2.getParent();
    System.out.println(classLoader3);

    //掌握一下的用法
    ClassLoader clazzload=this.getClass().getClassLoader();
    System.out.println(clazzload.getClass());
    InputStream inputStream= clazzload.getResourceAsStream("com/hu/jdbc.properties");
    Properties properties=new Properties();
    properties.load(inputStream);
    String usString=properties.getProperty("user");
    System.out.println(usString);
    }
    @Test
    public void test233() throws IOException{
    //如果创建的在当前的工程下的时候调用
    FileInputStream fileInputStream=new FileInputStream(new File("jdbc.properties"));
    Properties properties=new Properties();
    properties.load(fileInputStream);
    String password=properties.getProperty("password");
    System.out.println(password);
    }
    @Test
    public void test555() throws Exception, InvocationTargetException{
    Class claz=Person.class;
    Constructor method=claz.getDeclaredConstructor(String.class,int.class);
    method.setAccessible(true);
    Person person=(Person) method.newInstance("Wang",10);
    System.out.println(person);
    }

    }

  • 相关阅读:
    git的学习笔记
    软件工程概论第一周综合测验
    人月神话读后感
    软件工程概论通读第十一章
    软件工程概论通读第十章
    软件工程概论通读第九章
    软件工程概论通读第八章
    软件工程概论通读第七章
    软件工程概论通读第六章
    软件工程概论通读第五章
  • 原文地址:https://www.cnblogs.com/afterhours/p/6229051.html
Copyright © 2020-2023  润新知