在 Android 中我看到这样的代码:
public class Now extends Activity implements View.OnClickListener
这在java中合法吗? View.OnClickListener 在这里到底代表什么。
最佳答案
问题似乎与View.x
的使用有关。正如@Vicente_Plata 在评论中很好地指出的那样:
OnClickListener is an interface declared inside the View class. Like an "inner class" but, rather, an "inner interface
旧答案:
View.OnclickListener 是一个接口(interface)。它定义了您的 Activity 必须实现的方法,在本例中为
OnClickListener()
。任何检查的人都想知道您的类中是否有这样的函数,因此他们可以将 Now
用作 OnclickListener
。
接口(interface)不提供实现。
此外,来自 this page
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
关于java - 在android中实现关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5769036/