Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,当传递给方法时,如何要求参数具有两种类型_Java - Fatal编程技术网

在Java中,当传递给方法时,如何要求参数具有两种类型

在Java中,当传递给方法时,如何要求参数具有两种类型,java,Java,在一个类中,我必须调用另一个类的构造函数,该类需要两个参数,IHelloServiceConnectionObserver和ContextWrapper。问题是它们都是这个 注意:ContextWrapper是一个我无法控制的框架类(android.content.ContextWrapper)。我的类(安卓活动)已经是一个上下文包装器,我想加入一点IHelloServiceConnectionObserver 还要注意的是,我的类是所有继承自ContextWrapper的几个类之一,因此将Co

在一个类中,我必须调用另一个类的构造函数,该类需要两个参数,
IHelloServiceConnectionObserver
ContextWrapper
。问题是它们都是
这个

注意:
ContextWrapper
是一个我无法控制的框架类(
android.content.ContextWrapper
)。我的类(安卓
活动
)已经是一个
上下文包装器
,我想加入一点
IHelloServiceConnectionObserver

还要注意的是,我的类是所有继承自
ContextWrapper
的几个类之一,因此将
ContextWrapper
iHelloServiceConnectionObser
组合起来是行不通的

我可以这样做:

HelloServiceConnection svc = HelloServiceConnection(this,this);
HelloServiceConnection svc = HelloServiceConnection(this);
召唤

但这看起来很傻。或者我可以这样做:

HelloServiceConnection svc = HelloServiceConnection(this,this);
HelloServiceConnection svc = HelloServiceConnection(this);
召唤

但是现在我把一个不错的编译时错误移到了运行时错误

这里的最佳实践是什么

编辑:嗯,我不能说这是一个“最佳实践”,但对于我的特殊情况,乔恩·斯基特有正确的答案。下面是代码最终的样子:

helloServiceConnection = HelloServiceConnection.create(this);
召唤

因此,让我再解释一下为什么这是针对这种特殊情况的正确答案。因为
ContextWrapper
是我无法控制的框架的一部分,所以我无法更改它。因为它也是几个类的祖先,我可能想在中使用
HelloServiceConnection
,所以将
ContextWrapper
的所有decentant扩展到
IHelloServiceConnectionObserver
中是没有意义的

所以我想我别无选择,只有
this,this
idom。乔恩的回答,一旦我明白了,就省去了这一天

谢谢你,乔恩——也谢谢所有参与的人。

好吧,你可以打普通电话:

public <T extends ContextWrapper & IHelloServiceConnectionObserver> void method
    (T item)
公共作废方法
(T项目)
让类型推断来解决它。不过这并不十分令人愉快

编辑:看起来您正在尝试使用构造函数,这将使其更加困难。您可以使用静态方法创建实例,但:

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;
}
公共静态
HelloServiceConnection createConnection(T值)
{
返回新的HelloService连接(值,值);
}
私有HelloService连接(ContextWrapper,
iHelloService连接观察者(观察者)
{
this.wrapper=包装器;
this.observer=观察者;
}
好的,构造函数和类型本身将以两个单独的字段结束-但是我们知道它们都引用同一个对象。如果愿意,您甚至可以在构造函数中断言


正如其他人所说,值得考虑的是,你是否真的,真的需要这种耦合。如果包装器和观察者不是同一个对象,会发生什么不好的事情?

如果您想强调对象具有ContextWrapper类型并实现接口,那么可以在方法调用中强制转换它

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

强制转换显然是多余的,但它使您的观点更加明确。

我认为您应该继续使用当前的设计,或者使用
ContextWrapper
extend
IHelloServiceConnectionObserver
。您将两者合并的理由是
新的HelloService连接(this,this)
看起来很尴尬。但为什么会出现这种情况

这是因为您恰好有一个对象,它既是
ContextWrapper
又是
IHelloServiceConnectionObserver
。如果它们总是在一起(这是通过将两个构造函数参数合并在一起而强加给程序员的),那么让一个类型扩展另一个类型。或者,让
HelloServiceConnection
获取一个参数,该参数是
的任何类型


如果它们应该分开,那么您当前的代码就可以了。或者,您可以让当前类(this
的类型)扩展/实现
ContextWrapper
IHelloServiceConnectionObserver
,而不是让它只扩展/实现其中一个,并让一个成员变量指向另一个的实例。请记住,当有疑问时,.

我不会害怕编写以下代码:

IHelloServiceConnectionObserver observer = this;
ContextWrapper contextWrapper = this;
HelloServiceConnection svc = new HelloServiceConnection(observer, contextWrapper);
但事实上,我会重新考虑让
this
的类既是
IHelloServiceConnectionObserver
又是
ContextWrapper

只要可能,我的类要么只扩展一个类,要么只实现一个接口。我喜欢这种方法所提供的清晰和分离。这种哲学倾向于“有-有”关系,而不是“是-有”关系。Java中的内部成员类使“每个类一个继承”的方法非常实用。

理想情况下,您可以这样做:

HelloService连接svc= HelloServiceConnection(IHelloServiceConnectionObserver)this,(ContextWrapper)this)

和你以前试过的一样,但更详细

另一个(IMO)解决方案是创建一个接口
组合
,扩展您的
IHelloServiceConnectionObserver
ContextWrapper
接口,更改
this
的类以实现
i组合
,并将
HelloServiceConnection
更改为接受类型为
i组合的单个参数

这是否有效

从概念上讲,它声明了一个泛型类型
Combined
,该类型使用对象本身作为观察者和上下文包装器来创建
HelloServiceConnection
对象

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();
    }
}
class ContextWrapper{}
接口IHelloServiceConnectionObserver{}
类HelloService连接
{
HelloService连接(ContextWrapper cw、iHelloService连接观察服务器o)
{ 
System.out.println(“HelloService连接(“+cw+”,“+o+”));
}
}
抽象类组合扩展了ContextWrapp
IHelloServiceConnectionObserver observer = this;
ContextWrapper contextWrapper = this;
HelloServiceConnection svc = new HelloServiceConnection(observer, contextWrapper);
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();
    }
}