开发者

Why and When we need to create inner class in android

I am new in android world. I am trying to learn it. I have a question about inner classes in android code. Why and when do we need to creat inner class. We can create an individual class and by creating its object we can access its methods. So why do we need to create inner class and when it is necessary to create as inner class.

One advantage which i know is that we cant extend more than one class. So by creating inner classes we can extend more classes. What else ??

/开发者_运维知识库/Shan


I think this really depends on what level of access you want to grant the potentially inner class to the outer class's fields. Also consider what level of access classes (or instances of) outside the outer class (and / or package) need to have to the inner class.

Specific to Android, I think it's more of a convention than a requirement. Sometimes I create inner classes merely for the convenience of avoiding the need to create a new file for 2 or 3 lines of functional code. This may not meet approval from absolutely everyone, but is easy to re-factor if issues come up. There is some good info on the topic at the developer site under Designing for Performance (scroll to the bottom).

Hope this is helpful. Best of luck!


It's just for organization. There's normally not a need for inner classes. It just makes a lot of things easier because you can access the containing class' member variables

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜