简体   繁体   English

相同方法的不同返回类型

[英]Different return type for the same method

i need to implement the newInstance method in order to make this code to work: 我需要实现newInstance方法,以使此代码工作:

protected IDatabase createDatabase() throws Exception{
    return newInstance(CLASS_NAME_DATABASE);
}

protected IDataLoader createDataLoader() throws Exception{
    return newInstance(CLASS_NAME_DATA_LOADER);
}

I'm not allowed to touch this code, but when i try to implement the newInstance as Object it means i need to cast the newInstance in the code above you. 我不允许触摸此代码,但是当我尝试将newInstance实现为Object时,这意味着我需要在您上面的代码中强制转换newInstance。 Is there a way for me to implement newInstance without changing the code? 有没有办法在不更改代码的情况下实现newInstance?

Thanks! 谢谢!

If the parameter to newInstance were a Class object, or something else with a generic type parameter, you could do the following, which would avoid needing to cast the return value, and be type-safe: 如果newInstance的参数是Class对象,或带有泛型类型参数的其他参数,则可以执行以下操作,这将避免需要强制转换返回值,并且是类型安全的:

protected <T> T newInstance(Class<T> klass) throws Exception {
    return klass.newInstance();
}

(Note: klass.newInstance() is the reflection API method, not the same as the method being defined, despite the same name.) (注意: klass.newInstance()是反射API方法,与定义的方法不同,尽管名称相同。)

However, you can use generics no matter what sort of parameters newInstance takes: 但是,无论newInstance采用什么类型的参数,您都可以使用泛型:

@SuppressWarnings("unchecked")
protected <T> T newInstance(String className) throws Exception {
    return (T)Class.forName(className).newInstance();
}

This is a bit dirty, because it completely eliminates compile-time error checking on the type of the return value. 这有点脏,因为它完全消除了返回值类型的编译时错误检查。 The compiler will allow IDataLoader x = newInstance(CLASS_NAME_DATA_LOADER); 编译器将允许IDataLoader x = newInstance(CLASS_NAME_DATA_LOADER); but will just as happily allow int x = newInstance(CLASS_NAME_DATA_LOADER); 但同样乐意允许int x = newInstance(CLASS_NAME_DATA_LOADER); (which actually casts it to an Integer then unboxes it). (实际上将它转换为Integer然后将其取消装箱)。

However, manually casting an Object return value would also not be checked at compile time, so using generics here is probably still an improvement since it reduces the number of times you have to write each type. 但是,在编译时也不会检查手动转换Object返回值,因此在此处使用泛型可能仍然是一种改进,因为它减少了您必须编写每种类型的次数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 继承同名但返回类型不同的方法 - Inheriting a method with the same name but different return type 具有相同参数和不同返回类型的Spring RESTful GET方法 - Spring RESTful GET method with same parameters and different return type 两个接口具有相同的方法名称和不同的返回类型 - Two interface have same method name with different return type 使用相同的方法签名但不同的返回类型实现两个接口 - Implement two interfaces with the same method signature but different return type 如何为同一方法传递不同的 object 类型,而不是在 java 中编写具有相同功能和返回类型的两个方法 - How to pass different object type for same method instead of writing two methods with same functionality and return type in java 如果子类中的实例方法具有相同的签名和DIFFERENT返回类型,那么它是重写方法还是新方法? - If an instance method in the subclass has the same signature and a DIFFERENT return type, is it a overriding method or a new method? 我可以使用另一个具有相同参数但具有不同返回类型的方法重载方法吗? - can i overload a method with another one that takes the same parameters but has a different return type? 从另一个类扩展的类可以使用具有不同返回类型的相同方法吗? - Can a class extended from another class use the same method with different return type? 当一个类实现两个接口时,接口具有相同的方法名称,但返回类型不同,为什么它不起作用? - When a class implements two interfaces, interfaces have same method name but different return type why it wont work? 使用具有相同方法但返回类型不同的两个接口的Java多重继承 - Java multiple inheritance using two interface having same method but different return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM