泛型扩展类AND实现接口

泛型扩展类AND实现接口

本文介绍了泛型扩展类AND实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来像一个奇怪的问题,但是如何定义一个通用的扩展类和实现一个接口? Im目前拥有以下原型的通用功能:

  public static< E extends Enum&E;列表与LT E  - 代替; buildEnumList(类< E> enumClass)

这个工作就像意图一样。我现在的问题是,我想进一步限制可执行的类到那些枚举和实现一个特定的接口可读(不是在java.lang中)。由于一个泛型使用相同的关键字 extends 来表示它应该实现一个接口,我没有看到任何方法来获得以下伪行为:

  public static< E extends Enum< E>实现可读>列表与LT E  - 代替; buildLexicographicalEnumList(class& E> enumClass)


解决方案

& 表示 E 还必须实现一个界面:

  public static< E extends Enum< E> &安培;可读>列表与LT E  - 代替; buildLexicographicalEnumList(Class< E> enumClass){


This might sound like an odd question but how can on define a generic which has to extend a class AND to implement an interface? Im currently having a generic function with the following prototype:

public static <E extends Enum<E>> List<E> buildEnumList(Class<E> enumClass)

This works just as intented. My problem now is that I want to further restrict the passable classes to those which are enums and implement a specific interface Readable (not that in java.lang). Since a generic uses the same keyword extends to indicate that it should implement an interface, I dont see any way to get the following pseudo behaviour:

public static <E extends Enum<E> implements Readable> List<E> buildLexicographicalEnumList(Class<E> enumClass)
解决方案

You can use & to indicate that E must also implement an interface:

public static <E extends Enum<E> & Readable> List<E> buildLexicographicalEnumList(Class<E> enumClass) {

这篇关于泛型扩展类AND实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:34