zoukankan      html  css  js  c++  java
  • 运行时类型识别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());
    
        }
    
    }
  • 相关阅读:
    C++隐式推导-auto关键词
    git的几种实用操作(合并代码与暂存复原代码)
    CPU的后记,程序员的未来之计
    CPU中的程序是怎么运行起来的
    nginx-日志切割
    Linux里面使用navicat连接MySQL数据显示2002-Can't connect to local MysQL serverthrough socket'/var/lib/mysq/mysql.sock'(13"权限不够")
    Cacti图形中的方框乱码解决办法
    Cacti1.2.14最新版安装和配置(详细版)
    ERROR 1419 (HY000) at line 9: You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
    Linux服务器查看对应网卡的网口
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/4236674.html
Copyright © 2011-2022 走看看