Get the App
SLTechnology News&Howtos  ›  Development  › 

A detailed explanation of the example code of the anonymous inner class in java

Shulou Source: shulou.com Published: 2022-06-03 14:05:59 11月14日 Update

This article introduces the relevant knowledge of "detailed explanation of the example code of the java anonymous inner class". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Person.java

Package insof;public class Person extends Object {String name; static int age; public Person () {this.name = "tom"; System.out.println ("the constructor is executed");} public void test () {System.out.println ("test method of Person");}

Main.java

Package insof;public class Main {public static void main (String [] args) {/ / anonymous inner class, which does not have a class name and cannot be displayed by calling the new method to create an object. You can initialize the attribute with the code block Person p = new Person () {{super.name = "jack";} @ Override public void test () {System.out.println ("Anonymous subclass");}} System.out.println (p.name); p.test ();}}

The constructor is executed.

Jack

Anonymous subclass

This is the end of the content of "detailed explanation of the instance code of java anonymous inner class". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Method code subclass instance content more knowledge practicality achievement next dilemma reality object property situation article case class name editor website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Huawei Linux OPPO Reno