In one class, I have to call the constructor of another class, which requires two parameters: IHelloServiceConnectionObserverand ContextWrapper. The problem is that they are both this.
Note. ContextWrapperis a framework class that I do not control ( android.content.ContextWrapper, in fact). My class (Android Activity) is already there-a ContextWrapper, and I want to mix it a bit with IHelloServiceConnectionObserver.
Also note that my class is one of several classes all inherit from ContextWrapper, so the union ContextWrapperand IHelloServiceConnectionObsererwill not work.
I could do this:
HelloServiceConnection svc = HelloServiceConnection(this,this);
challenges
public HelloServiceConnection(IHelloServiceConnectionObserver observer, ContextWrapper contextWrapper){
this.observer = observer;
this.contextWrapper = contextWrapper;
}
But it looks stupid. Or I could do this:
HelloServiceConnection svc = HelloServiceConnection(this);
challenges
public HelloServiceConnection(IHelloServiceConnectionObserver observer){
this.observer = observer;
this.contextWrapper = (ContextWrapper) observer;
}
But now I am moving the beautiful compile-time error to the runtime error.
?
: , , " ", , . , :
helloServiceConnection = HelloServiceConnection.create(this);
public static <T extends ContextWrapper & IHelloServiceConnectionObserver> HelloServiceConnection create(T value){
return new HelloServiceConnection(value, value);
}
, ,
private HelloServiceConnection(IHelloServiceConnectionObserver observer, ContextWrapper contextWrapper){
this.observer = observer;
this.contextWrapper = contextWrapper;
}
, , . ContextWrapper , , . , HelloServiceConnection in, ContextWrapper IHelloServiceConnectionObserver.
, , , , . , , !
, - , .