4
A
回答
1
在Java中,參數化類型被稱爲Generics。 Sun有很好的generics tutorial。
2
2
Generics are a facility of generic programming that was added to the Java
programming language in 2004 as part of J2SE 5.0. They allow "a type or method
to operate on objects of various types while providing compile-time type safety.
周守軍wikipedia。
3
這個人不是在問泛型。他詢問在<algorithm>
中指定的那種模板函數。最接近Java的可能是爲每個要調用的函數類型定義(通用化)接口,然後將接受實例接口實例的自己的實用程序庫作爲輸入。例如,你可以創建以下接口
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();
}
}
}
}
實際上,你可以找到在Apache Commons Collections中圖書館這種模式,但它的不靈活或廣泛的C++算法庫。我想給出STL函子和算法庫的任何特定的例子,你可以用Java編寫類似的東西,但沒有內建的我知道的等價物。我認識的大多數人(甚至C++開發人員)都認爲<algorithm>
非常神祕。
相關問題
- 1. 模板模板C++函數
- 2. C++中的模板函數
- 3. C++函數模板
- 4. c + +模板函數
- 5. C++:函數模板
- 6. 函數模板的C++ Decltype
- 7. C++中的模板函數參數14
- 8. C模板仿函子。類模板vs函數模板
- 9. C++模板類和模板函數
- 10. C++模板最小函數
- 11. C++中的模板化函數指針
- 12. 類型(函數模板):C++
- 13. Java中的C++模板
- 14. 包裝C++模板函數
- 15. C++函數嵌套模板
- 16. C++函數模板問題
- 17. C++模板仿函數
- 18. C++ variadic函數模板
- 19. C++返回模板函數
- 20. C++模板函數指針
- 21. C++函數模板重載
- 22. 模板函數重載C++
- 23. 爲函數模板C++
- 24. C++仿函數模板
- 25. 錯誤:C++模板函數
- 26. C++模板函數重載
- 27. 模板類中的模板成員函數的定義(C++)
- 28. C++模板函數選擇
- 29. 作爲模板函數的模板參數的模板函數
- 30. C++仿函數和函數模板
正如答案所述,Java中的泛型是您所需要的。檢查這個線程在SO比較模板和泛型:http://stackoverflow.com/questions/36347/what-are-the-differences-between-generic-types-in-c-and-java – sateesh 2010-01-15 12:06:53
他沒有問泛型。他問的是與此相當的java:http://www.sgi.com/tech/stl/functors.html – Jherico 2010-01-15 18:33:11