这是你要翻译的内容: Java中的泛型接口是否可以强制执行泛型类型?

huangapple go评论56阅读模式
英文:

Is it possible for Java interface with generics to enforce the generic type?

问题

考虑这个例子

public interface Equatable<T> {
    public boolean equal(T t1, T t2);
}

public class Square implements Equatable<Square> {
    public boolean equal(Square t1, Square t2) {
        return false;
    }
}

接口Equatable是否可以强制Square实现一个接受两个Square对象作为参数的equal函数,而不仅仅是两个任意类型的对象(例如两个字符串等)?

英文:

Consider this example

    public interface Equatable&lt;T&gt; {
        public boolean equal(T t1, T t2);
    }

    public class Square implements Equatable&lt;Square&gt; {
        public boolean equal(Square t1, Square t2) {
            return false;
        }
    }

Is it possible for the interface Equatable to enforce Square to implement an equal function that takes in two Squares, and not just two of any types (two Strings etc)?

答案1

得分: 1

是的,你没有定义接口中的 "T" 是什么,你可以将其限制为只能用于实现 Equatable 接口的类,因此 String 将不被允许:

public interface Equatable<T extends Equatable<T>>
{ 
     //...
}

正确的用法:

public class Square implements Equatable<Square>
{
    public boolean equal(Square t1, Square t2)
    {
        return false;
    }
}

编译错误:

public class Circle implements Equatable<String>
{
    public boolean equal(String t1, String t2)
    {
        return false;
    }
}

注意
请注意,将相同的类型名称放入尖括号中是你的责任,因为编译器不会覆盖下面的情况。
我猜你想阻止对不同类型使用 equal 方法。

public class Square implements Equatable<Circle>
{
    public boolean equal(Circle t1, Circle t2)
    {
        return false;
    }
}
英文:

Yes, you didn't define what is "T" in your interface, you can limit it only to classes which implements Equatable, so String will not be allowed:

public interface Equatable&lt;T extends Equatable&lt;T&gt;&gt;
{ 
     //...
}

Works Good:

public class Square implements Equatable&lt;Square&gt;
{
	public boolean equal(Square t1, Square t2)
	{
		return false;
	}
}

Compilation Error:

public class Circle implements Equatable&lt;String&gt;
{
	public boolean equal(String t1, String t2)
	{
		return false;
	}
}

Note<br>
Pay attention, it's your responsibility to put in the brackets the same Type name, because below case will not be covered by compiler.<br>
I guess you want to disallow to use equal method for different type.

public class Square implements Equatable&lt;Circle&gt;
{
	public boolean equal(Circle t1, Circle t2)
	{
		return false;
	}
}

答案2

得分: 0

你可以通过递归的泛型类型在一定程度上实现这一点。

public interface Equatable<T extends Equatable<T>> {
    public boolean equal(T t1, T t2);
}

相关链接:https://stackoverflow.com/questions/211143/java-enum-definition

英文:

You can enforce this to an extent with a recursive generic type.

public interface Equatable&lt;T extends Equatable&lt;T&gt;&gt; {
    public boolean equal(T t1, T t2);
}

related: https://stackoverflow.com/questions/211143/java-enum-definition

huangapple
  • 本文由 发表于 2020年10月10日 02:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64285780.html
匿名

发表评论

匿名网友

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

确定