运行时类型识别RTTI

1.RTTI的工作原理

例1. 用Class加载对象示例。

package RTTI;

public class Candy {
    static{
        System.out.println("Loading Candy in static block.");
    }
    public static void main(String[] args) {
        System.out.println("Loading Candy in main method.");

    }

}
package RTTI;

public class loadClass {

    public static void main(String[] args) {
        System.out.println("Before loading Candy.");
        try {
            Class.forName("Candy");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

2. 使用getClass()方法获取类信息

例2. getClass()方法使用示例。

package RTTI;

public class Shape {
    void showMsg(){
        System.out.println("This is Shape class.");
    }
}
package RTTI;

public class Circle extends Shape {
    void showMsg(){
        System.out.println("This is Circle class.");
    }
}
package RTTI;

public class getClassName {

    public static void showName(Shape shape){
        Class c1 = shape.getClass();
        System.out.println(c1.getName());
        if(c1.getName().equals("RTTI.Shape"))
            System.out.println("This is a shape object.");
        else if(c1.getName().equals("RTTI.Circle"))
            System.out.println("This is a circle object.");
    }
    public static void main(String[] args) {
        showName(new Circle());
        showName(new Shape());

    }

}

程序的输出结果为:

RTTI.Circle
This is a circle object.
RTTI.Shape
This is a shape object.

3. 使用类标记

java提供了一种简便生成Class对象的方法:类标记。如果T是任意的java类型,那么,T.class就代表匹配的类对象。例如:

Class c1 = int.class;
Class c2 = double[].class;
Class c3 = Shape.class;

例3. 类标记使用示例

package RTTI;

public class getClassName {

    public static void showName(Shape shape){
        Class c1 = shape.getClass();
        System.out.println(c1.getName());
//        if(c1.getName().equals("RTTI.Shape"))
        if(c1==Shape.class)
            System.out.println("This is a shape object.");
//        else if(c1.getName().equals("RTTI.Circle"))
        else if(c1==Circle.class)
            System.out.println("This is a circle object.");
    }
    public static void main(String[] args) {
        showName(new Circle());
        showName(new Shape());

    }

}

4. 使用关键字instanceof判断所属类

java提供了一个关键字instanceof,用于帮助程序员判断一个对象真正所属的类。它是一个二元运算符,一般形式如下:

objectName instanceof className

计算结果为true或false。

例4. 使用instanceof判断所属类。

package RTTI;

public class getClassName {

    public static void showName(Shape shape){
        Class c1 = shape.getClass();
        System.out.println(c1.getName());
//        if(c1.getName().equals("RTTI.Shape"))
//        if(c1==Shape.class)
        if(shape instanceof Circle)
            System.out.println("This is a circle object.");
//        else if(c1.getName().equals("RTTI.Circle"))
//        else if(c1==Circle.class)
        else if(shape instanceof Shape)
            System.out.println("This is a shape object.");
    }
    public static void main(String[] args) {
        showName(new Circle());
        showName(new Shape());

    }

}
原文地址:https://www.cnblogs.com/gaopeng527/p/4236674.html