关于java泛型的使用方式。。。。
2015-02-12 15:13
489 查看
转自:http://onewebsql.com/blog/generics-extends-super
以下基本够用了
Today we continue our mini-series on Java Generics. In previous posts we have investigated
how to crash your IDE using Java wildcards,
what beginners find surprising when creating parametrized objects, and
what is the difference between Java arrays and generic lists.
Java type parameters are used as type placeholders.
?
A List<X> is a container for X objects, and X can be instantiated with any class: you can haveList<Object>, List<String>, and List<Number>.
The syntax you use in Java is like this:
?
Every time you instantiate the Garage, the type parameter has to be a subclass of Vehicle.
?
Thus Garage<Car> and Garage<Motorcycle> are OK but Garage<Fruit> is not OK.
You can specify more than one bound with extends:
?
You can specify at most one class in the bound (obviously, as you can only inherit from one class in Java) and as many interfaces as you want.
The bound can refer to the typing parameter:
?
BST class can only be instantiated with classes X which implement the Comparable<X>interface.
?
As with class definitions, you often want to restrict the type parameter in the method. A method which takes a list of Vehicles and returns the fastest vehicle in the list can have the following type.
?
You can pass as argument a list of any vehicles. List<Car> is OK, List<Motorcycle> is OK, List<Vehicle> is OK, too. List<Number> is not OK.
Note that the following declaration wouldn't do the trick.
?
The argument to the method getFastest2 has to be exactly a List<Vehicle>, and not aList<Car>, because List<Car> is not a subtype of List<Vehicle>,
?
The parameter T occurs only once in the method signature, in an argument. You can imagine that the method body does not use the name T either. In this case you can use an alternative syntax, called wildcards, denoted with ?:
?
The two signatures for totalFuel are equivalent. The meaning of <? extends Vehicle> is: I don't care what the type parameter is, as long as it is a subclass of Vehicle.
?
Why? Because such construction doesn't make sense. For example, you can't erase the type parameter with Vehicle because the class Forbidden could be instantiated with Object. So you have to erase type parameters to Object anyway. If think about class Forbidden<Object>, it can take any value in place of X, not only superclasses of Vehicle. There's no point in using super bound, it wouldn't get us anything. Thus it is not allowed.
?
The method has to take a comparator which is able to compare Vehicles. If it comparesObjects as well, that's fine too.
People are often confused when to use extends and when to use super bounds. The rule of thumb is the get-put principle. If you get something from a parametrized container, useextends.
?
The method totalFuel gets Vehicles from the list, asks them about how much fuel they have, and computes the total.
If you put objects into a parametrized container, use super.
?
The method totalValue puts Vehicles into the Valuer.
It's useful to know that extends bound is much more common than super.
One more tip: if you are intimidated by wildcards (which is natural in the beginning), try to write the explicitly parametrized version first. In typical usage the two versions are equivalent. Eventually, you'll figure out when you can get rid of type parameters and use wildcards.
以下基本够用了
Today we continue our mini-series on Java Generics. In previous posts we have investigated
how to crash your IDE using Java wildcards,
what beginners find surprising when creating parametrized objects, and
what is the difference between Java arrays and generic lists.
Java type parameters are used as type placeholders.
?
Introducing bounds: extends
You often want to restrict the set of types that can be used in instantiation. If you create a class Garage, you want it to hold only Vehicle objects.The syntax you use in Java is like this:
?
?
You can specify more than one bound with extends:
?
The bound can refer to the typing parameter:
?
Bounds in method parameters
Java method can be parametrized, too. The syntax is as follows:?
?
Note that the following declaration wouldn't do the trick.
?
Wilcards
Take a look at the following declaration.?
?
Introducing bounds: super
There is also dual bound, called super. As you guess it is used to denote that you can pass only superclasses of the bound. There are some differences between extends and super, though.You can't use super in class declaration
The super bound is not allowed in class definition.?
Wildcards
The syntax for wildcards is also similar to extends:?
When to use extends and super
Wildcards are most useful in method parameters. They allow for the necessary flexibility in method interfaces.People are often confused when to use extends and when to use super bounds. The rule of thumb is the get-put principle. If you get something from a parametrized container, useextends.
?
If you put objects into a parametrized container, use super.
?
It's useful to know that extends bound is much more common than super.
One more tip: if you are intimidated by wildcards (which is natural in the beginning), try to write the explicitly parametrized version first. In typical usage the two versions are equivalent. Eventually, you'll figure out when you can get rid of type parameters and use wildcards.
相关文章推荐
- 关于Java泛型使用的问题记录
- Java基础(21):泛型—泛型的定义、用法与类型通配符的使用方式
- java关于泛型与自定义泛型的使用
- 关于java response的两种页面输出方式,以及HttpUrlconnection 代理使用注意点
- Java 理论与实践: 使用通配符简化泛型使用
- 关于使用java中的一些规范
- 关于如何使用自定义的结束消息循环的方式 (转载)
- 关于Lucene的详细说明和操作使用方式
- 关于在asp.net中使用showDialogModal方式输入数据中存在的问题
- Sun授权ASF以开放源代码方式使用Java
- 关于java中package使用的几点问题
- 在 Java 的泛型类型中使用通配符
- Java 泛型使用(续)
- Java 泛型使用(续)
- 关于JavaBean的使用!!!
- Java中对XML四种操作方式的使用
- 使用.net和java两种方式收取Gmail邮件。
- 关于使用Java开发工具的一些心得
- 关于javabean属性使用annotation约束的测试
- java 泛型?基本使用