学习java和android还是要多看英文文档,翻译过来的东西有时候经常会看的迷茫,产生理解偏差,今天看异步消息源码的时候对几个类名产生了疑惑,相信很多人对成员类,嵌套类,内部类静态内部类的概念还没有真正搞清楚。
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
/**
* Tests whether the class represented by this {@code Class} is a member
* class.
*/
public boolean isMemberClass() {
return getDeclaringClass() != null;
}
/**
* Returns the declaring {@code Class} of this {@code Class}. Returns
* {@code null} if the class is not a member of another class or if this
* {@code Class} represents an array class, a primitive type, or void.
*/
public native Class<?> getDeclaringClass();
member class有些人会想当然的认为是内部类的意思,内部类的真实英文名字其实是inner class,member class就是成员类的意思,范围比内部类大一点,member class包括inner class和static nest class(静态嵌套类,不持有外部类的引用,行为接近普通类)类的成员类就是member class,这里指的是范围(scope),跟inner形容的不是太一个范畴。
很明显有的local class(方法内定义的类)是内部类不是成员类(member class),
所有的内部类,Local内部类,匿名内部类都可以直接访问外面的封装类的实例变量和方法。而静态嵌套类则不能。内部类,Local内部类,匿名内部类的实例都持有一个外部封装类实例的隐式引用;java对象序列化要求对象里所有的对象成员都实现序列化接口,如果只对内部类实现序列化,而外部封装类没有实现序列化接口,就会在对内部类进行序列化的时候报出异常(他持有外部类实例的隐式调用)。