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 implement anonymous inner classes in Java2

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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!

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

Development

Wechat

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

12
Report