generics - 泛型作为构造函数的参数
问题描述
我正在查看一个包含多个条目的代码,如下所示:
open class PersonFinder: Finder<Long, Person>(Person::class.java)
open class CompanyFinder: Finder<Long, Company>(Company::class.java)
open class DeviceFinder: Finder<Long, Device>(Device::class.java)
我想要一种更通用的方法,如下所示:
open class GenericFinder<T>: Finder<Long, T>(T::class.java)
但语法无效,因为 T::class.java 不能用作那里的参数。有什么方法可以让我以惯用的方式完成这项工作吗?
解决方案
我想我找到了解决方案:
open class GenericFinder<T>(type: Class<T>) : Finder<Long, T>(type)
推荐阅读
- html - TD图标右上对齐 怎么办?
- c# - 在 c# 中使用 StackExchange.Redis 执行 GET 时出现错误超时?
- r - 一个单元格包含在 R 中的另一个单元格中
- java - 如何在 Java 中为不同的对象创建单独的列表?
- java - 使用 spark-submit 部署应用程序:应用程序已添加到调度程序但尚未激活
- xamarin.ios - xamarin.ios 上的滚动视图无法使用情节提要
- powershell - 'quser' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
- c# - WindowsIdentity:异常:System.Security.SecurityException:用户名或密码不正确
- php - Laravel 发布请求
- php - 在 Woocommerce 3 中使用 WC_Cart add_to_cart() 方法存储自定义数据