有没有办法检索Network
类(定义如下(中的静态类数组,并将每个类的属性class
传递到方法调用kryo.register
的参数中?
public class Network {
// Classes to be transferred between the client and the server
public static class A {
public int id;
public String name;
}
public static class B {
public int id;
public int x;
public int y;
}
// Rest of the classes are defined over here
static public void register(EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();
// typical way of registering classes so that kryonet can use it
// kryo.register(A.class);
// kryo.register(B.class);
// the rest of the classes are registered with kryonet over here
// my attempt at solving the question,
// but for some reason this doesn't work?
for(Object o : Network.class.getDeclaredClasses()) {
kryo.register(o.getClass());
}
}
}
问题是你正在使用类的类,这不是你想要的。 如果对getDeclaredClasses()
调用的结果使用正确的类型,则会更明显:
for(Class<?> c : Network.class.getDeclaredClasses()) {
kryo.register(c);
}
(顺便说一句,您已经在使用反射 -> getDeclaredClasses()
(。