Java中的通用参数类型和返回类型(Generic Parameter Type and Return Type in Java)

编程入门 行业动态 更新时间:2024-10-18 02:30:49
Java中的通用参数类型和返回类型(Generic Parameter Type and Return Type in Java)

我正在尝试创建一个Java方法,它接受一个Object类型以及它应该转换为的数据类型。

例如,如果我能够根据需要将值1返回为Int或Double。 我使用Class传递数据类型作为参数。

问题:如何根据输入参数使方法通用以接受返回类型?

下面的代码只是一个示例,它可能在语法上不正确,并提供解释我的问题。

public class JavaTest { public static void main(String[] args) { getValue(1,int.class); getValue(1,double.class); getValue("adf",String.class); } public static <E> void getValue(Object obj,Class<?> type) { if (type == String.class) { // return string value }else if (type == int.class) { //return Integer.parseInt(obj); }else if (type == double.class) { //return Double.parseDouble(obj); } } }

I am trying to create a Java method which accept a Object type and the type of data type it should be converted to.

For example if I should be able to return a value of 1 as Int or Double as required. I am using Class to pass the data type as parameter.

Question: How can I make the method generic to accept return type based on the input parameter?

The below code is just an example and it may be syntactically incorrect and provided to explain my question.

public class JavaTest { public static void main(String[] args) { getValue(1,int.class); getValue(1,double.class); getValue("adf",String.class); } public static <E> void getValue(Object obj,Class<?> type) { if (type == String.class) { // return string value }else if (type == int.class) { //return Integer.parseInt(obj); }else if (type == double.class) { //return Double.parseDouble(obj); } } }

最满意答案

如何根据输入参数使方法通用以接受返回类型?

type参数可用于指定方法的返回类型,您可以通过该参数的类型参数将其与Class参数相关联:

public static <E> E getValue(Object obj, Class<E> type) {

您还需要使用Class.cast()来满足编译器该方法返回正确类型的要求。 (您也可以使用普通的演员,但这将受到类型安全警告的影响。)例如:

// ... } else if (type == Integer.class) { return type.cast(Integer.valueOf(obj.toString())); }

另请注意,您不能以这种方式直接对原始类型进行转换,因为您的代码建议您尝试这样做。 您必须转而使用它们的包装类。 换句话说,你不能使用int.class , double.class ; 您必须改为使用Integer.class , Double.class等。

How can I make the method generic to accept return type based on the input parameter?

The type parameter can be used to designate the method's return type, and you can associate that with the Class argument by means of that argument's type parameter:

public static <E> E getValue(Object obj, Class<E> type) {

You will also need to use Class.cast() to satisfy the compiler that the method returns the correct type. (You could also use an ordinary cast, but that will be subject to type safety warnings.) For example:

// ... } else if (type == Integer.class) { return type.cast(Integer.valueOf(obj.toString())); }

Note also that you cannot this way effect conversions directly to primitive types, as your code suggests you are trying to do. You must instead convert to their wrapper classes. In other words, you cannot use int.class, double.class, etc.; you must instead use Integer.class, Double.class, and so forth.

更多推荐

本文发布于:2023-08-05 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1441011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   参数   Java   Generic   Return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!