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.


     

  • 相关阅读:
    左耳听风笔记之一
    富爸爸穷爸爸 -- 笔记
    Aruba无线控制器常用操作
    接入交换机办公网常用配置
    核心交换机办公网常用配置
    FortiGate防火墙办公网常用配置
    去掉深信服上网认证页面里的“修改密码”
    深信服上网行为管理短信认证多用户登录问题
    深信服上网行为管理配置跨三层MAC识别
    深信服上网行为管理实现一次认证成功之后连续3天无流量通过才再次认证
  • 原文地址:https://www.cnblogs.com/wucg/p/1986376.html
Copyright © 2011-2022 走看看