Example Analysis of Anonymous Internal Class quota in Java object-oriented
This article mainly introduces the example analysis of anonymous internal classes in Java object-oriented. It is very detailed and has a certain reference value. Interested friends must read it!
/ * * 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.When the motorcycle Person {touch (Moto moto)} 4.3alarm sounds, Alarm {beep ()} * / public class TestMoto {public static void main (String [] args) {/ / prepare to produce a southern motorcycle Moto moto = new Moto ("South") / / A small power alarm moto.load (new SmallAlarm ()) was installed on the motorcycle; / / at this time Li Ming came over Person p = new Person ("Li Ming"); / / Li Ming accidentally ran into the motorcycle, and the motorcycle issued an alarm sound p.touch (moto). }} interface Alarm {/ / alarm can sound public void beep ();} class SmallAlarm implements Alarm {/ / alarm can sound public void beep () {System.out.println ("someone has touched me and may steal a motorcycle");}} 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 touches the motorcycle, and the alarm on the motorcycle sounds public void touch (Moto moto) {moto.getAlarm (). Beep () }} these are all the contents of the article "sample Analysis of Anonymous Internal classes in Java object-oriented". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!