developer.android.com 文档中有一篇关于性能的文章,里面提到了内部类的使用。文章建议“对于私有内部类 使用 包訪问权限取代私有权限訪问”,

这里说的是在内部类訪问外部类的成员或方法时假设 内部类是私有的而且外部类的成员也是私有的,那么编译器就会为内部类在外部类中添加一个静态方法。

真的是这种吗?仅仅有试一试才知道。

我们使用一个简单的样例来測试下:

public class One {

	private int a;

	private class B{

		public int getA(){
return a;
}
} }

上边的代码为One创建了一个私有的内部类B,而且B中有一个方法訪问到了 One中的私有成员 a。我们把上边的代码编译成 dex文件后导出程序的指令码与类信息。

导出的结果例如以下:

Processing 'one.dex'...
Opened 'one.dex', DEX version '035'
Class #0 -
Class descriptor : 'LOne$B;'
Access flags : 0x0000 ()
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
#0 : (in LOne$B;)
name : 'this$0'
type : 'LOne;'
access : 0x1010 (FINAL SYNTHETIC)
Direct methods -
#0 : (in LOne$B;)
name : '<init>'
type : '(LOne;)V'
access : 0x10002 (PRIVATE CONSTRUCTOR)
code -
registers : 2
ins : 2
outs : 1
insns size : 6 16-bit code units
0001a0: |[0001a0] One.B.<init>:(LOne;)V
0001b0: 5b01 0000 |0000: iput-object v1, v0, LOne$B;.this$0:LOne; // field@0000
0001b4: 7010 0400 0000 |0002: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0004
0001ba: 0e00 |0005: return-void
catches : (none)
positions :
0x0000 line=7
locals :
0x0000 - 0x0006 reg=0 this LOne$B; Virtual methods -
#0 : (in LOne$B;)
name : 'getA'
type : '()I'
access : 0x0001 (PUBLIC)
code -
registers : 2
ins : 1
outs : 1
insns size : 7 16-bit code units
0001bc: |[0001bc] One.B.getA:()I
0001cc: 5410 0000 |0000: iget-object v0, v1, LOne$B;.this$0:LOne; // field@0000
0001d0: 7110 0300 0000 |0002: invoke-static {v0}, LOne;.access$0:(LOne;)I // method@0003
0001d6: 0a00 |0005: move-result v0
0001d8: 0f00 |0006: return v0
catches : (none)
positions :
0x0000 line=10
locals :
0x0000 - 0x0007 reg=1 this LOne$B; source_file_idx : 10 (One.java) Class #1 -
Class descriptor : 'LOne;'
Access flags : 0x0001 (PUBLIC)
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
#0 : (in LOne;)
name : 'a'
type : 'I'
access : 0x0002 (PRIVATE)
Direct methods -
#0 : (in LOne;)
name : '<init>'
type : '()V'
access : 0x10001 (PUBLIC CONSTRUCTOR)
code -
registers : 1
ins : 1
outs : 1
insns size : 4 16-bit code units
0001dc: |[0001dc] One.<init>:()V
0001ec: 7010 0400 0000 |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0004
0001f2: 0e00 |0003: return-void
catches : (none)
positions :
0x0000 line=3
locals :
0x0000 - 0x0004 reg=0 this LOne; #1 : (in LOne;)
name : 'access$0'
type : '(LOne;)I'
access : 0x1008 (STATIC SYNTHETIC)
code -
registers : 2
ins : 1
outs : 0
insns size : 3 16-bit code units
0001f4: |[0001f4] One.access$0:(LOne;)I
000204: 5210 0100 |0000: iget v0, v1, LOne;.a:I // field@0001
000208: 0f00 |0002: return v0
catches : (none)
positions :
0x0000 line=5
locals : Virtual methods -
source_file_idx : 10 (One.java)

哈看到没 One中多一个方法,而我们的源代码中是没有的。

One.access$0:(LOne;)I

如今我们把源代码改下

public class One {

	private int a;

	class B{

		public int getA(){
return a;
}
} }

然后在编译,导出:

Processing 'one.dex'...
Opened 'one.dex', DEX version '035'
Class #0 -
Class descriptor : 'LOne$B;'
Access flags : 0x0000 ()
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
#0 : (in LOne$B;)
name : 'this$0'
type : 'LOne;'
access : 0x1010 (FINAL SYNTHETIC)
Direct methods -
#0 : (in LOne$B;)
name : '<init>'
type : '(LOne;)V'
access : 0x10000 (CONSTRUCTOR)
code -
registers : 2
ins : 2
outs : 1
insns size : 6 16-bit code units
0001a0: |[0001a0] One.B.<init>:(LOne;)V
0001b0: 5b01 0000 |0000: iput-object v1, v0, LOne$B;.this$0:LOne; // field@0000
0001b4: 7010 0400 0000 |0002: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0004
0001ba: 0e00 |0005: return-void
catches : (none)
positions :
0x0000 line=7
locals :
0x0000 - 0x0006 reg=0 this LOne$B; Virtual methods -
#0 : (in LOne$B;)
name : 'getA'
type : '()I'
access : 0x0001 (PUBLIC)
code -
registers : 2
ins : 1
outs : 1
insns size : 7 16-bit code units
0001bc: |[0001bc] One.B.getA:()I
0001cc: 5410 0000 |0000: iget-object v0, v1, LOne$B;.this$0:LOne; // field@0000
0001d0: 7110 0300 0000 |0002: invoke-static {v0}, LOne;.access$0:(LOne;)I // method@0003
0001d6: 0a00 |0005: move-result v0
0001d8: 0f00 |0006: return v0
catches : (none)
positions :
0x0000 line=10
locals :
0x0000 - 0x0007 reg=1 this LOne$B; source_file_idx : 10 (One.java) Class #1 -
Class descriptor : 'LOne;'
Access flags : 0x0001 (PUBLIC)
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
#0 : (in LOne;)
name : 'a'
type : 'I'
access : 0x0002 (PRIVATE)
Direct methods -
#0 : (in LOne;)
name : '<init>'
type : '()V'
access : 0x10001 (PUBLIC CONSTRUCTOR)
code -
registers : 1
ins : 1
outs : 1
insns size : 4 16-bit code units
0001dc: |[0001dc] One.<init>:()V
0001ec: 7010 0400 0000 |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0004
0001f2: 0e00 |0003: return-void
catches : (none)
positions :
0x0000 line=3
locals :
0x0000 - 0x0004 reg=0 this LOne; #1 : (in LOne;)
name : 'access$0'
type : '(LOne;)I'
access : 0x1008 (STATIC SYNTHETIC)
code -
registers : 2
ins : 1
outs : 0
insns size : 3 16-bit code units
0001f4: |[0001f4] One.access$0:(LOne;)I
000204: 5210 0100 |0000: iget v0, v1, LOne;.a:I // field@0001
000208: 0f00 |0002: return v0
catches : (none)
positions :
0x0000 line=5
locals : Virtual methods -
source_file_idx : 10 (One.java)

依旧有附加的方法

One.access$0:(LOne;)I

我们在改动下

public class One {

	int a;

	class B{

		public int getA(){
return a;
}
} }

在编译 导出

Processing 'one.dex'...
Opened 'one.dex', DEX version '035'
Class #0 -
Class descriptor : 'LOne$B;'
Access flags : 0x0000 ()
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
#0 : (in LOne$B;)
name : 'this$0'
type : 'LOne;'
access : 0x1010 (FINAL SYNTHETIC)
Direct methods -
#0 : (in LOne$B;)
name : '<init>'
type : '(LOne;)V'
access : 0x10000 (CONSTRUCTOR)
code -
registers : 2
ins : 2
outs : 1
insns size : 6 16-bit code units
000184: |[000184] One.B.<init>:(LOne;)V
000194: 5b01 0000 |0000: iput-object v1, v0, LOne$B;.this$0:LOne; // field@0000
000198: 7010 0300 0000 |0002: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0003
00019e: 0e00 |0005: return-void
catches : (none)
positions :
0x0000 line=7
locals :
0x0000 - 0x0006 reg=0 this LOne$B; Virtual methods -
#0 : (in LOne$B;)
name : 'getA'
type : '()I'
access : 0x0001 (PUBLIC)
code -
registers : 2
ins : 1
outs : 0
insns size : 5 16-bit code units
0001a0: |[0001a0] One.B.getA:()I
0001b0: 5410 0000 |0000: iget-object v0, v1, LOne$B;.this$0:LOne; // field@0000
0001b4: 5200 0100 |0002: iget v0, v0, LOne;.a:I // field@0001
0001b8: 0f00 |0004: return v0
catches : (none)
positions :
0x0000 line=10
locals :
0x0000 - 0x0005 reg=1 this LOne$B; source_file_idx : 9 (One.java) Class #1 -
Class descriptor : 'LOne;'
Access flags : 0x0001 (PUBLIC)
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
#0 : (in LOne;)
name : 'a'
type : 'I'
access : 0x0000 ()
Direct methods -
#0 : (in LOne;)
name : '<init>'
type : '()V'
access : 0x10001 (PUBLIC CONSTRUCTOR)
code -
registers : 1
ins : 1
outs : 1
insns size : 4 16-bit code units
0001bc: |[0001bc] One.<init>:()V
0001cc: 7010 0300 0000 |0000: invoke-direct {v0}, Ljava/lang/Object;.<init>:()V // method@0003
0001d2: 0e00 |0003: return-void
catches : (none)
positions :
0x0000 line=3
locals :
0x0000 - 0x0004 reg=0 this LOne; Virtual methods -
source_file_idx : 9 (One.java)

已经没有附加的方法了。

假设外部类的成员是包訪问的,内部类是私有的相同也不会产生附加的方法。

内部类对外部类成员的訪问是不能直接訪问私有成员的,编译器会添加额外的辅助方法,避免的方法是改动外部类的成员为包訪问,文档中也提到了这会在一定程度上破坏

封装。

android 内部类的优化的更多相关文章

  1. Android应用性能优化(转)

    人类大脑与眼睛对一个画面的连贯性感知其实是有一个界限的,譬如我们看电影会觉得画面很自然连贯(帧率为24fps),用手机当然也需要感知屏幕操作的连贯性(尤其是动画过度),所以Android索性就把达到这 ...

  2. 包建强的培训课程(9):Android App性能优化

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  3. Android代码内存优化建议-Android官方篇

    转自:http://androidperformance.com/ http://developer.android.com/intl/zh-cn/training/displaying-bitmap ...

  4. Android开发性能优化总结(一)

    安卓开发应用首先要讲究良好的用户体验,如果一款软件卡顿现象严重,不流畅,经常崩溃,那么将给用户带来极不良好的体验,从而损失用户. 在实际开发和学习中,我总结了一下关于安卓性能的优化,供大家参考交流. ...

  5. fir.im Weekly - 如何进行 Android App 性能优化

    关于 Android App 的优化,@anly-jun 用 3 个月完成了这一系列文章,从 性能分析工具 到 ANR .Layout .消除卡顿 到 内存优化.内存分析工具大概十五六篇,并对此做一个 ...

  6. Android客户端性能优化(魅族资深工程师毫无保留奉献)

    本文由魅族科技有限公司资深Android开发工程师degao(嵌入式企鹅圈原创团队成员)撰写,是degao在嵌入式企鹅圈发表的第一篇原创文章,毫无保留地总结分享其在领导魅族多个项目开发中的Androi ...

  7. Android UI性能优化实战, 识别View中的性能问题

    出自:[张鸿洋的博客]来源:http://blog.csdn.net/lmj623565791/article/details/45556391 1.概述 2015年初google发布了Android ...

  8. 转 iOS和android游戏纹理优化和内存优化(cocos2d-x)

    iOS和android游戏纹理优化和内存优化(cocos2d-x) (未完成) 1.2d游戏最占内存的无疑是图片资源. 2.cocos2d-x不同平台读取纹理的机制不同.ios下面使用CGImage, ...

  9. android app性能优化大汇总(内存性能优化)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上 ...

随机推荐

  1. 手工安装kubernetes

    参考的URL是 http://www.cnblogs.com/zhenyuyaodidiao/p/6500830.html 安装kubernets本身比较顺利,只是作dashboard时,老是日文版, ...

  2. HTTP 协议基本知识

    HTTP协议    7.1.什么是HTTP协议:        HTTP协议是用来规定浏览器客户端和服务器通信的方式 7.2.基本原则        基于请求响应模型        一次请求对应一次响 ...

  3. golang笔记:cookie

    在同一个问题上栽了两次,以后碰到cookie出问题多半都是因为这个. Request.Cookie(name)取Cookie的时候,返回值只有name和value cookie.go cookies ...

  4. 洛谷——P1327 数列排序

    P1327 数列排序 题目描述 给定一个数列{an},这个数列满足ai≠aj(i≠j),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? 输入输出格式 输入格式: ...

  5. 谜题15:令人晕头转向的Hello

    下面的程序是对一个老生常谈的例子做出了稍许的变化之后的版本.那么,它会打印出什么呢? /** * Generated by the IBM IDL-to-Java compiler, version ...

  6. POJ 2186 Popular Cows(强连通分量)

    [题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, ...

  7. 【分块】hdu5057 Argestes and Sequence

    分块,v[i][j][k]表示第i块内第j位是k的元素数.非常好写.注意初始化 要注意题意,①第i位是从右往左算的. ②若x没有第i位,则用前导零补齐10位.比如103---->00000001 ...

  8. 【可持久化Trie】【set】bzoj3166 [Heoi2013]Alo

    枚举每个数,计算以其为次大数的最大区间,显然,只需要用这个区间的答案 对 答案进行更新即可. 找到每个数右侧.左侧第1.2个比它大的数,然后分类讨论一下即可. 找到的过程中把数sort以后,从大到小把 ...

  9. 使用GIT时排除NuGet的packages文件夹

    这段时间一直在用GIT做本地自己写的程序的源码管理工具,在使用的过程中发现了一个问题:Git往往会把NuGet的packages文件夹作为项目的一部分给添加进来了.网上搜了一下,原因是GIT只是和文件 ...

  10. UITextField 如何设置为密码方式显示?

    UITextField 怎么设置成为一个 *号密码框 呢? 可以在 Interface Builder 里面直接设置吗? Attributes inspector 中 Text Field 下选中 S ...