How to require two types of parameters when passing to a method in Java

In one class, I have to call the constructor of another class that requires two parameters, ihelloserviceconnectionobserver and contextwrapper The problem is that they are all like this

Note: contextwrapper is a framework class beyond my control (actually Android. Content. Contextwrapper) My class (an Android activity) is already a contextwrapper. I want to mix it with ihelloserviceconnectionobserverness

Also note that my class is one of several classes that inherit from contextwrapper, so the combination of contextwrapper and ihelloserviceconnectionobserver will not work

I can do this:

HelloServiceConnection svc = HelloServiceConnection(this,this);

Telephone

public HelloServiceConnection(IHelloServiceConnectionObserver observer,Contextwrapper contextwrapper){
    this.observer = observer;
    this.contextwrapper = contextwrapper;
}

But it looks silly Or I can do this:

HelloServiceConnection svc = HelloServiceConnection(this);

Telephone

public HelloServiceConnection(IHelloServiceConnectionObserver observer){
    this.observer = observer;
    this.contextwrapper = (Contextwrapper) observer;
}

But now I move a good compile - time error to a run - time error

What are the best practices here?

Editor: Well, I can't say it's a "best practice", but Jon skeet has the right answer for my special case This is what the code ultimately looks like:

helloServiceConnection = HelloServiceConnection.create(this);

Telephone

public static <T extends Contextwrapper & IHelloServiceConnectionObserver> HelloServiceConnection create(T value){
    return new HelloServiceConnection(value,value);
}

Call back

private HelloServiceConnection(IHelloServiceConnectionObserver observer,Contextwrapper contextwrapper){
    this.observer = observer;
    this.contextwrapper = contextwrapper;
}

So let me explain more why this is the correct answer to this particular case Because the context wrapper is part of a framework beyond my control, I can't change it Because it is also the ancestor of several classes, any of which I may want to use helloserviceconnection, it makes no sense to extend all descendants of contextwrapper to add it in ihelloserviceconnectionobserver

So I thought I had no choice when I left, only this, this idol Once I understand Jon's answer, I can save a day!

Thank you, Jon – and all those involved

Solution

Well, you can make the call generic:

public <T extends Contextwrapper & IHelloServiceConnectionObserver> void method
    (T item)

Then let type inference sort them Although it is not very pleasant

Edit: it looks like you're trying to use a constructor, which will make it more difficult You can create instances using static methods:

public static <T extends Contextwrapper & IHelloServiceConnectionObserver>
    HelloServiceConnection createConnection(T value)
{
    return new HelloServiceConnection(value,value);
}

private HelloServiceConnection(Contextwrapper wrapper,IHelloServiceConnectionObserver observer)
{
    this.wrapper = wrapper;
    this.observer = observer;
}

OK, so the constructor and the type itself will eventually have two separate fields - but we know they both reference the same object You can even assert in the constructor if you like

As others have said, it's worth considering whether you really need this coupling What happens if the wrapper and the observer are not the same object?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>