package wuyueily5.ToyTest;
interface HasBatteries{}
interface Waterproof{}
interface Shoots{}
class Toy
{
Toy(){}
Toy(int i){}
}
class FancyToy extends Toy implements HasBatteries,Waterproof,Shoots
{
FancyToy()
{
super(1);
}
}
public class ToyTest
{
static void printInfo(Class cc)
{
System.out.println("Class name: "+cc.getName()+" is interface["+ cc.isInterface()+"]");
}
public static void main(String[] args)
{
Class c=null;
try
{
c=Class.forName("FancyToy");
}
catch(ClassNotFoundException e)
{
System.out.println("Can't find FancyToy");
System.exit(1);
}
printInfo(c);
Class[] faces=c.getInterfaces();
for(int i=0; i<faces.length; i++)
{
printInfo(faces[i]);
}
Class cy=c.getSuperclass();
Object o=null;
try
{
o=cy.newInstance();
}
catch(InstantiationException e)
{
System.out.println("Cannot instantiate");
System.exit(1);
}
catch(IllegalAccessException e)
{
System.out.println("Cannot access");
System.exit(1);
}
printInfo(o.getClass());
}
}
程序运行总是抛出异常,说找不到类。是为什么?
我把第一行批包的语句去掉,再运行就能找到类了,这又是因为什么?
请高手于以指教。本人QQ1153701,有意共同学习Java就加我好了。