简体   繁体   English

如何从Java中的方法返回通用类型

[英]How to return generic type from a method in java

I'm new to generics. 我是泛型新手。 Here are my classes. 这是我的课。

public interface Animal {    
    void eat();
}

public class Dog implements Animal {    
   public void eat() {   
      System.out.println("Dog eats biscuits");    
   }    
}

public class Cat implements Animal {    
   public void eat() {    
      System.out.println("Cat drinks milk");    
   }    
}

Now I want these classes to be used in a generic way. 现在,我希望以通用方式使用这些类。

public class GenericExample {

   public <T extends Animal> T method1() {    
      //I want to return anything that extends Animal from this method
      //using generics, how can I do that    
   }

   public <T extends Animal> T method2(T animal) {    
      //I want to return anything that extends Animal from this method
      //using generics, how can I do that
   }

   public static void main(String[] args) {    
      Dog dog = method1(); //Returns a Dog    
      Cat cat = method2(new Cat()); //Returns a Cat    
   }    
}

How can I return the generic type (may be a Cat or Dog) from the methods "method1" and "method2". 如何从方法“ method1”和“ method2”返回通用类型(可能是猫或狗)。 I've several such methods that returns "T extends Animal", so is it better to declare the generic type in a method level or class level. 我有几种返回“ T扩展动物”的方法,因此最好在方法级别或类级别声明泛型。

You cannot have a method returning a generic type, and expect to be able to access that type in the caller of the method without a cast, unless that type can be deduced from the arguments to the method. 您不能有一个返回泛型类型的方法,并且期望能够在不使用强制转换的情况下在该方法的调用者中访问该类型,除非可以从该方法的参数推断出该类型。 So the examples in your main won't work. 因此,您main的示例将无效。

So you either go without generics, and either cast the return type manually 因此,您要么不用泛型,要么手动转换返回类型

public Animal method1()
    Dog dog = (Dog)method1()

or have the method return the subclass type to start with. 或让方法返回子类类型开始。

public Dog method1()
    Dog dog = method1()

Or you can go with generics, and either specify the type when calling the method 或者您可以使用泛型,并在调用方法时指定类型

public <T extends Animal> T method1()
    Dog dog = <Dog>method1()

or pass some argument from which the type can be deduced (which the second method already satisfies): 或传递可从中推导类型的参数(第二种方法已满足该参数):

public <T extends Animal> T method1(Class<T> classOfreturnedType)
    Dog dog = method1(Dog.class)

Also nituce that you only can call method1 from static main if it is static itself. 还要指出,如果它本身是static ,则只能从static main调用method1

The method1 simply says it returns an Animal. method1只是说它返回一个动物。 Return any instance of Animal, and the code will compile: 返回任何Animal实例,代码将编译:

return new Dog();

The second method says that it returns an animal which is of the same type than the animal given as argument. 第二种方法说,它返回的动物的类型与作为参数给出的动物的类型相同。 So you could return the argument itself, and it would compile. 因此,您可以返回参数本身,并进行编译。 You've said which type the method must return, but you haven't said what the method must do and return, so it's impossible to implement it. 您已经说过该方法必须返回的类型,但是您没有说该方法必须返回的类型,因此无法实现它。 All I can say is that return animal; 我只能说是return animal; would compile. 会编译。

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

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