Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use anonymousinnerclass

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces "how to use anonymousinnerclass" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use anonymousinnerclass" article can help you solve your doubts.

Can anonymousinnerclass (anonymous inner class) extends (inherit) other classes and implements (implement) interface (interface)?

Anonymous inner class must be constructed with the help of an interface or an abstract class or an ordinary class. From this level, anonymous inner class implements the interface or inherits the class, but it cannot inherit the class or implement the interface through extends or implement keywords.

The following points are explained:

Because the anonymous inner class has no name, it does not have a constructor. Because there is no constructor, it must be instantiated entirely from the constructor of the parent class, and the anonymous inner class completely leaves the task of creating the object to the parent class.

Second, it doesn't make much sense to create a new method in an anonymous inner class, but it can achieve a magical effect by overriding the parent class, as shown in the example above. This is the embodiment of polymorphism.

Third, because the anonymous inner class does not have a name, the downward cast cannot be performed, and the variable type that holds a reference to an anonymous inner class object must be its direct or indirect parent class type.

Note that anonymous inner classes are declared at compile time and instantiated at run time. This means that a new statement in the for loop creates several instances of the same anonymous class, rather than one instance of several different anonymous classes.

Class Car {

Void move () {}

}

Interface Person {

Void learn ()

}

Abstract Class Animal {

Abstract void eat ()

}

Public class AnonymousInnerClassDemo {

Public static void main (String [] args) {

Car car = new Car () {

Void move () {

System.out.println ("move () method of anonymous inner class")

}

}

Car.move ()

Person person = new Person () {

Void learn () {

System.out.println ("learn () method of anonymous inner class")

}

}

Person.learn ()

Aninal animal = new Animal () {

Void eat () {

System.out.println ("eat () method of anonymous inner class")

}

}

}

} after reading this, the article "how to use anonymousinnerclass" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report