简体   繁体   English

为什么Java不能将类型作为参数?

[英]why can't java have types as arguments?

why can't java have types as arguments ? 为什么Java不能将类型作为参数?

eg can't have Vector of reals where compiler checks type. 例如,不能有编译器检查类型的实数向量。

(example taken from http://www.ics.uci.edu/~kibler/javacourse/java.html ) (示例取自http://www.ics.uci.edu/~kibler/javacourse/java.html

I think that might be outdated, as now you can do: 我认为这可能已过时,因为您现在可以这样做:

Vector<Real> vec = new Vector<Real>();

and the compiler will signal an error if you try to : 如果您尝试执行以下操作,编译器将发出错误信号:

vec.add(new Animal());

Because that page is 12 years old and not very well written besides. 因为该页面已有12年历史,而且书写得不太好。 Java actually does have generics . Java实际上确实具有泛型

Use a collection with generics support, such as Vector<Double> instead of its non generic counterpart. 使用具有泛型支持的集合,例如Vector<Double>而不是其非泛型副本。

Nowadyas Java does support "types as parameters", or genericity. Nowadyas Java确实支持“类型作为参数”或通用性。 Read more on that here . 这里阅读更多。 Your link is referring to ancient Java versions. 您的链接是指古老的Java版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM