Some time ago wrote a part of iOS program, where the delegation (delegate)/agent is very powerful. Today, we will write a simple commission program for Android and iOS in the principle of delegation to share the following:
1. Project
1.1
The first step: Create a new Interface Iproxy (the Protocol (Protocol) in iOS is the same, but there's a difference. It's not much to say)
1 Package com.xx; 2 3 Public Interface iproxy {4 string SayHello (string name); 5 }
Step two: New a delegate (proxy class)
1 Packagecom.xx;2 3 Public classAImplementsiproxy{4 5 @Override6 Publicstring SayHello (string name) {7 //TODO auto-generated Method Stub8 return"Hello!" +name;9 }Ten One}
Step three new Class B
1 Packagecom.xx;2 3 Public classBImplementsiproxy{4 Iproxy proxy;5 6 PublicB (Iproxy proxy) {7 Super();8 This. Proxy =proxy;9 }Ten One @Override A Publicstring SayHello (string name) { - //TODO auto-generated Method Stub - returnProxy. SayHello (name); the } - -}
Fourth Step test (differs from iOS but roughly the same)
1 Packagecom.xx;2 3 Public classTest {4 Public Static voidMain (string[] args) {5A A =NewA ();6 7b b =NewB (a);8System.out.println (B.sayhello ("Xiao Hu")));9 }Ten}
Results:
Hope this article is helpful to everyone, welcome to find the problem and everyone to discuss and solve the problem.
Trusted implementations of Android and iOS