反射全的方法
package com.hu.Fs;
public class Creature <T>{
public double weight;
public void breath(){
System.out.println("生物会呼吸!");
}
}
***********************************
package com.hu.Fs;
import java.io.Serializable;
public interface MyInterface extends Serializable {
}
***************************************
package com.hu.Fs;
public class Person extends Creature<String> implements Comparable,MyInterface {
public String name;
public int age;
private int id;
private String favorat;
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 int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFavorat() {
return favorat;
}
public void setFavorat(String favorat) {
this.favorat = favorat;
}
public void display(String nation){
System.out.println("我的国籍是"+nation);
}
private String zuo(String name,int age,double weight){
System.out.println("sanwei"+weight);
return name;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", id=" + id
+ ", favorat=" + favorat + "]";
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
public Person(String name, int age, int id, String favorat) {
super();
this.name = name;
this.age = age;
this.id = id;
this.favorat = favorat;
}
public Person(String name) {
super();
this.name = name;
}
public Person(){
}
}
***************************************
package com.hu.Fs;
import static org.junit.Assert.*;
import org.junit.Test;
public class Testting {
@Test
public void test() throws InstantiationException, IllegalAccessException {
Class clazz=Person.class;
Person person=(Person) clazz.newInstance();
System.out.println(person);
}
}
*************************************************************
package com.hu.Fs;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.junit.Test;
public class Ts {
@Test
public void test() {
Class clazzClass=Person.class;
//1,常规方法只能访问到公开的属性
Field[] fields=clazzClass.getFields();//此方法只能获取到运行时类极其弗雷中的public属性
for(Field f:fields){
System.out.println(f);
System.out.println();
}
//2,可以访问私有的属性
Field[] field=clazzClass.getDeclaredFields();//此方法只能获取到运行时类本身声明的属性
for(Field d:field){
System.out.println(d);
}
}
@Test
public void test2(){
Class clazzClass=Person.class;
//1.获取属性
Field[] field=clazzClass.getDeclaredFields();
for(Field f:field){
int i=f.getModifiers();
String shu=Modifier.toString(i);
System.out.println(shu);
//2,获取类型
Class class1=f.getType();
System.out.println(class1);
//3,获取属性名
String nameString=f.getName();
System.out.println(nameString);
}
}
//获取运行时类的方法
@Test
public void getmothod(){
Class clazz=Person.class;
Method[] methods=clazz.getMethods();//获取运行时类极其父类中的public 方法
for(Method m:methods){
System.out.println(m);
System.out.println();
}
//2,获取自身的所有方法,包括私有方法
Method[] methods2=clazz.getDeclaredMethods();
for(Method m:methods2){
System.out.println(m);
//3,获取权限修饰符
String quanxianString =Modifier.toString(m.getModifiers());
System.out.print(quanxianString);
}
}
@Test
public void methid(){
Class clazz=Person.class;
Method[] methods2=clazz.getDeclaredMethods();
for(Method m:methods2){
//3,获取权限修饰符
String quanxianString =Modifier.toString(m.getModifiers());
System.out.println(quanxianString+" "+ " ");
System.out.print(" ");
//4,获取返回值类型
Class returntypeClass=m.getReturnType();
System.out.print(returntypeClass+" ");//这里也可以使returntypeClass.getName()
//5,获取名字
String nameString=m.getName();
System.out.print(nameString+" ");
//6,获取形参列表
System.out.println("(");
Class[] para= m.getParameterTypes();
for(int i=0;i<para.length;i++){
System.out.print(para[i].getName()+" " +"args-->"+i);
}
System.out.println(")");
//7,获取构造器
Constructor[] constructors=clazz.getDeclaredConstructors();
for(Constructor c:constructors){
System.out.println(c+"构造器");
}
}
}
@Test
public void testtt(){
//1,获取运行时类的父类
Class clazz=Person.class;
Class clazzfaClass=clazz.getSuperclass();
System.out.println(clazzfaClass);
//2,获取带泛型的父类
Type type=clazz.getGenericSuperclass();
System.out.println(type);
}
@Test
public void test33(){
//获取父类的泛型
Class clazzClass=Person.class;
Type type=clazzClass.getGenericSuperclass();
ParameterizedType param=(ParameterizedType)type;
Type[] argTypes=param.getActualTypeArguments();
System.out.println(((Class)argTypes[0]).getName());
//4,获取接口
Class[] iClasses=clazzClass.getInterfaces();
for(Class i:iClasses){
System.out.println(i);
}
//5,获取包
Package package1=clazzClass.getPackage();
System.out.println(package1);
}
}