覆盖方法;返回具有合并参数的类

huangapple 未分类评论50阅读模式
英文:

Overriding method; Returning class with consolidated parameters

问题

public class SomeClass {
	
}

public class ContainerClass<A extends SomeClass> {
	
}

public class DuoClass<A extends SomeClass, B extends ContainerClass<A>> {
	
	public <X extends SomeClass, Y extends ContainerClass<X>> DuoClass<X,Y> methodToOverride(X field) {
		return null;
	}
	
}

public class Overriding_Attempt<A extends SomeClass> extends DuoClass<A,ContainerClass<A>> {
	//Error Here: The return type is incompatible with DuoClass<A,ContainerClass<A>>.methodToOverride()
	public <X extends SomeClass, Y extends ContainerClass<X>> Overriding_Attempt<X> methodToOverride(X field) {
		return null;
	}
}
英文:

I'm trying to override a method that returns an instance of the class that it is declared in, which changes the parameters of the class between the instance that the method is called on and the instance returned by the call. I would like to be able to return an instance of the Overriding_Attempt class from the overriding method.

It's not clear to me why Overriding_Attempt&lt;X&gt; is not equivalent to DuoClass&lt;X,ContainerClass&lt;X&gt;&gt; given the extension in the declaration of Overriding_Attempt. I would like to be able to return this more specific instance of DuoClass&lt;X,ContainerClass&lt;X&gt;&gt;. The error I get is The return type is incompatible with DuoClass&lt;A,ContainerClass&lt;A&gt;&gt;.methodToOverride()

This issue is very much related to the Nested Parameters question posted here.

Also of note: Deleting the declaration of parameter Y from Overriding_Attempt seems to cause a signature mismatch such that the error becomes Name clash: The method methodToOverride() of type Overriding_Attempt&lt;A&gt; has the same erasure as methodToOverride() of type DuoClass&lt;A,B&gt; but does not override it. It seems odd that I would need to declare an unused parameter but that's not my main problem.

public class SomeClass {
	
}

public class ContainerClass&lt;A extends SomeClass&gt; {
	
}

public class DuoClass&lt;A extends SomeClass, B extends ContainerClass&lt;A&gt;&gt; {
	
	public &lt;X extends SomeClass, Y extends ContainerClass&lt;X&gt;&gt; DuoClass&lt;X,Y&gt; methodToOverride(X field) {
		return null;
	}
	
}

public class Overriding_Attempt&lt;A extends SomeClass&gt; extends DuoClass&lt;A,ContainerClass&lt;A&gt;&gt; {
	//Error Here: The return type is incompatible with DuoClass&lt;A,ContainerClass&lt;A&gt;&gt;.methodToOverride()
	public &lt;X extends SomeClass, Y extends ContainerClass&lt;X&gt;&gt; Overriding_Attempt&lt;X&gt; methodToOverride(X field) {
		return null;
	}
}

答案1

得分: 0

我非常确定您实际上并不想要带有类型的方法 - 即XY实际上是AB,因此您不应该将类型XY添加到该方法中。

考虑到这一点,您需要一个自引用类型:

public class DuoClass<A extends SomeClass, B extends ContainerClass<A>, T extends DuoClass<A, B, T>> {
    public T methodToOverride(A field) {
        return null;
    }
}

public class Overriding_Attempt<A extends SomeClass> extends DuoClass<A, ContainerClass<A>, Overriding_Attempt<A>> {
    public Overriding_Attempt<A> methodToOverride(A field) {
        return null;
    }
}

您所面临的通用“死结”是编译器不知道返回类型DuoClass<A, B>与声明类是同一个类 - 理论上它可以是任何子类,但您希望它是声明类。

通过为声明类型添加额外的类型,您锁定了对声明类型的引用,并且可以将其表示为T

英文:

I'm pretty sure you don't actually want typed methods - ie X and Y are actually A and B, so you should not add types X and Y to the method.

Given that, you need a self-referencing type:

public class DuoClass&lt;A extends SomeClass, B extends ContainerClass&lt;A&gt;, T extends DuoClass&lt;A, B, T&gt;&gt; {
    public T methodToOverride(A field) {
        return null;
    }
}

public class Overriding_Attempt&lt;A extends SomeClass&gt; extends DuoClass&lt;A, ContainerClass&lt;A&gt;, Overriding_Attempt&lt;A&gt;&gt; {
    public Overriding_Attempt&lt;A&gt; methodToOverride(A field) {
        return null;
    }
}

The generic "knot" you were facing is that the compiler doesn't know that the return type DuoClass&lt;A, B&gt; is the same class is the declaring class - it could theoretically be any subclass, but you want it to be the declaring class.

By adding an extra type for the declaring type, you lock in the reference to the declaring type and can refer to it a T.

huangapple
  • 本文由 发表于 2020年7月24日 12:31:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63066855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定