class Person{
public String name;
public int age;
private int sex;
public Person(){
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
private void getSex(){
System.out.println(sex);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
Class<String> stringClass = String.class;
System.out.println(stringClass.getModifiers());//17因为String类的修饰符是public final
(2)String = getName():获取类的全名包括包名
String = getSimpleName():获取类名
Class<String> stringClass = String.class;
String name = stringClass.getName();
String simpleName = stringClass.getSimpleName();
System.out.println(name);//java.lang.String
System.out.println(simpleName);//String
(3)Package = getPackage()//获得此类所在的包
Class<String> stringClass = String.class;
Package stringClassPackage = stringClass.getPackage();
System.out.println(stringClassPackage.getName());//java.lang
(4)Class = getSuperClass()//获得此类的父类
Class<String> stringClass = String.class;
Class<? super String> stringClassSuperclass = stringClass.getSuperclass();
System.out.println(stringClassSuperclass.getName());//java.lang.Object
(5)Class[] = getInterFaces();获得此类所实现的接口
Class<String> stringClass = String.class;
Class[] interfaces = stringClass.getInterfaces();
for (Class anInterface : interfaces) {
System.out.println(anInterface.getName());
}
/*
java.io.Serializable
java.lang.Comparable
java.lang.CharSequence
*/
(6)Object = newInstance()//相当于调用类中的无参构造创建此类对象
Class<String> stringClass = String.class;
String s = stringClass.newInstance();
s = "123";
System.out.println(s);//123
(7)Filed = getField(String)//获取该类的某一个属性
Class<Person> personClass = Person.class;
Field stringClassField = personClass.getField("name");
System.out.println(stringClassField.getName());//name
(8)Filed[] = getFileds();//获取到该类的所有非private修饰的成员变量,包括继承的
Class<Person> personClass = Person.class;
Field[] fields = personClass.getFields();
for (Field field : fields) {
System.out.println(field.getName());
}
/*
* name
* age
*/
(9)Filed[] = getDeclaredField()//获取到共有的私有的属性,但是没有获取到父类的
Class<Person> personClass = Person.class;
Field[] declaredFields = personClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField.getName());
}
/*name
age
sex*/
(10)Filed = getDeclaredField(String)//获取到此类的一个私有属性
Class<Person> personClass = Person.class;
Person p1 = personClass.newInstance();//获取一个该类对象
Field sex = personClass.getDeclaredField("sex");//获取到该类的私有属性sex
sex.setAccessible(true);//这个属性是否可以重新设置 true表示可以重新设置
sex.set(p1,1);//将p1的sex属性设置为1
System.out.println(p1);//打印输出
下图是程序的运行结果,可以看出已经改掉了sex的值
以下代码分别可以获得各个的Class对象:
//获得Object类的Class对象
Class<Object> aClass = Object.class;
//获得接口Comparable的Class对象
Class<Comparable> comparableClass = Comparable.class;
//获得一维数组的Class对象
Class<String[]> aClass1 = String[].class;
//获得二维数组的Class对象
Class<int[][]> aClass2 = int[][].class;
//获得注解的Class对象
Class<ElementType> elementTypeClass = ElementType.class;
Class<Override> overrideClass = Override.class;
//获得包装类的Class对象
Class<Integer> integerClass = Integer.class;
//获得void的Class对象
Class<Void> voidClass = void.class;
//获得Class的Class对象
Class<Class> classClass = Class.class;