sách gpt4 ăn đã đi

java - 什么时候需要 Some> 而不是 Some?

In lại 作者:搜寻专家 更新时间:2023-11-01 03:46:56 25 4
mua khóa gpt4 giày nike

Để ý:这个问题与 Enum 无关,所以它不是重复的。Enum 被迫只与自身比较,因为编译器生成类型参数,而不是因为 java 递归类型参数。

我试图找到将类声明为的优势:

public class Some>

与声明为:

public class Some

我尝试提供返回 E 的方法和方法返回 Some ,复杂类层次结构中的不同交叉调用以及每次我试图删除额外的 - 没有出现新的错误/警告

你能告诉我一个方法来证明这个额外的 的优势吗? ?由于 JDK 声明,我假设存在一个:>

对 SO 上其他问题的回答例如:

With the additional construct, you know that any class that extends Enum is comparable only against itself

但是,我可以很容易地打破这个理论:

public static class Animal> {
public boolean compare(E other) {...}
}

public class Cat extends Animal { }
public class Dog extends Animal { } // Note "Cat" !!!

尽管是通用递归,我仍然可以比较 Dog 和 Cat:

Dog dog = new Dog();
dog.compare(new Cat());

传输理论:

you know that any class that extends Animal is comparable only against itself

这是错误的——我比较了 Dog 类,它用 Cat 扩展了 Animal,而不是它本身。

câu trả lời hay nhất

很少有像 class Some> 这样的边界的情况有必要的。大多数时候人们写这个,代码中实际上没有使用绑定(bind),class Some也可以。

但是,在某些特殊情况下,class Some> 中的边界实际使用。例如:

abstract class Some> {
abstract E foo();
Some bar() {
return foo();
}
}

关于您的问题——class Some 怎么样? ?好吧,第一个最明显的问题是您使用的是原始类型。永远不要在新代码中使用原始类型。但是你不相信。

具有上述类 (class Some) 的原始类型确实编译时带有警告(您可以忽略它,后果自负)。但是,原始类型意味着可以用它做不安全的事情。

想出一个例子来证明它是不安全的,需要付出一些努力。这是一个:

abstract class Some {
abstract E foo();
Some bar() {
return foo();
}
}

class SomeFoo extends Some {
SomeFoo foo() { return this; }
}

class SomeBar extends Some {
SomeFoo foo() { return new SomeFoo(); }
}

class SomeBaz extends Some {
SomeBar foo() { return new SomeBar(); }
}

// then in some method:
Some a = new SomeBaz();
Some b = a.bar();
SomeBar c = b.foo();

代码编译时有警告但没有错误,并抛出 Ngoại lệ ClassCast在运行时。

关于java - 什么时候需要 Some> 而不是 Some?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46915011/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress