Java foundation - Reflection

In this paper, we talk about the use of reflection, and gradually supplement the relevant content.

Article directory


First, define a common class:

	package cn.itcast.test09;
	
	public class Person {
		//attribute
		private String name;
		private String id;
		//Construction method without parameters
		public Person() {}
		//Parametric construction
		public Person(String name, String id) {
			this.name = name;
			this.id = id;
		}  
		//Common method
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
	}

1. Obtain Class

There are three ways to get a Class:

		// Get Class
		Class clazz1 = Person.class;
		Class clazz2 = new Person().getClass();
		Class clazz3 = Class.forName("cn.itcast.test09.Person");

2. Common operation methods

	//Operate common methods, such as setName
	@Test
	public void test4() throws Exception {
		//Get Class
		Class c4 = Class.forName("cn.itcast.test09.Person");
		//Get the Person instance
		Person p4 = (Person) c4.newInstance();
		//Get common methods
		//c4.getDeclaredMethods(); / / get all common methods
		Method m1 = c4.getDeclaredMethod("setName", String.class);
		//The private method of the operation needs to be set to true
		//m1.setAccessible(true);
		//Let the setName method execute, execute the set value
		m1.invoke(p4, "niuqi");
		System.out.println(p4.getName());
	}

3. Operation attribute

	//Action name attribute
	@Test
	public void test3() {
		try {
			//Get Class
			Class c2 = Class.forName("cn.itcast.test09.Person");
			//Get the name attribute
			//c2.getDeclaredFields(); / / get all attributes
			//Get an instance of the Person class
			Person p11 = (Person) c2.newInstance();
			Field f1 = c2.getDeclaredField("name");
			//Set actionable private properties
			f1.setAccessible(true);
			//Set name value
			f1.set(p11, "wangwu"); //Equivalent to p.name = "wangwu";
			System.out.println(f1.get(p11)); //Equivalent to p.name
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

4. Operation constructor

4.1. Parameter constructor
	//Construction method of operation with parameters
	@Test
	public void test2() throws Exception {
		//Get Class
		Class c1 = Class.forName("cn.itcast.test09.Person");
		//Using the construction method with parameters
		//c1.getConstructors(); / / get all construction methods
		//Passing is the parameter type in the construction method with parameters. The type is passed in the form of class
		Constructor cs = c1.getConstructor(String.class,String.class);
		//Set the value through the construction method with parameters
		//Creating a Person instance through a construction method with parameters
		Person p1 = (Person) cs.newInstance("lisi","100");
		System.out.println(p1.getId()+" "+p1.getName());
	}
4.1. Parameterless constructor
	//Construction method of operation without parameters
	@Test
	public void test1() throws Exception {
		//Get Class
		Class c3 = Class.forName("cn.itcast.test09.Person");
		//Get an instance of the Person class
		Person p = (Person) c3.newInstance();
		//Set value
		p.setName("zhangsan");
		System.out.println(p.getName());
	}

Keywords: Attribute P4

Added by BobRoberts on Fri, 06 Dec 2019 23:54:17 +0200