Java"接口"类

Gre*_*Guy 3 java interface class java-8

在Java中,有一个Class类可用于表示其他类而无需实例化它们.例如:

Class[] foo = { ArrayList.class, String.class, Character.class, Integer.class, MyCustomClass.class };
ArrayList bar = foo[0].newInstance();
Run Code Online (Sandbox Code Playgroud)

将是有效的代码.例如,我也可以运行一个Objects 数组instanceof并对它们进行检查,以检查数组中的相应类foo,这些类具有潜在的应用程序.

Java中是否存在这样的接口类?是否有一些类我可以做类似这样的事情:

Interface[] foo = {Comparable, List<String>, MyCustomInterface}
Run Code Online (Sandbox Code Playgroud)

从Java 8开始,Interfaces现在可以有静态方法,如果做法不好,这种行为可能会有用.它存在吗?

And*_*ner 10

接口只是一个Class.isInterface()返回true 的类.所以:

Class<?>[] foo = {Comparable.class, List.class, MyCustomInterface.class};
Run Code Online (Sandbox Code Playgroud)

请注意,没有通用的类实例,例如List<String>:它只是原始的List.class.