- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个通用容器类。两个类型参数代表所包含元素的类型,以及一个用于查找元素的键类型。该集合具有一个 get
方法,该方法接受一个键并返回一个元素。因此该类的第一个签名将如下所示:
class MyContainer<E, K> {
...
E get(K k) { ...}
...
}
现在我想将 K
限制为类,这些类本身就是我想要用作键的元素的容器。
这些包含键的类是通过实现某个接口(interface)来指定的:
interface ProvidesKeys<E> {...}
然后我有具体的实现,例如
class SomeInts implements ProvidesKeys<Integer> {...}
这意味着 SomeInts
从 Integer 中选择一些可以用作键的元素。
我想像这样使用MyContainer
:
MyContainer<String, SomeInts> myContainer = new MyContainer<>();
...
myContainer.get(3);
问题是 3
的类型是 Integer
,而不是 SomeInts
。所以我最初的 MyContainer 方案不再起作用了。
有没有办法限制 K
以便我可以访问 key 容器的元素类型?
我已经尝试过
class MyContainer<E, K extends ProvidesKeys<K> {....}
和通配符形式,但没有任何东西可以让我访问 key 提供类型的元素类型,所以我可以有一个正确的 E get(X k) {...}
方法!?我是否必须引入第三个类型变量来表示关键元素类型(尽管它隐含在关键容器中)?
最佳答案
Do I have to introduce a third type variable to represent the key element type (although it is implicit in the key container)?
是的。你做。除了将其作为参数之外,没有其他方法可以访问该类型。
关于java - 访问充当上限的通用集合的元素类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59038014/
在典型的 Scala 上界示例中 abstract class Animal { def name: String } abstract class Pet extends Animal {} c
我有 tstzrange 类型的列(带时区范围的时间戳),我只需要更新此值的上限或下限(并保持包含/排除边界) 我设法改变了 (-infinity,infinity) 与 UPDATE table S
我很好奇 GCD 问题。我正在参加 Coursera 算法工具箱类(class),它指出问题的天真解决方案是: for d from 1 to a+b: if d|a and d|b:
我需要知道是否有东西在两个限制之间,但我在 Playground 上不断遇到相同的 2 个错误,而且我似乎无法在网上找到解决方案。知道如何在 Swift 中做到这一点吗? var upperLimit
什么是快速计算 (long int) ceiling(log_2(i)) 的方法,其中输入和输出是 64 位整数?有符号或无符号整数的解决方案是可以接受的。我怀疑最好的方法是类似于找到的方法 here
lower_bound 是什么意思。如果我不得不猜测,我会回答这个函数在小于请求值的最后一个元素处返回迭代器。但我看到lower_bound 几乎和upper_bound 一样。唯一的区别是在 upp
我有一个曾经是 TreeView 控件的菜单,但现在我想让每个项目更加直观,并向树中的每个对象添加更多信息。 我的第一个意图是制作一个代表项目的用户控件,并在运行时将它们添加到面板中。这是一个好方法吗
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Limiting the max size of a HashMap in Java 当键数超过容量时,如何
我将 time_point 设置为下一个完整的五分钟,这很容易: template using minutes = std::chrono::duration, std::chrono::m
这个问题在这里已经有了答案: Upper bound vs lower bound for worst case running time of an algorithm (3 个答案) 关闭 7
这是代码。结果我得到“4 4”。不明白为什么不是“2 4”(根据下限和上限的定义)。 #include using namespace std; int main() { vector v
我必须检查一个包含平方根的不等式。为了避免由于 float 不准确和舍入导致的不正确结果,我使用 std::nextafter() 来获取上限/下限: #include // DBL_MAX #in
我想将一些小数点后两位的数字四舍五入为 1。然而我总是希望它能进入第一轮amount 列中的数字列表示例 140.08 = 140.1 141.63 = 141.7 如果我使用 round(141.6
我是 jfreechart 的菜鸟,我有一个应用程序可以创建一个运行良好的简单条形图。问题是,我希望所有图表显示 1 到 10 的范围。当图表中的最高值低于该值时,较低的值将成为图表的上限,并且将以不
我对支持向量机有一个担忧,即它们的分类分数:这些分类分数有上限吗?我认为不是,因为 SVM 只是一个超平面,而分数基本上是一个点到该超平面的距离。如果没有限制,一个点可以位于空间中的任何位置,因此距离
我有一个网页,我想将其设计为看起来像一本打开的书,中间有一个折痕/阴影。页面的高度不是固定的,而是灵活的,随着内容的增长而增长。 body 元素具有纸张纹理的背景图像,没有任何阴影。 对于阴影,我的设
如何在运行时更改 python for 循环的上限? 代码, from random import randint lower_limit = 0 higher_limit = 200 step_si
我正在尝试构建一个函数: 接受长度为 n 的正整数列表作为参数, 返回所有长度为 n 的列表,这些列表由具有以下属性的非负整数组成: 对于列表 lst 它认为对于所有索引 i,lst[i] ≤ upp
我正在尝试查询我的数据库 ratingsChoices= m$distinct({'answers'}) 但我收到了一个警告:错误:明显太大,16mb 上限 在 mongolite 中有解决这个错误的
我有一个 Mongodb 集合。简单地说,它有两列:用户和网址。它有 39274590 行。该表的键是 {user, url}。 使用 Java,我尝试列出不同的 url: MongoDBMana
我是一名优秀的程序员,十分优秀!