获取类的信息
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 Main{
static void printInfo(Class cc){
System.out.println("Class name: " + cc.getName() + " is interface? [" + cc.isInterface() + "]");
System.out.println("Simple name: " + cc.getSimpleName());
System.out.println("Canonical name : " + cc.getCanonicalName());
}
public static void main(String[] args){
Class c = null;
try {
c = Class.forName("com.company.FancyToy");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
printInfo(c);
for(Class face : c.getInterfaces()){
printInfo(face);
}
Class up = c.getSuperclass();
Object obj = null;
try {
obj = up.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
printInfo(obj.getClass());
}
}
运行结果:
Class name: com.company.FancyToy is interface? [false]
Simple name: FancyToy
Canonical name : com.company.FancyToy
Class name: com.company.HasBatteries is interface? [true]
Simple name: HasBatteries
Canonical name : com.company.HasBatteries
Class name: com.company.Waterproof is interface? [true]
Simple name: Waterproof
Canonical name : com.company.Waterproof
Class name: com.company.Shoots is interface? [true]
Simple name: Shoots
Canonical name : com.company.Shoots
Class name: com.company.Toy is interface? [false]
Simple name: Toy
Canonical name : com.company.Toy
使用newInstance()来创建对象的类,必须要有默认的构造方法
加载类方法
class Initable{
static final int staticFinal = 47;
static final int StaticFinal2 = (int)Math.random()*1000;
static {
System.out.println("Initializing Initable");
}
}
class Initable2 {
static int staticNonFinal = 147;
static {
System.out.println("Initializing Initable2");
}
}
class Initable3 {
static int staticNonFinal = 74;
static {
System.out.println("Initializing Initable3");
}
}
public class Main{
public static void main(String[] args) throws ClassNotFoundException {
Class initable = Initable.class;
System.out.println("After create Initable ref");
System.out.println(Initable.staticFinal);
System.out.println(Initable.StaticFinal2);
System.out.println(Initable2.staticNonFinal);
// Class initable3 = Class.forName("com.company.Initable3");
System.out.println("After create Initable3 ref");
System.out.println(Initable3.staticNonFinal);
}
}
执行结果:
After create Initable ref
47
Initializing Initable
0
Initializing Initable2
147
After create Initable3 ref
Initializing Initable3
74
可以发现,调用“.class”加载方式不自动初始化static和执行构造方法;而“Class.forName”会初始化static和执行构造函数。在执行“Initable.staticFinal“时,也没有执行构造函数,因为“Initable.staticFinal“有final修饰符,为编译期常量,其实如果将“Initable2.staticNonFinal”修饰为final,打印时,将不会执行构造方法。那为什么Initable执行了构造方法呢?因为虽然“Initable.StaticFinal2”也为final,但是被随机函数赋值的,不是一个编译期常量
使用泛型,利用newInstance()方法生成对象保存在list中
class CountedInteger{
private static long counter;
private final long id = counter++;
public String toString(){
return Long.toString(id);
}
}
class FilledList{
private Class type;
public FilledList(Class type){
this.type = type;
}
public List create(int nElements){
List result = new ArrayList();
try {
for (int i = 0; i < nElements; i++) {
result.add(type.newInstance());
}
}catch (Exception e){
throw new RuntimeException(e);
}
return result;
}
}
public class Main{
public static void main(String[] args) throws ClassNotFoundException {
FilledList fl = new FilledList(CountedInteger.class);
System.out.println(fl.create(13));
}
}
运行结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
获取类类型时,使用了泛型效果
class B{}
class C extends B{}
public class Main{
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//如果不写成泛型,那么生成对象只能用Object
//Class cClass = C.class;
//Object c = cClass.newInstance();
Class cClass = C.class;
C c = cClass.newInstance();
//泛型父类限制
//不能直接写成Class up = cClass.getSuperclass()
Class super C> up = cClass.getSuperclass();
//不能写成 B obj = up.newInstance();
Object obj = up.newInstance();
}
}
利用类类型cast()强转
class B{}
class C extends B{}
public class Main{
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
B b = new C();
Class Ctype = C.class;
C c = Ctype.cast(b); //相当于 C c = (C)b;
}
}