zoukankan      html  css  js  c++  java
  • Android中的实体类的正确用法 [转]

    实体类是Android 开发中经常用到的一个东东,然而今天我才听说、然后了解到原来Android中的实体类的用法不应该和Java中的实体类的用法一个样。

    之前学java基础的时候知道实体类这个东西都是这样的:先建一个类,然后设置几个私有属性,然后通过get和set方法供外界调用,于是到了Android中也是一样这样用,但是其实这样在Android中是不推荐的方式。

    Android中推荐使用的实体类是属性公有,不必通过get和set来调用,而是外界可以直接使用,而且这样写法在速度上会比属性私有的那种通过get和set方式调用的写法快3倍。

    具体的详细说明可以参考官方的介绍说明,这个是原文:

    Avoid Internal Getters/Setters


    In native languages like C++ it's common practice to use getters (i = getCount()) instead of accessing the field directly (i = mCount). This is an excellent habit for C++ and is often practiced in other object oriented languages like C# and Java, because the compiler can usually inline the access, and if you need to restrict or debug field access you can add the code at any time.

    However, this is a bad idea on Android. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

    Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter.

    Note that if you're using ProGuard, you can have the best of both worlds because ProGuard can inline accessors for you.

  • 相关阅读:
    .NetCore 部署到IIS上的问题
    泛型(EF)增删改查
    Ef数据GroupBy多字段查询Vb.net与c#参考
    WEBAPI 最近更新项目时 服务器总是提示:An error has occurred.
    SQL SERVER 语法
    Fonour.AspnetCore 生成SQL SERVER数据库
    Windows10出现打开EXE应用程序错误
    jQuery实现DOM加载方法源码分析
    前端面试高频题:删除数组重复元素的多种方法
    Mac 下使用homebrew 安装node后全局安装找不到问题
  • 原文地址:https://www.cnblogs.com/minlio/p/3651196.html
Copyright © 2011-2022 走看看