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.


     

  • 相关阅读:
    Android 打开相册拍照选择多张图片显示
    Mac 打开、编辑 .bash_profile 文件
    Ionic app IOS 在Xcode 模拟运行 真机调试
    android studio 把 ionic 打包时修改应用名称、修改应用图标、修改启动画面,升级打包
    Android studio 运行打包 Ionic 项目
    ionic4 路由跳转、ionic4 路由跳转传值 NavController 返回上一页 、NavController 回到根
    Ionic4.x ion-refresher 下拉更新
    Ionic4.x ion-infinite-scroll 上拉分页加载更多
    Ionic4.x ion-infinite-scroll 上拉分页加载更多
    Ionic4.x Modal模态对话框以及 Modal 传值
  • 原文地址:https://www.cnblogs.com/wucg/p/1986376.html
Copyright © 2011-2022 走看看