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.


     

  • 相关阅读:
    伴郎
    MySQL出现Waiting for table metadata lock的场景浅析
    相同name,取最小的id的值,mysql根据相同字段 更新其它字段
    Sequence contains no elements
    Check if List<Int32> values are consecutive
    comparison of truncate vs delete in mysql/sqlserver
    Are query string keys case sensitive?浏览器种输入url附带的参数是否区分大小写
    Understanding Action Filters (C#) 可以用来做权限检查
    糖果缤纷乐攻略
    DNGuard HVM Unpacker(3.71 trial support and x64 fixed)
  • 原文地址:https://www.cnblogs.com/wucg/p/1986376.html
Copyright © 2011-2022 走看看