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.


     

  • 相关阅读:
    ASP.net MVC 构建layui管理后台(构造基础仓储)<1>
    ASP.net MVC 构建layui管理后台(整体效果)
    搭建私有 Nuget 服务器教程(1)
    SSAS数据集Cube不存在或者尚未处理
    浅谈MDX处理空值NULL及格式化结果
    Zoey.Dapper--Dapper扩展之把SQL语句放到文件中
    Polly每次重试执行不同的操作
    Exceptionless应用--自定义插件
    Fiddler开启Https的时候出现unable to configure windows to trust Fiddler Root certificate问题
    ASP.NET Core 中的配置
  • 原文地址:https://www.cnblogs.com/wucg/p/1986376.html
Copyright © 2011-2022 走看看