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

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.

, , , , . , , !

, - , .

+3
7

, :

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

. , .

EDIT: , , . :

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

, , , . , , .

, , . , ?

+5

, ContextWrapper , ?

method((ContextWrapper)this, (IHelloServiceConnectionHelper)this)

, .

+1

, ContextWrapper extend IHelloServiceConnectionObserver. , new HelloServiceConnection(this, this) . ?

, , ContextWrapper, IHelloServiceConnectionObserver. ( , ), . , HelloServiceConnection , this.

, . , ( this) / ContextWrapper, IHelloServiceConnectionObserver, -, . , , .

+1

:

IHelloServiceConnectionObserver observer = this;
ContextWrapper contextWrapper = this;
HelloServiceConnection svc = new HelloServiceConnection(observer, contextWrapper);

, this a IHelloServiceConnectionObserver, a ContextWrapper.

, , . , . "has-a" "is-a". - Java " " .

0

:

HelloServiceConnection svc = HelloServiceConnection ((IHelloServiceConnectionObserver) , ( ContextWrapper) );

, , !

0

Another (not nice IMO) solution would be to create an interface Combinedthat extends your interfaces IHelloServiceConnectionObserverand ContextWrapperchanges the class thisto implementation ICombinedand changes HelloServiceConnectionto take one type argument ICombined.

0
source

It works?

Conceptually declares a generic type Combinedthat creates objects HelloServiceConnectionusing itself as an observer observer and context.

class ContextWrapper {}

interface IHelloServiceConnectionObserver {}

class HelloServiceConnection
{
    HelloServiceConnection(ContextWrapper cw, IHelloServiceConnectionObserver o)
    { 
        System.out.println("HelloServiceConnection(" + cw + " ," + o + ")");
    }
}

abstract class Combined<T> extends ContextWrapper
        implements IHelloServiceConnectionObserver
{
    public HelloServiceConnection svc()
    {
        return new HelloServiceConnection(this, this);
    }
}

class MyClass extends Combined<MyClass>
{
    public static void main(String args[])
    {
        MyClass mc = new MyClass();
        System.out.println(mc);
        mc.svc();
    }
}
0
source

Source: https://habr.com/ru/post/1794638/


All Articles