English 中文(简体)
JavaMmimic C++模板功能
原标题:Mmimic C++ template functions in Java
  • 时间:2010-01-15 11:34:39
  •  标签:
  • java
  • c++

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 */
}




相关问题
Spring Properties File

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 ...

Logging a global ID in multiple components

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 ...

Java Library Size

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 ...

How to get the Array Class for a given Class in Java?

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....

SQLite , Derby vs file system

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 ...

热门标签