Java SE之反射技术[Class](三)

/**
 * 
 * @author Zen Johnny
 *
 */
package com.cpms.test;

import java.lang.reflect.Field;
import java.util.List;

public class ReflectDemo {
	public static void clazz() {
		Person employee = new Person();
		Class<Person> clazz = (Class<Person>) employee.getClass();
		
		System.out.println("getCanonicalName: " + clazz.getCanonicalName());
		System.out.println("getName: " + clazz.getName());
		System.out.println("getSimpleName: " + clazz.getSimpleName());
		System.out.println("getTypeName: " + clazz.getTypeName());
	}
	
	public static void main(String args[]) {
		clazz();
	}
}

class Person{
	public String nickName;//public field
	private String realName;
	private int age;
	private List<String> friends;
	private double money; //no setter and getter
	
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public List<String> getFriends() {
		return friends;
	}
	public void setFriends(List<String> friends) {
		this.friends = friends;
	}
	
	@Override
	public String toString() {
		return "Person [nickName=" + nickName + ", realName=" + realName + ", age=" + age + ", friends=" + friends
				+ ", money=" + money + "]";
	}
}

  输出:

getCanonicalName: com.cpms.test.Person
getName: com.cpms.test.Person
getSimpleName: Person
getTypeName: com.cpms.test.Person

  

原文地址:https://www.cnblogs.com/johnnyzen/p/7828630.html