Java interface Pass instance of data

Source: Internet
Author: User

We want to talk about the data changes in class E notification Class A, which is implemented through interface F. The principle is that every data change of E makes it notify the interface; Class A inherits the interface, so every time the call interface of E triggers the triggering of the data Change event for Class A.

First create a class E:

public class E {
Private E context;

private static int i = 0;

E () {
context = this;
}

public void Start () {
New Thread (New Runnable () {
public void Run () {
while (1!= 0) {
i = i + 1;

F.onf (context, I);//Each data change call interface let its data change
}

}
). Start ();
}

Private F F;

The instance of the public void Setflistener (F ff) {//interface requires a method called by the class that needs to get the data for this class.
f = ff;
}

}

Second, create an interface F:

Public interface F {
void Onf (e e, int i); Parameters are data source Class E and change data
}

Create a Class A again to implement the interface F:

public class A implements f{
private static int S;
private static A A;

@Override
public void onf (e e, int i) {
s = i;
System.out.println (s);
}

public static void Main (String args[]) {
E e = new E ();
A = new A ();
E.start ();
E.setflistener (a);
}
}

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.