Examples of Java events

Source: Internet
Author: User
Tags event listener

Words don't say much, on the code:
Package Test;import Java.util.eventlistener;import Java.util.eventobject;import java.util.list;import    Java.util.vector;class Eventclassone extends eventobject{public eventclassone (Object source) {super (source);    }}class eventsource{public String name;    Public EventSource (String name) {this.name=name;        }//Store the event listener's basket vector listeners=new vector ();//Add the event listener to the basket public void AddEventListener (EventListener e) {    Listeners.add (e); }//action to trigger event public void say (String s) {for (int i = 0; I <listeners.size (); i++) {((M        Yeventlistener) Listeners.elementat (i)). Action (new Eventclassone (this)); }}}class MyEventListener implements EventListener {public void action (EventObject eventobject) {if (EVENTOBJ            Ect.getsource () instanceof EventSource) {String name = ((EventSource) Eventobject.getsource ()). Name;        System.out.println (name+ ", shout what shout, noisy to others have sex"); }}}pUblic class Eventtest {public static void main (string[] args) {EventSource Xiao Feng =new EventSource ("Xiao Feng");        EventSource giant pillar =new EventSource ("giant Pillar");        MyEventListener listener1=new MyEventListener ();        MyEventListener listener2=new MyEventListener ();        Xiao Feng. AddEventListener (Listener1);        Xiao Feng. AddEventListener (Listener2);        Giant pillar. AddEventListener (Listener1);        Xiao Feng Say ("Want a Macho Man");    Giant pillar. Say ("Want a Beautiful Woman"); }}
Results:

"C:\Program Files\java\jdk1.8.0_171\bin\java.exe"
Xiao-Feng, shout what shout, noisy to somebody else to have sex
Xiao-Feng, shout what shout, noisy to somebody else to have sex
Big pillar, shouting what shout, noisy to somebody else to have sex
Process finished with exit code 0

Schematic diagram:

Examples of Java events

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.