C++ 我可以写出一个模板功能,该功能采用数据类型作为理由,以便能够重复使用单一功能,而不是数据类型。 是否有在 Java做类似事情的规定?
Thanks,
Roger
C++ 我可以写出一个模板功能,该功能采用数据类型作为理由,以便能够重复使用单一功能,而不是数据类型。 是否有在 Java做类似事情的规定?
Thanks,
Roger
此人不问基因。 他询问了<代码><algorithm>中具体规定的那种类型的模板功能。 you最接近的是界定(遗传化) 您希望能够打电话,然后推出自己的实用图书馆,接受接口作为投入。 例如,你可以建立以下接口:
public interface UnaryOperator<T> {
public boolean test(T item);
}
从而形成这样的公用事业类别
public class Algorithms {
public static <T> void removeIf(Collection<T> c, UnaryOperator<T> op) {
Iterator<T> itr;
for (itr = c.iterator(); itr.hasNext(); ) {
T item = itr.next();
if (op.test(item)) {
itr.remove();
}
}
}
}
确实,你可以在阿帕奇科斯藏书图书馆找到这种模式,但其并不像C++算法图书馆那样灵活或广泛。 我认为,STL探测器和算法图书馆的任何具体例子都表明,在 Java,你能够组成类似的东西,但没有像我所知的类似建筑。 我知道的大多数人(7个C++ devs)认为:<algorithm>
只是弧度。
无, Java没有职能,但你可以形成通用的方法。 http://java.sun.com/docs/books/tutorial/extra/generics/methods.html” rel=“nofollow noreferer”>here。
<T> void foo( T object ) {
/** there is your code */
}
在 Java,参数类型称为Generics。 Sun has a good generics tutorial。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...