简体   繁体   English

方法中有多个返回类型

[英]more than one return type from method

Is there a way to get different return type from a method like int or String or double? 有没有办法从int或String或double等方法获取不同的返回类型?

what i can think of is using generics wildcard,not sure 我能想到的是使用泛型通配符,不确定

like: 喜欢:

public x m1(){
//implementation
}

x can be int,string,long etc x可以是int,string,long等

No, because Java is statically typed. 不,因为Java是静态类型的。 You can use generics or polymorphic types, but you cannot just return arbitrary type (well, you can return Object ...) How are you suppose to work with return value? 你可以使用泛型或多态类型,但你不能只返回任意类型(好吧,你可以返回Object ...)你觉得如何使用返回值?

??? value = m1()

The type of value is unknown and you cannot do anything about it. value的类型是未知的,你无法做任何事情。

You can however think of some generic, strongly typed container, similar to Either . 但是,您可以想到一些类似于Either通用强类型容器。 Apache Commons Lang 3 has Pair<L,R> , you can take it as an example. Apache Commons Lang 3有Pair<L,R> ,你可以把它作为一个例子。

No, a method can have only one return type. 不,方法只能有一种返回类型。 If you want to return objects of different types you can return Object but you'd still have to cast in that case. 如果要返回不同类型的对象,可以返回Object但在这种情况下仍然需要进行转换。 You also can't return unboxed primitives in that case (ie int or double ). 在这种情况下,您也无法返回未装箱的基元(即intdouble )。

Generics could help somewhat in that they add some of those casts implicitly. 泛型可能有所帮助,因为他们隐含地添加了一些演员。 However, depending on what you want to achieve it might not be worth it. 但是,根据您想要实现的目标,它可能不值得。

We're using something like this, for example: 我们正在使用这样的东西,例如:

class BasicBuilder<T extends BasicBuilder<T>> {
  private int value;

  public T setValue( int v ) {
    value = v;
     return (T)this;
  }
}

class ConcreteBuilder extends BasicBuilder<ConcreteBuilder> {
  //more here
}

This way, when I have a ConcreteBuilder instance and call setValue(...) on it, I'll get a ConreteBuilder reference back. 这样,当我有一个ConcreteBuilder实例并在其上调用setValue(...)时,我将得到一个ConreteBuilder引用。 Note that this only works if the generics are used properly and the casts are valid. 请注意,这仅在正确使用泛型并且强制转换有效时才有效。

You could do this in HotSpot Java 6 with generics, but this was considered a bug and Java 5.0 nor Java 7 supports this. 您可以使用泛型在HotSpot Java 6中执行此操作,但这被视为一个错误,Java 5.0和Java 7都支持此操作。

http://vanillajava.blogspot.com/2011/02/with-generics-return-type-is-part-of.html http://vanillajava.blogspot.com/2011/02/with-generics-return-type-is-part-of.html

Java in general is not context aware, but this is changing with implied type support in Java 7 and not implied types with Java 8. Java通常不支持上下文,但这种情况正在改变,Java 7中的隐含类型支持,而不是Java 8中隐含的类型。

The common workaround is to specify the type in the name. 常见的解决方法是在名称中指定类型。 eg ByteBuffer or DataInput with getInt() , getLong() , getDouble() 例如带有getInt()getLong()getDouble() ByteBuffer或DataInput

Use Object class as return type and then use Integer or String (Wrapper) classes. 使用Object类作为返回类型,然后使用Integer或String(Wrapper)类。 So the point where you are returning you can use Object's getClass() method to identify its class. 因此,您返回的点可以使用Object的getClass()方法来标识其类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM