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