java反射的运用基础

本文详细介绍了Java反射机制,通过示例展示了如何通过对象获取类的构造器、属性和方法。首先创建了一个Student类,然后分别用三种方式获取类名、包名。接着,演示了获取构造器的方法,并区分了公共构造器与私有构造器。最后,讨论了如何获取类的属性和方法,包括字段和公共方法以及私有方法。这些内容深入浅出地解释了Java反射的核心概念和使用场景。
摘要由CSDN通过智能技术生成

反射的定义:在Java中类是对象的模板,而反射就是要通过对象来找出类的构造方法,属性,类名,参数等。

一,建立一个Student类 并生成他的构造方法 等一系列方法

public class Student {
  public String name;
  public int age;
  public   String gender;


    public Student(String name, int age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name) && Objects.equals(gender, student.gender);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, gender);
    }

    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 String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    private Student() {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

}

二,通过对象找出类名,包名,共有三种方法

public class ReflectSarter {
    public static void main(String[] args) throws ClassNotFoundException {
//        Student student = new Student();
//第一种方式
//        Class<? extends Student> stu1 = student.getClass();
//第二种方法
        Class<Student> stu2 = Student.class;
//第三种方法
        Class<?> stu3 = Class.forName("com.mbiletrin.Student");
//        System.out.println(stu1);
        System.out.println(stu2);
        System.out.println(stu3);


    }
}

三,通过对象找出构造器

public class RefletSecond {
    public static void main(String[] args) throws NoSuchMethodException {
        Class<Student> stuclz = Student.class;
//        Constructor<Student> const1 = stuclz.getConstructor();
        Constructor<Student> const2 = stuclz.getConstructor(String.class, int.class, String.class);
        Constructor<?>[] const3 = stuclz.getConstructors();
//        System.out.println(const1);
        System.out.println(const2);
        System.out.println(Arrays.asList(const3));



        Constructor<Student> dcont1 = stuclz.getDeclaredConstructor();
        Constructor<Student> dcont2 = stuclz.getDeclaredConstructor(String.class, int.class, String.class);
        Constructor<?>[] dcont3 = stuclz.getDeclaredConstructors();
        System.out.println(dcont1);
        System.out.println(dcont2);
        System.out.println(Arrays.asList(dcont3));

    }
}

四,通过对象去找属性

public class ReflFourth {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<Student> stuclz = Student.class;
        Field name = stuclz.getField("name");
        Field[] fields = stuclz.getFields();
        System.out.println(name);
        System.out.println(Arrays.asList(fields));
        Field gender = stuclz.getDeclaredField("gender");
        Field[] declaredFields = stuclz.getDeclaredFields();
        System.out.println(gender);
        System.out.println(Arrays.asList(declaredFields));

    }
}

五,通过对象去找方法

public class ReflecTird {
    public static void main(String[] args) throws NoSuchMethodException {
        Class<Student> stud = Student.class;
        Method getName = stud.getMethod("getName");
        Method setName = stud.getMethod("setName", String.class);
        Method[] methods = stud.getMethods();
        System.out.println(getName);
        System.out.println(setName);
        System.out.println(Arrays.asList(methods));
        System.out.println("--------------------------------------------------");
        Method method = stud.getDeclaredMethod("getAge");
        Method getName1 = stud.getDeclaredMethod("getName");
        Method[] m3 = stud.getDeclaredMethods();
        System.out.println(method);
        System.out.println(getName1);
        System.out.println(Arrays.asList(m3));

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值