日期:2014-05-20  浏览次数:20661 次

怎么得到类的修饰符和超类
怎么得到类的修饰符和超类

------解决方案--------------------
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import javax.swing.*;
abstract class BaseClass
{
}
class MyClass extends BaseClass
{
public MyClass()
{
}
public MyClass(int x,String str)
{
}
}
public class Reflection2
{
static void ClassOrInterface(Class c)
{
String name = c.getName();
if (c.isInterface())
System.out.println(name + " is an interface");
else
System.out.println(name + " is a class");
}
public static void main(String[] args) throws Exception
{
MyClass myClassObject=new MyClass();
Class c = myClassObject.getClass();
Class s = c.getSuperclass();
System.out.println("SuperClass of myClassObject = "+
s.getName());
ClassOrInterface(s);
ClassOrInterface(Serializable.class);
int m = s.getModifiers();
if (Modifier.isPublic(m))
System.out.println("Class of "+
s.getName()+ " is "+" public");
if (Modifier.isAbstract(m))
System.out.println("Class of "+
s.getName()+ " is "+" abstract");
if (Modifier.isFinal(m))
System.out.println("Class of "+
s.getName()+ " is "+" final");
}
}