Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement anonymous inner classes in Java2

Shulou Source: shulou.com Published: 2022-06-02 04:45:12 09月26日 Update

This article is about how to implement anonymous inner classes in Java2. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

/ * * knowledge: anonymous Internal topic: there is an alarm on the motorcycle. When someone touches the motorcycle, the alarm will sound. Abstract class: 1.1motorcycle (Moto), alarm (Alarm), human (Person) 2. Find out the class relationship: 2.1Motorcycle equipped with alarm Alarm-> Moto (1 to 1) 2.2.2.The alarm has different brand classification Alarm 3. Find out the class attributes: 3.1 Moto (brand, alarm reference) 3.2 Alarm (brand) 3.3 Person (name) 4. Moto {load (Alarm alarm)} 4.2.The motorcycle Person {touch (Moto moto)} 4.3.The alarm sounds Alarm {beep ()} * / public class TestMoto2 {public static void main (String [] args) { / / prepare to produce a Southern Motorcycle Moto moto = new Moto ("South") / / A small power alarm Alarm alarm = new Alarm () {public void beep () {System.out.println ("someone stole the car"); moto.load (alarm) is pre-installed on the motorcycle. / prepare to produce a Jialing motorcycle Moto moto2 = new Moto ("Jialing"); moto2.load (alarm); / / at this time Li Ming came over Person p = new Person ("Li Ming") / / Li Ming accidentally touched the motorcycle, and the motorcycle sounded the alarm sound p.touch (moto); p.touch (moto2);}} interface Alarm {/ / the alarm can sound public void beep ();} class Moto {private String brand;// brand private Alarm alarm / / alarm reference public Moto (String brand) {this.brand = brand;} public Alarm getAlarm () {return this.alarm;} / / load alarm public void load (Alarm alarm) {this.alarm = alarm;}} class Person {private String name / / name public Person (String name) {this.name = name;} / / someone touched the motorcycle, and the alarm on the motorcycle sounded public void touch (Moto moto) {moto.getAlarm (). Beep ();}} Thank you for reading! This is the end of the article on "how to implement anonymous inner classes in Java2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Motorcycle motorcycle alarm brand top sound Li Ming Zhongshi content name more knowledge article Jialing preparation production different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Xiaomi Shulou Technology MySQL