转自:http://blog.csdn.net/csdn1234/article/details/2563806
一.定义
Class[] getDeclaredClasses()
返回类中定义的公共、私有、保护的内部类和接口。但不包括继承的内部类和接口。
Class[] getClasses()
返回类定义的公共的内部类和接口,以及从父类、父接口那里继承来的内部类和接口
二.例子
1.GrandFather.java
- public class GrandFather {
- public class ClassG_Pub {
- int x;
- }
- class ClassG_Pro {
- int x;
- }
- private class ClassG_Pri {
- int x;
- }
- public interface InterfG_Pub {
- public String getName();
- }
- interface InterfG_Pro {
- public String getName();
- }
- private interface InterfG_Pri {
- public String getName();
- }
- }
public class GrandFather {
public class ClassG_Pub {
int x;
}
class ClassG_Pro {
int x;
}
private class ClassG_Pri {
int x;
}
public interface InterfG_Pub {
public String getName();
}
interface InterfG_Pro {
public String getName();
}
private interface InterfG_Pri {
public String getName();
}
}
2.Father.java
- public class Father extends GrandFather{
- public class ClassF_Pub {
- int x;
- }
- class ClassF_Pro {
- int x;
- }
- private class ClassF_Pri {
- int x;
- }
- public interface InterfF_Pub {
- public String getName();
- }
- interface InterfF_Pro {
- public String getName();
- }
- private interface InterfF_Pri {
- public String getName();
- }
- }
public class Father extends GrandFather{
public class ClassF_Pub {
int x;
}
class ClassF_Pro {
int x;
}
private class ClassF_Pri {
int x;
}
public interface InterfF_Pub {
public String getName();
}
interface InterfF_Pro {
public String getName();
}
private interface InterfF_Pri {
public String getName();
}
}
3.Son.java
- public class Son extends Father{
- public class ClassS_Pub {
- int x;
- }
- class ClassS_Pro {
- int x;
- }
- private class ClassS_Pri {
- int x;
- }
- public interface InterfS_Pub {
- public String getName();
- }
- interface InterfS_Pro {
- public String getName();
- }
- private interface InterfS_Pri {
- public String getName();
- }
- public static void main(String args[]) throws Exception{
- System.out.println("getClasses:");
- for (Class c:Son.class.getClasses()) {
- System.out.println("class name:" + c.getName());
- }
- System.out.println("getDeclaredClasses:");
- for (Class c:Son.class.getDeclaredClasses()) {
- System.out.println("class name:" + c.getName());
- }
- }
- }
public class Son extends Father{
public class ClassS_Pub {
int x;
}
class ClassS_Pro {
int x;
}
private class ClassS_Pri {
int x;
}
public interface InterfS_Pub {
public String getName();
}
interface InterfS_Pro {
public String getName();
}
private interface InterfS_Pri {
public String getName();
}
public static void main(String args[]) throws Exception{
System.out.println("getClasses:");
for (Class c:Son.class.getClasses()) {
System.out.println("class name:" + c.getName());
}
System.out.println("getDeclaredClasses:");
for (Class c:Son.class.getDeclaredClasses()) {
System.out.println("class name:" + c.getName());
}
}
}
4.java Son的输出结果是:
- getClasses:
- class name:Son$InterfS_Pub
- class name:Son$ClassS_Pub
- class name:Father$InterfF_Pub
- class name:Father$ClassF_Pub
- class name:GrandFather$InterfG_Pub
- class name:GrandFather$ClassG_Pub
- getDeclaredClasses:
- class name:Son$InterfS_Pri
- class name:Son$InterfS_Pro
- class name:Son$InterfS_Pub
- class name:Son$ClassS_Pri
- class name:Son$ClassS_Pro
- class name:Son$ClassS_Pub