An instance of the Java interface passing data

Source: Internet
Author: User

We're going to talk about data changes in Class E, which informs Class A, which is achieved through interface F. The specific principle is that every time the data change of E Let it notify the interface, and Class A inherits the interface, so every time the call interface of E will trigger the data change event of 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 time the data changes the calling interface makes its data change
}

}
}). Start ();
}

Private F F;

public void Setflistener (F ff) {//An instance of the interface, a method that needs to be called for classes that need to get the data for this class.
f = ff;
}

}

Next, create an interface F:

Public interface F {
void OnF (e e, int i); parameter is the data source Class E and the change data
}

Create a second Class A to implement the interface F:

public class A implements f{
private static int S;
private static 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);
}
}

An instance of the Java interface passing data

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.