简体   繁体   English

我可以声明方法来返回Class对象,并限制Java中给定类型的子类吗?

[英]Can I declare method to return Class object with restriction to subclasses of given type in Java?

Suppose I declare some abstract method, that will return Class object (like Dog.class, Cat.class), like this: 假设我声明了一些抽象方法,它将返回Class对象(如Dog.class,Cat.class),如下所示:

abstract public Class getProducedAnimalType();

Can I force clients extending this API to return only Class objects that represents classes, wchich are subclassess of Animal.class ? 我可以强制扩展此API的客户端只返回表示类的Class对象,它们是Animal.class的子类吗? something like: 就像是:

abstract public Class<representing subclass of Animal> getProducedAnimalType();

So the client extending my class cannot write: 所以扩展我的类的客户端不能写:

public Class getProducedAnimalType() {
   // Integer not extends Animal!, shoud not compile!
   return Integer.class; 
}

but can: 但可以:

public Class getProducedAnimalType() {
   return Fish.class; // Fish extends Animal
}

You can change the return type Class to Class<? extends Animal> 您可以将返回类型Class更改为Class<? extends Animal> Class<? extends Animal> . Class<? extends Animal> The following code will allow to return Cat.class and Dog.class , but won't allow to return Integer.class : 以下代码将允许返回Cat.classDog.class ,但不允许返回Integer.class

abstract public Class<? extends Animal> getProducedAnimalType();

The client can write the following code and will compile fine: 客户端可以编写以下代码并编译正常:

public Class<? extends Animal> getProducedAnimalType() 
{
   return Fish.class; // Fish extends Animal
}

But the following code won't compile: 但是以下代码将无法编译:

public Class getProducedAnimalType() 
{
   return Integer.class; 
}
public Class<? extends Animal> getProducedType() {
// return Integer.class; // compile time error: incompatible types
    return Dog.class;
}

暂无
暂无

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

相关问题 我可以用通用形式声明一个类,然后将子类转换为在Java中声明为通用的类型吗? - can i declare a class with generic form and then cast the subclasses to the type that is declared generic in java 给定一个Class <?>,我可以确定它是否是特定类型的子类吗? - Given a Class<?>, can I determine if it subclasses a particular type? 在Java中,如果递归方法可以返回混合值,我应该如何声明返回类型? - In Java, how should I declare the return type if a recursive method can return mixed values? 根据Java中的对象类型调用子类的方法 - Call Subclasses' Method Depending on Object Type in Java 你如何让一个类的子类返回正确的类型 Java - How do you make subclasses of a class return right type Java 如何用每个对象的类的返回类型声明一个类型间方法? - How to declare an inter-type method with return type of every object's class? 为什么我不能在 Java 的本地类中声明对象? - Why can't I declare object in my local class in Java? 带有子类的java中的toString方法的对象 - object to toString method in java with subclasses 我如何在同一个 class 的不同方法中使用私有 static 方法(对象的扩展)的返回值? (爪哇) - How can i use the return value of a private static method (extension of object) in a different method in that same class? (java) 如何声明可以是超类子类之一的对象的类字段 - How can you declare a class field that can be an object of one of a superclass's subclasses
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM