What instantiation is called in Java?

This may be a very basic problem for Java, but I don't remember it and don't know how to search it online

button.setOnClickListener(new OnClickListener()
{
  @Override
   public void onClick(View v)
   {
     Thread.sleep(100);
   }
}

I'm used to initializing objects with new constructors such as onclicklistener (arg1, arg2,...) In my own experience, I never override methods when constructing objects I only do this when I expand the course If there are any terms, what is the instantiation called in Java? Should we use it in other cases?

Solution

This is an anonymous course It is an implementation of the onclicklistener interface, but it is unnamed The writing is concise and the implementation is visible in the call context (usually useful for readability), but because it is implemented in place, it cannot be used elsewhere

Also note that it is an inner class and therefore references its surrounding classes

For details, see here

Reply A reference to the surrounding classes The inner class has an implicit reference to its outer class (you can see this if you try to serialize the inner class using xStream. It will pull it out with the outer class) If there is a variable in the external class, you can reference it internally (assuming it is the final variable, remember)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>