什么是<和>意思是比如实现Comparable< BigInteger&gt ;? [英] What do < and > mean such as implements Comparable<BigInteger>?

查看:156
本文介绍了什么是<和>意思是比如实现Comparable< BigInteger&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 1.4.2 中,class java.math.BigInteger 实现了接口 Comparable 可序列化



Java 1.5.0 ,class java.math.BigInteger 实现了接口 Serializable $ Comparable< BigInteger>



这只是一个例子来帮助我询问< > 。我真正想知道的是< > 的东西。



我的问题有三个:


  • < BigInteger> 实现语句的一部分意味着什么?

  • 这是什么语法?

  • 它是做什么的?



PS:对于< > 并且不可能在< >



谢谢!

解决方案

阅读 Java泛型教程。尖括号之间的东西是一个类型参数 - Comparable是一个泛型类,在这种情况下,尖括号表示该类与其他BigInteger相媲美。



在这种情况下稍微澄清一点,请查看 Javadocs for Comparable 在1.5。请注意,它被声明为 Comparable< T> ,并且 compareTo 方法接受类型为 T 。 T是使用接口时填充的类型参数。因此,在这种情况下,声明实现 Comparable< BigInteger> 意味着您必须有一个 compareTo(BigInteger o)方法。另一个类可能实现 Comparable< String> ,这意味着它必须实现 compareTo(String o)方法。 / p>

希望你能从上面的代码中看到好处。在1.4中, compareTo 的签名只能使用 Object ,因为各类实现了Comparable,并且没有确切知道需要什么的方法。但是,对于泛型,您可以指定您相对于某个特定的类,然后编写一个更具体的compareTo方法,该类只将该类作为参数。



这里的好处是双重的。首先,您不需要在方法的实现中执行 instanceof 检查和强制转换。其次,编译器可以在编译时进行更多的类型检查 - 你不会意外地将字符串传递给实现 Comparable< BigInteger> 的东西,因为类型don' t匹配。编译器能够更好地指出这一点,而不是像通常在非泛型代码中发生的那样导致运行时异常。


In Java 1.4.2, class java.math.BigInteger implements interfaces Comparable, Serializable.

In Java 1.5.0, class java.math.BigInteger implements interfaces Serializable, Comparable<BigInteger>.

This is just an example to help me ask about < and >. What I am really wondering about is the < and > stuff.

My question is threefold:

  • what does the <BigInteger> part of the implements statement mean?
  • what is that syntax called?
  • and what does it do?

P.S.: It's really hard to google for < and > and impossible to search SO for < and > in the first place.

Thanks!

解决方案

Read the Java Generics Tutorial. The thing between the angle brackets is a type parameter - Comparable is a generic class, and in this case the angle brackets mean that the class is comparable to other BigIntegers.

For a little more clarification in this case, have a look at the Javadocs for Comparable in 1.5. Note that it is declared as Comparable<T>, and that the compareTo method takes an argument of type T. The T is a type parameter that is "filled in" when the interface is used. Thus in this case, declaring you implement Comparable<BigInteger> implies that you must have a compareTo(BigInteger o) method. Another class might implement Comparable<String> meaning that it would have to implement a compareTo(String o) method.

Hopefully you can see the benefit from the above snippet. In 1.4, the signature of compareTo could only ever take an Object since all kinds of classes implemented Comparable and there was no way to know exactly what was needed. With generics, however, you can specify that you are comparable with respect to a particular class, and then write a more specific compareTo method that only takes that class as a parameter.

The benefits here are two-fold. Firstly, you don't need to do an instanceof check and a cast in your method's implementation. Secondly, the compiler can do a lot more type checking at compile time - you can't accidentally pass a String into something that implements Comparable<BigInteger>, since the types don't match. It's much better for the compiler to be able to point this out to you, rather than have this cause a runtime exception as would have generally happened in non-generic code.

这篇关于什么是&lt;和&gt;意思是比如实现Comparable&lt; BigInteger&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆