1. 程式人生 > >java反射類的字段

java反射類的字段

public field ati true illegal 靜態方法 [] main方法 import

java反射類的字段:

package com.ma.reflection;

import java.lang.reflect.Field;

import org.junit.Test;

import com.ma.bean.StudentBean;

/**
 * 反射字段
 * @author ma
 *
 */
public class Demo4 {
	/**
	 * 反射公有字段
	 * @throws ClassNotFoundException 
	 * @throws NoSuchFieldException 
	 * @throws SecurityException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test1() throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
		//加載類
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		
		StudentBean s = new StudentBean();
		
		//加載字段
		Field field = clazz.getField("name");
		
		//調用字段
		String name = (String)field.get(new StudentBean());
		
		//設置字段值
		field.set(s, (Object)"關悅");
		
		//輸出字段
		System.out.println(s.name);
		System.out.println(name);
		
	}
	/**
	 * 反射私有字段
	 * @throws ClassNotFoundException 
	 * @throws NoSuchFieldException 
	 * @throws SecurityException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test2() throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
		//加載類
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		StudentBean s = new StudentBean();
		//加載字段
		Field field = clazz.getDeclaredField("work");
		field.setAccessible(true);
		
		//獲取字段
		String work = (String)field.get(s);
		
		System.out.println(work);
	}
	/**
	 * 反射靜態字段
	 * @throws ClassNotFoundException 
	 * @throws NoSuchFieldException 
	 * @throws SecurityException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@Test
	public void test3() throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
		////加載類
		Class<?> clazz = Class.forName("com.ma.bean.StudentBean");
		StudentBean s = new StudentBean();
		//加載字段
		Field field = clazz.getDeclaredField("sex");
		field.setAccessible(true);
		//獲取字段
		String sex = (String)field.get(s);
		
		System.out.println(sex);
	}
	/**
	 * 反射字段
	 */
	@Test
	public void test4(){
		//
		//
		//
	}
	/**
	 * 反射字段
	 */
	@Test
	public void test5(){
		//
		//
		//
	}
}

  實體類Student

package com.ma.bean;
/**
 * 學生類
 * @author ma
 *
 */
public class StudentBean {
	
	public String name = "劉備";
	public int age;
	private String work = "教師";
	private static String sex = "男";
	
	
	public StudentBean() {
		super();
	}

	public void eat(){
		System.out.println("吃飯了。。。。。。。。。。。。。。");
	}
	
	public void play(String name,int age){
		System.out.println(name+":去玩了喲。。。。。。。。。"+age);
	}
	
	public void run(int  ...intaa){
		System.out.println(":這是可變參數方法");
	}
	
	private void jump(){
		System.out.println("這是私有方法");
	}
	
	public static void sleep(int age){
		System.out.println(age+":這是靜態方法");
	}
	
	public static void main(String[] args) {
		System.out.println("這是main方法!!!");
	}
}

  

java反射類的字段