关于泛型的类型安全就这么多了
有一些奇怪的事情让我无法理解。一般来说,泛型是一件好事。它在编译时提供类型安全。例如:
class A{}
class B{}
static void someMethod() {
List<B> listB = new ArrayList<B>();
listB.add(B);
listB.add(A); // here goes a compiler error
// but this will compile fine
List<Object> objList = new ArrayList<Object>();
objList.add("String");
objList.add(new Integer(9));
objList.add(B);
}
嗯,这是 Java 开发人员错过的东西吗?
There is something strange I can't wrap my head about. Generics is a good thing in general. It gives a type-safety at compile time. For example:
class A{}
class B{}
static void someMethod() {
List<B> listB = new ArrayList<B>();
listB.add(B);
listB.add(A); // here goes a compiler error
// but this will compile fine
List<Object> objList = new ArrayList<Object>();
objList.add("String");
objList.add(new Integer(9));
objList.add(B);
}
Well, is it something Java developers missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Object 是所有事物的基类,您正在添加 Object 类型的对象。因此类型安全(因为所有插入的对象都满足对象类型的要求)。
Object is the base class of everything and you are adding objects of type Object. Therefore type safe (as all inserted objects fulfill the requirement of being of type object).
如果您创建
对象
列表,则您可以添加对象。这就是为什么您可以添加String
、Integer
和B
的实例...它们都是对象!也许您想知道为什么不仅允许添加
对象
。嗯,允许添加子类型会非常方便。例如,考虑您想要在列表中存储大量数字。你不知道它们是浮点数还是整数等。那么你可以这样做If you create a list of
Objects
, you're allowed to add Objects. That's why you can add instances ofString
,Integer
andB
... they are all objects!Perhaps you wonder why one is not only allowed to add
Objects
. Well, it can be quite handy to allow for subtypes to be added. Consider for instance that you want to store lots of numbers in a list. You don't know if they are floating point numbers or integers etc. Then you could do简单来说,
"String"
new Integer(9)
和B
是Object
,而B
不是A
。在继承中,如果
A
扩展了B
,那么A 也被称为类型
A和
B;
Object是所有类的
超类。In simple words,
"String"
new Integer(9)
andB
areObject
, whereasB
is notA
.In inheritance, if
A
extendsB
, thenA is said to be of type
Aand
Bas well;
Objectis the
super` class of all the class.本质上:
是这样解释的
,事实上,向这个列表中添加任意对象都可以。这应该在编译时发出警告,因为您使用的是不带参数的参数化类型。
以这种方式实现是为了保持向后兼容性。如果所有使用 new ArrayList()(或任何其他集合框架类)的非参数化代码突然无法编译,那么将花费大量的时间和金钱来修复。是的,这意味着您可以编写不安全的代码,但您会收到警告,并且至少您现有的 1000 万行应用程序仍然可以工作。
Essentially:
is interpreted as
so, in fact, adding arbitrary Objects to this list is fine. This should emit a warning when it is compiled because you are using a parameterised type with no parameters.
It was implemented this way to preserve backward compatibility. If all non-parameterised code that used
new ArrayList()
(or any other collections framework class) suddenly didn't compile then that would cost an enormous amount of time and money to fix. Yes, it means you can write unsafe code, but you get warned about it and at least your existing 10-million-line application still works.Java 泛型确实提供了适当的类型安全性,但您没有适当地指定边界。这是泛型实际上为您提供的一个示例:
Java generics do provide proper type safety, but you are not specifying the boundaries appropriately. Here is an example what generics actually provide you with:
您的第一个示例失败的原因是 A 不是 B`。
第二个示例之所以有效,是因为
String
、new Integer(9)
和B
都是Object
。这具有良好的逻辑意义,并且出于其他发帖者所说的原因而实用。
the reason your first example fails is because
A is not a
B`.the reason the second example works is because
String
,new Integer(9)
, andB
are allObject
s.This makes good logical sense, and practical for the reasons other posters said.