Java 泛型在子类化时不兼容类型

2023-12-20

从泛型类类型/形式类型参数进行子类化时T/E具有有效的类类型/实际类型参数,例如Type/String出现多种组合,令人困惑的是该使用哪一种以及何时使用?

    public class SubClass<T> implements SuperIfc<T>  <-- It is straight forward to understand
    public class SubClass<T> implements SuperIfc<Type>

    public class SubClass<Type> implements SuperIfc<T>
    public class SubClass<Type> implements SuperIfc<Type>
    public class SubClass<Type> implements SuperIfc
    public class SubClass implements SuperIfc<Type>
    public class SubClass implements SuperIfc<T>    <--- Hope we cannot declare <T> in his case while initialising SubClass.

    // Bounded type parameter
    public class SubClass<T extends Type> implements SuperIfc<Type>
    public class SubClass<T extends Type> implements SuperIfc<T> <-- Looks <T> at SuperIfc also refers <T extends Type>, and no need to declare it again at SuperIfc.

    // Recursive type bound
    public class SubClass<T extends Comparable<T>>> implements SuperIfc<T>
    public class SubClass<T extends Comparable<T>>> implements SuperIfc<Type>

这样我就可以更清楚地解决问题incompatible types while subclassing

Case_1:

public class Test {

    interface TestIfc {

        public static <T extends TestIfc> T of(int choice) {

            if(choice == 1) {
                return new TestImpl(); <-- PROB_1: incompatible type error 
            } else {
                return new SomeOtherTestImpl(); //incompatible type error
            }
        }
    }

    static class TestImpl implements TestIfc {}
    
    static class SomeOtherTestImpl<T extends TestIfc> implements TestIfc {

        //The below method also having same error though with declaration
        public T of() {
            return new TestImpl();  <-- PROB_2: incompatible type error
        }
    }
}

Case_1:PROB_1:返回类型是T extends TestIfc并返回TestImpl implements TestIf那么有什么问题吗?

Case_1:PROB_2:与PROB_1类似,如何在不进行外部铸造的情况下进行纠正。请帮忙。


Case_2:

public interface SuperIfc<T> {
    
    public T create(Object label);
}

class Type {

    public static Type of(){
         return new Type();
    }
}
------

public class SubClass<Type> implements SuperIfc<Type>{

    @Override
    public Type create() {
        return Type.of(); <---- PROB_1: cannot resolve method
    }
}
-------

public class SubClass<T extends Type> implements SuperIfc<Type>{

    @Override
    public Type create() {
        return Type.of(); <---- PROB_1: is resolved
    }
}

SuperIfc<Type> object = new SubClass(); <-- PROB_2 Unchecked assignement warning
SuperIfc<Type> object = new SubClass<TypeImpl>(); <-- PROB_3: bound should extend Type
  1. 我想知道如何一起解决Case_2、PROB_1和PROB_2?

  2. 如何用类类型编写泛型超类的子类以及规则是什么?

  3. 变更通用名时应注意什么T去上课Type子类化时?可能是下面和何时使用之间的区别?

     public class SubClass<Type> implements SuperIfc<Type>
     public class SubClass<Type> implements SuperIfc
     public class SubClass implements SuperIfc<Type>
     public class SubClass<T extends Type> implements SuperIfc<Type>
     public class SubClass<T extends Type> implements SuperIfc<T>
     public class SubClass<T> implements SuperIfc<Type>
    

在第一个of()方法,该方法可以返回实现的任何类型InformationIfc,但你的方法总是返回一个特定的实现 -InformationImpl- 这是不可接受的。

例如,如果您有其他课程SomeOtherInformationImpl实现该接口,该方法的调用者将被允许编写:

SomeOtherInformationImpl i = InformationImpl.of();

但你的方法不返回SomeOtherInformationImpl.

第二of()方法与第一种方法有同样的问题。 如果您使用以下方法实例化您的类:

InformationImpl i = new InformationImpl<SomeOtherInformationImpl>();

the of()方法必须返回一个SomeOtherInformationImpl, not a InformationImpl.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java 泛型在子类化时不兼容类型 的相关文章

随机推荐

  • MVC 提交按钮未触发

    我正在使用 ASP net MVC 4 和 Razor 引擎 我有一个页面 Index cshtml 和一个控制器 HomeController cs 我正在尝试将我的提交按钮连接到控制器中的操作结果 但我似乎无法让它触发 我的HTML u
  • 如何使用 Visual Studio 2010 在 Win32 项目中隐藏控制台窗口

    我需要在应用程序启动期间创建一个没有控制台窗口或 任何其他窗口 的 exe 应用程序 为此我尝试了以下方法 使用 Visual Studio 2010 创建一个 Win32 控制台应用程序作为空项目 向项目添加头文件 stdafx h 向项
  • 如何使用 Selenium 和 C# 识别具有 href 属性的元素

    我在选择 href 时遇到问题 a title Population and Immigration Authority href https www gov il en Departments population and immigra
  • Android:notifyDataSetChanged被调用但ListView未更新

    我得到了这个 ListView 它是从网络上的 JSON 数据填充的 但是当我更新 JSON 条目时 例如添加新条目 ListView 不会更新 即使我已经调用了notifyDataSetChanged 它也不会在列表中显示新条目 这是我的
  • 在 System.Data.SQLite 中创建/使用用户定义的函数?

    用户定义的函数和整理序列 对用户定义函数和整理序列的完全支持意味着 在许多情况下 如果 SQLite 没有某个功能 您可以用您最喜欢的 NET 语言自行编写它 编写 UDF 和整理序列从未如此简单 我在 C 上发现了这一点SQLite AD
  • 如何找到 0 - 100 之间的质数?

    想要改进这篇文章吗 提供此问题的详细答案 包括引用和解释为什么你的答案是正确的 不够详细的答案可能会被编辑或删除 在 Javascript 中 我如何找到 0 100 之间的素数 我想过 但我不知道如何找到它们 我想过做 x x 但我发现了
  • 避免同一缓存区域的多次重新填充(由于并发)

    我有一个高流量的网站 我使用休眠 我还使用 ehcache 来缓存生成页面所需的一些实体和查询 问题是 并行缓存未命中 详细的解释是 当应用程序启动并且缓存区域变冷时 每个缓存区域都会被不同的线程填充多次 而不是仅一次 因为该站点被许多用户
  • ListView.getCheckedItemPositions 无法返回 SparseBooleanArray 中的选中项

    我正在尝试读取包含多个的列表中的联系人checkboxes 但是当我打电话给sparsebooleanarray 它只是为所有列表条目返回 false 即使对于已检查的条目也是如此 我调查了这个线程为什么 ListView getCheck
  • 为多个服务器加密 web.config 数据

    我需要为客户端加密 web config 文件的部分内容 我见过的大多数参考文献都是使用 aspnet regiis 进行加密 但是 据我所知 这需要在托管该站点的 Web 服务器上进行 这意味着每个服务器的加密值都不同 我无权访问该客户端
  • 我可以将嵌入的 Base64 编码字体转换为字体文件吗?

    我有一个 font face 规则 它看起来像这样 font face font family F src url format embedded opentype url data application x font woff char
  • 为什么 Python 枚举中的可变值是同一个对象?

    在尝试不同的值类型时Enum成员们 当值可变时 我发现了一些奇怪的行为 如果我定义一个值Enum作为不同的列表 成员的行为仍然与Enum值是典型的不可变类型 例如str or int 即使我可以更改现有成员的值 以便两个值Enum成员相同
  • AngularJS For 循环与数字和范围

    Angular 确实为在 HTML 指令中使用数字的 for 循环提供了一些支持 div do something div 但是 如果您的作用域变量包含具有动态数字的范围 那么您每次都需要创建一个空数组 在控制器中 var range fo
  • Pandas GroupBy:如何根据列获取前n个值

    如果这是一个基本问题 请原谅我 但我是熊猫新手 我有一个带有 A 列的数据框 我想根据 A 列中的计数获取前 n 行 例如 原始数据看起来像 A B C x 12 ere x 34 bfhg z 6 bgn z 8 rty y 567 hm
  • 在 BigQuery 中使用 _TABLE_SUFFIX 时无法识别名称“列”

    我有以下 BigQuery 数据集 一组带有名称的表keywords ab keywords ac keywords zz 另一组带有名称的表keywords different schema ab keywords different s
  • 异常:类型“String”不是类型“Map”的子类型

    异常 类型 String 不是类型 Map 的子类型 collection data id 1 name Marko picture https lh3 googleusercontent com a AAuE7mC1vqaKk Eylt
  • 无法在 kivy 中打开窗口[重复]

    这个问题在这里已经有答案了 我开始在我的游戏中使用 kivy 因此在遵循一些在线教程时 python shell 返回这些错误 INFO Logger Record log in C Users kivy logs kivy 18 10 2
  • Powershell - 用户映射 SQL Server 2012

    我正在尝试为不同的登录帐户编写用户映射脚本 我已经编写了用户和单个服务器角色的创建脚本 但我不知道如何使用 Powershell 设置用户映射 我还需要设置Database Role membership 尤其 db backupopera
  • 为什么 Fortran 代码会出现分段错误?

    下面的 Fortran 代码出现分段错误 但是 当我修改print pow 10 8 i to print pow j i 它可以正常工作 不会出现分段错误 为什么 这很奇怪 module mdl implicit none integer
  • Xcode 变量

    在 Xcode 中 我知道你可以获得诸如PROJECT DIR在某些情况下使用 例如运行脚本构建阶段 我想知道是否可以获得构建类型 即 Release or Debug 有任何想法吗 最好的来源可能是苹果官方文档 http help app
  • Java 泛型在子类化时不兼容类型

    从泛型类类型 形式类型参数进行子类化时T E具有有效的类类型 实际类型参数 例如Type String出现多种组合 令人困惑的是该使用哪一种以及何时使用 public class SubClass