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);
}
}