java - 怎样才算是一个内部类
阿神
阿神 2017-04-18 10:47:59
0
2
571

请问下怎样才算是一个内部类,最近在看android的Handler方面的东西,有点被搞晕了,一般情况下的内部类我是明白的,但是为什么Handler也算是Activity的一个内部类。

public class MainActivity extends Activity{
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };   
}

Handler并已经是一个被定义好的类,只不过在使用的时候一般回去重写其中的handleMessage方法,这样就算是一个内部类了吗?

这里主要是想请问下:

  1. 内部类的定义是什么?

  2. 编译器是怎么去判断一个类是内部类的

阿神
阿神

闭关修行中......

reply all(2)
左手右手慢动作

If you know the general inner classes, then you should know this inner class, because it is the most basic.

According to the official definition of the Java language, inner classes are defined like this:

A nested class is a member of its enclosing class.

If a class is a member of another class, it is an inner class.
Inner classes are divided into static inner classes and non-static inner classes

The above handler is a non-static inner class. Unlike static inner classes, to initialize an instance of such an inner class, there must first be an instance of its outer class.
In such a non-static inner class, you can refer to an instance of the outer class through MainActivity.this. Static inner classes are not allowed.

As long as the compiler recognizes that a class is defined inside another class, it will know that it is an internal class.

大家讲道理

This is called an anonymous inner class

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!