`
leonzhx
  • 浏览: 793281 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 53: Prefer interfaces to reflection

阅读更多

1.  Given a Class object, you can obtain Constructor, Method, and Field instances representing the constructors, methods, and fields of the class represented by the Class instance. These objects provide programmatic access to the class’s member names, field types, method signatures, and so on. You can construct instances, invoke methods, and access fields of the underlying class by invoking methods on the Constructor, Method, and Field instances.

 

2.  Reflection allows one class to use another, even if the latter class did not exist when the former was compiled. This power, however, comes at a price:

    1) You lose all the benefits of compile-time type checking, including exception checking.

    2) The code required to perform reflective access is clumsy and verbose.

    3) Performance suffers.

 

3.  As a rule, objects should not be accessed reflectively in normal applications at runtime. There are a few sophisticated applications that require reflection. Examples include class browsers, object inspectors, code analysis tools, and interpretive embedded systems. Reflection is also appropriate for use in remote procedure call (RPC) systems to eliminate the need for stub compilers.

 

4.  For many programs that must use a class that is unavailable at compile time, there exists at compile time an appropriate interface or superclass by which to refer to the class. If this is the case, you can create instances reflectively and access them normally via their interface or superclass. If the appropriate constructor has no parameters, then you don’t even need to use java.lang.reflect; The Class.newInstance method provides the required functionality.

 

5.  If you are writing a program that has to work with classes unknown at compile time, you should, if at all possible, use reflection only to instantiate objects, and access the objects using some interface or superclass that is known at compile time.

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 65: Prefer interfaces to reflection Item 66: Use native methods judiciously Item 67: Optimize judiciously Item 68: Adhere to generally accepted naming conventions 10 Exceptions Item 69: Use ...

    专享:prefer的用法__(全).pdf

    13. “I would prefer to spend the weekend at home rather than driving all the way to your mother’s.”说话者宁愿在家度过周末,而不愿长途驾车去对方母亲家。 14. “I should prefer beef rather than ...

    Effective C#

    **Item 22: Prefer Defining and Implementing Interfaces to Inheritance** - **Reasons:** Interfaces promote loose coupling and make it easier to swap implementations. - **Implementation:** ```csharp ...

    新人教九年级英语unit单词讲解PPT课件.pptx

    4. **特定情况**:`prefer to do sth.` 用于强调在特定情境下,更愿意做某事,例如:"I prefer to walk."(我宁愿走路。) 5. **对比形式**:`prefer doing sth. to doing sth.` 这种结构表示在两个动作之间,更...

    Copy Constructors and Assignment Operators终极解释

    在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...

    effective c++

    2. **Item 7: Prefer Initialization to Assignment** - **核心观点**:提倡初始化而非赋值,尤其是在构造函数和成员变量的初始化过程中。 - **应用场景**:适用于对象创建时的资源管理,有助于减少内存泄漏和提高...

    介词to的用法归纳.pdf

    * prefer to * compare to * contrast to 这些例句中,to表示比较或对比的意思,强调两者之间的差异或相似性。 五、tҏ 与 及 个 别 的 名 词 构 成 比 较 之 意 在这个用法中,to与及个别的名词构成比较之意。...

    安徽省太和县北城中心学校九年级英语全册Unit9IlikemusicthatIcandancetoSectionA1a_2d导学

    3. prefer sth to sth,如:I prefer fish to meat. 或 I prefer reading to playing. 通过以上内容的学习,学生应能理解并运用这些词汇和语法结构,进行关于音乐喜好的交流,同时也能理解和使用定语从句,提升英语...

    九年级英语下册 Unit 6 Topic1 ABCD学案(无答案) 仁爱版

    - 宁愿…也不…:prefer…to… - 儿童节目:children's program - 新闻报道:news report - 体育节目:sports program - 教育节目:educational program - 娱乐节目:entertainment program - 爱上…:fall ...

    ESLint 规则文件

    ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。

    初中要用到的固定搭配.docx

    - prefer to do sth / preferring doing sth.: 更喜欢做某事,如:I prefer to walk / I prefer walking. - begin to do sth / beginning doing sth.: 开始做某事,如:She began to cook / She began cooking. -...

    高考英语重点词组固定搭配大全.docx

    to do sth.:宁愿某人做某事,如:I prefer you to come earlier. 28. request sb. to do sth.:要求某人做某事,如:The client requested us to revise the design. 29. remind sb. to do sth.:提醒某人做某事,...

    schemalint:Lint数据库架构

    public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...

    2013年九年级英语下册 Module 8 On the town动词不定式专项练习 外研版

    解析:"prefer to do... rather than do..." 表示宁愿做...而不愿做...,所以两个位置都应使用动词不定式。 4. — What do you think of tomorrow’s football match? — It’s difficult for us to win the match....

    英语词汇固定搭配.doc

    - 宁愿某人做某事,例如:I prefer you to come early rather than late. 28. request sb. to do sth. - 要求某人做某事,例如:The client requested a meeting to discuss the project. 29. remind sb. to do sth...

    九年级英语上册重点短语默写卷精选.doc

    8. 更愿意做…而不愿意做…:prefer to do…rather than do… 9. 从现在起:from now on 10. 使某人做某事:make sb. do sth. 11. 几个:several 12. 打开、关闭:open / close 13. 由…制成(看得见原料):be made ...

    2013年中考英语 单选题专项练习(第1集)

    7. 题目考察固定搭配:prefer to do sth. rather than do sth. 意为“宁愿做…而不愿做…”,所以B. to stay 是正确答案。 8. 题目考察动词短语的搭配:borrow 意为“借入”,lend 意为“借出”,所以A. borrow, ...

    2015秋九年级英语上册Unit5ArtworldWelcometotheunit导学案无答案新版牛津版

    - 用法包括:prefer A to B(比起B更喜欢A),prefer doing A to doing B,以及prefer that sb. (should) do sth.。 - 造句示例:I prefer reading books to watching TV. 6. **课堂活动**: - 【演练展示】部分...

    山东省龙口市诸由观镇诸由中学九年级英语全册《Unit 4 I like music that I can dance to》练习

    - 更喜欢做:prefer to do - 他最著名的一首曲子:his most famous piece - 中国民族音乐:Chinese folk music - 感觉到了强烈的伤痛:feel a strong pain - 如此… 以至于:so...that... - 查阅;查询:look ...

Global site tag (gtag.js) - Google Analytics