zoukankan      html  css  js  c++  java
  • java RTTI

    Anytime you want to use type information at runtime, you must first get a reference
    to the appropriate Class object. Class.forName() is one convenient way to do this,
    because you don't need an object of that type in order to get the Class reference.
    However, if you already have an object of the type you're interested in, you can fetch
    the Class reference byb calling a method that's part of the Object root class:getClass().

    primitive type Class reference
    boolean.class = Boolean.TYPE
    char.class = Character.TYPE
    ...
    In Java SE5, Class<?> is preferred over plain Class, even though they are equivalent
    and the plain Class, as you saw, donesn't produce a compiler warning. The benefit of
    Class<?> is that it indicates that you aren't just using a non-specific class reference by

    accident, or out of ignorance. You chose the non-specfic version.
    Class<? extends Object> className=...;

    Reflection : runtime class information
    Reflection is different from RTTI. The type must be know at compile time in order for
    you to detect it using RTTI and to do something useful with the information. Put another
    way, the compiler must know about all the classes you're working with.
    This doesn't seem like that much of a limitatoin at first, but suppose you're given a
    reference to an object that's not in your program space. In fact, the class of isn't
    even availabble to your program at compile time.
    for example:
    1. in Component-based programming, in which you build projects using Rapid Application
    Development(RAD),Reflection provides the mechanism to detect the available methods and
    produce the method names.
    2. another compelling motivation for discovering class information at run time is to

    provide the ability to create and execute obbjects on remote platforms across a network.
    This is called Remote Method Invocation(RMI),and it allows a Java program to have objects
    distributed across many machines.

    java.lang.reflect library which contains the classes Field, Mehtod, and Constructor
    (each of which implements the Member interface).

    A class method extractor
    reflect helps to create more dynamic code. Reflection is in the language to support
    other java features, such as object serialization and JavaBeans(both covered later in
    the book). However, there are times when it's quite useful to dynamically extract
    infomation about a class.


     

  • 相关阅读:
    GIT操作命令
    flask中logger日志的使用
    Flask项目总结学习
    kubernetes容器网络
    Chrome浏览器离线安装包下载
    《Linux防火墙配置》
    Redhat7.4配置本地yum源
    zabbix5利用python脚本发报警邮件和趋势图
    screen命令
    MX记录
  • 原文地址:https://www.cnblogs.com/wucg/p/1986376.html
Copyright © 2011-2022 走看看