J2EE&反射

目录

一.什么是反射

       用实体类Student做示范

三.反射实例化

四.反射动态方法调用

五.反射读写属性


一.什么是反射

反射java语言中的一种机制,通过这种机制可以动态的实例化对象,读写属性,调用方法

二.类类

  1. Class.forName(完整类名)
  2. 类名 .class
  3. 对象 .getClass

       用实体类Student做示范

package com.zking.fanshe;

public class Student {
	private String sid;

	private String sname;

	public Integer age;
	
	static{
		System.out.println("加载进jvm中!");
	}

	public Student() {
		super();
		System.out.println("调用无参构造方法创建了一个学生对象");
	}

	public Student(String sid) {
		super();
		this.sid = sid;
		System.out.println("调用带一个参数的构造方法创建了一个学生对象");
	}

	public Student(String sid, String sname) {
		super();
		this.sid = sid;
		this.sname = sname;
		System.out.println("调用带二个参数的构造方法创建了一个学生对象");
	}

	@SuppressWarnings("unused")
	private Student(Integer age) {
		System.out.println("调用Student类私有的构造方法创建一个学生对象");
		this.age = age;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public void hello() {
		System.out.println("你好!我是" + this.sname);
	}

	public void hello(String name) {
		System.out.println(name + "你好!我是" + this.sname);
	}

	@SuppressWarnings("unused")
	private Integer add(Integer a, Integer b) {
		return new Integer(a.intValue() + b.intValue());
	}

	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age + "]";
	}
	
	
}
	//1.Class.forname()
	Class stuClass	=Class.forName("com.zking.fanshe.Student");
		//2.类
	Class Class = Student.class;
	
		//3.对象 .getClass
	Student student = new Student();
	Class class2 = student.getClass();
	
	System.out.println(stuClass);
	System.out.println(class2);
	System.out.println(Class);

三.反射实例化

  • getConstructor
  • getDeclaredConstructor
  • newInstance
    //实例化对象
    	Constructor con = stuClass.getConstructor();
    	Student stu = (Student) con.newInstance();
    	stu.setSid("001");
    	stu.setSname("宝宝");
    	System.out.println(stu);
    	
    	//有参 参数构造
    	Constructor con1 = stuClass.getConstructor(String.class,String.class);
    	Student stu02 = (Student) con1.newInstance("004","瑶宝");
    	System.out.println(stu02);
    
        //私有方法①
    	Constructor con3 = stuClass.getDeclaredConstructor(Integer.class);
    	con3.setAccessible(true);
    	Student stu04 = con3.newInstance(18);
    	System.out.println(stu04);

四.反射动态方法调用

  • getMethod
  • getDeclaredMethod
    //无参的公共方法
	Method method = stuClass.getMethod("hello");
	method.invoke(stu);
	
	//有参的公共方法
	Method method2 = stuClass.getMethod("hello", String.class);
	method2.invoke(stu, "辉辉");
	
	//②
	Method method3 = stuClass.getDeclaredMethod("add", Integer.class,Integer.class);
	method3.setAccessible(true);
	int n = (Integer) method3.invoke(stu, 1,2);
	System.out.println(n);

五.反射读写属性

  • getField
  • getDeclaredField
    //公有反射属性
	Field field = stuClass.getField("age");
	field.set(stu, 98);
	System.out.println(stu);
	
	//私有反射属性
	Field sname = stuClass.getDeclaredField("sname");
	sname.setAccessible(true);
	sname.set(stu, "小红花");
	System.out.println(stu);

你可能感兴趣的:(java,开发语言,eclipse,java-ee)