区别
get…是返回所有的…包括其父类的所有…
getDeclared…是只返回属于它的…
代码演示
class person{
private float height;
private float weight;
void getPersonName() {
}
public void getPersonHeight() {
}
private void getPersonWeight() {
}
protected void getPersonColor() {
}
}
class student extends person {
private String name;
private int age;
public student() {
super();
}
void getStudentHeight() {
}
public void getStudentName() {
}
private void getStudentAge() {
}
protected void getStudentColor() {
}
}
public class reflect {
public static void main(String[] args) {
try {
Class classzz = Class.forName("student");
System.out.println("classzz = " + classzz);
//getMethods方法
Method[] methods = classzz.getMethods();
System.out.println(" ----getMethods: ");
for(Method m : methods) {
System.out.println(m);
}
//getDeclaredMethods 方法
Method[] declaredMethods = classzz.getDeclaredMethods();
System.out.println("\r\n ----declaredMethods: ");
for(Method m : declaredMethods) {
System.out.println(m);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}