}
public interface Shoots {
}
public interface Waterproof {
}
public class Toy {
Toy(){};//默认构造器,如果不带默认构造器,则不能使用newInstance();创建对象。
Toy(int i){};
}
public 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()+"]");
System.out.println("simple name:"+cc.getSimpleName()+"canonical name "+cc.getCanonicalName()+"");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Class c=null;
try{
c=Class.forName("com.tkij.chapter14.Class.FancyToy");
}catch(ClassNotFoundException e){
System.out.println("cannot found fancytoy");
System.exit(1);
}
printInfo(c);
for(Class face:c.getInterfaces()){
printInfo(face);
}
Class up=c.getSuperclass();
Object obj=null;
try{
obj=up.newInstance();//使用newInstance()创建的类,必须带有默认的构造器,否则会报InstantiationException异常!
}catch(InstantiationException e){
System.out.println("canot instantiate");
}catch(IllegalAccessException e){
System.out.println("cannot access");
System.exit(1);
}
printInfo(obj.getClass());
}
}/*output
class name:com.tkij.chapter14.Class.FancyToyis interface? [false]
simple name:FancyToycanonical name com.tkij.chapter14.Class.FancyToy
class name:com.tkij.chapter14.Class.HasBatteriesis interface? [true]
simple name:HasBatteriescanonical name com.tkij.chapter14.Class.HasBatteries
class name:com.tkij.chapter14.Class.Waterproofis interface? [true]
simple name:Waterproofcanonical name com.tkij.chapter14.Class.Waterproof
class name:com.tkij.chapter14.Class.Shootsis interface? [true]
simple name:Shootscanonical name com.tkij.chapter14.Class.Shoots
class name:com.tkij.chapter14.Class.Toyis interface? [false]
simple name:Toycanonical name com.tkij.chapter14.Class.Toy
*/