当前位置: 动力学知识库 > 问答 > 编程问答 >

android - what is need of declare Listener inside java class

问题描述:

what is need of declare Listener inside java class ? I have class ClassDemo.java if i declare interface inside class rather that out side of class then what its impact,What are pros and cons of it?

网友答案:

To encapsulate the behaviour in generic and reusable way.So, basically, your interface visibility will be limited. Interface defines the methods but does not implement it. But the class which implements the listener(interface) will implement all the methods of that interface.

Now suppose I want to pass some result from class A to class B. This will become easier if we execute interface kind of methodology. So when the result is calculated by class A, it will update it to interface inside it and now as class B implements the interface , the result will be automatically given to class B.

网友答案:

If you need to pass something to your listener, it's better to create it in another class so you can pass the parameters to the costructor.

Making it on you main class is only more readable and handy if you don't want a lot of classes

分享给朋友:
您可能感兴趣的文章:
随机阅读: