泛型与 Class<?>
Generics vs Class<?>
是否有可能从类型转换的泛型中实现 class
例如,这对我来说失败了 buildingObject.tojava(S) 在下面的例子中
public abstract class AbstractPythonService implements FactoryBean<IHelloService> {
public IHelloService getObject() {
//Here is the actual code that interprets our python file.
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("src/main/python/HelloServicePython.py");
PyObject buildingObject = interpreter.get("HelloServicePython").__call__();
//Cast the created object to our Java interface
return (IHelloService) buildingObject.__tojava__(IHelloService.class);
}
@Override
public Class<?> getObjectType() {
return IHelloService.class;
}
}
我想要这样的东西
public abstract class AbstractPythonService<S> implements FactoryBean<S> {
public S getObject() {
//Here is the actual code that interprets our python file.
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("src/main/python/HelloServicePython.py");
PyObject buildingObject = interpreter.get("HelloServicePython").__call__();
//Cast the created object to our Java interface
return (S) buildingObject.__tojava__(S.class);
}
@Override
public Class<?> getObjectType() {
return S.class;
}
}
因为类型擦除,你需要一个 Class<S>
对象,一些 Xyz.class
.
public abstract class AbstractPythonService<S> implements FactoryBean<S> {
private final Class<S> type;
protected AbstractPythonService(Class<S> type) {
super(type); // Probably the factory would also need the type.
this.type = type;
}
return type.cast(buildingObject.__tojava__(type)); // type.cast probably unneeded.
public Class<S> getObjectType() {
return type;
}
是否有可能从类型转换的泛型中实现 class 例如,这对我来说失败了 buildingObject.tojava(S) 在下面的例子中
public abstract class AbstractPythonService implements FactoryBean<IHelloService> {
public IHelloService getObject() {
//Here is the actual code that interprets our python file.
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("src/main/python/HelloServicePython.py");
PyObject buildingObject = interpreter.get("HelloServicePython").__call__();
//Cast the created object to our Java interface
return (IHelloService) buildingObject.__tojava__(IHelloService.class);
}
@Override
public Class<?> getObjectType() {
return IHelloService.class;
}
}
我想要这样的东西
public abstract class AbstractPythonService<S> implements FactoryBean<S> {
public S getObject() {
//Here is the actual code that interprets our python file.
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("src/main/python/HelloServicePython.py");
PyObject buildingObject = interpreter.get("HelloServicePython").__call__();
//Cast the created object to our Java interface
return (S) buildingObject.__tojava__(S.class);
}
@Override
public Class<?> getObjectType() {
return S.class;
}
}
因为类型擦除,你需要一个 Class<S>
对象,一些 Xyz.class
.
public abstract class AbstractPythonService<S> implements FactoryBean<S> {
private final Class<S> type;
protected AbstractPythonService(Class<S> type) {
super(type); // Probably the factory would also need the type.
this.type = type;
}
return type.cast(buildingObject.__tojava__(type)); // type.cast probably unneeded.
public Class<S> getObjectType() {
return type;
}