首页 \ 问答 \ 泛型是多态的一种形式吗?(Are generics a form of polymorphism? [closed])

泛型是多态的一种形式吗?(Are generics a form of polymorphism? [closed])

我们可以说泛型是多态的形式吗?


Can we say generics are form of polymorphism?


原文:https://stackoverflow.com/questions/4045945
更新时间:2023-07-09 16:07

最满意答案

我不确定你在找什么奇怪的东西。 您有两个 TreeSet实例,每个实例都有自己的CompareID实例作为Comparator ,每个CompareID实例都维护它自己的计数器。

因此,看到每个计数器值(0,1,2等)出现两次并不奇怪。 至于计数器值的出现顺序,取决于TreeSet的内部实现。

至于

等于一组树是否是正确的输出?

是的,两个集都包含相同的元素。 订单无关紧要。 为了澄清 - 如果对于TreeSet的某个元素ycompare(x,y)==0containscontainsAll TreeSet的所有方法都会考虑将元素x包含在TreeSet ,其中compare是所提供的Comparatorcompare方法。 因此,在你的例子中,只有id属性决定两个元素是否相等。

以下情形解释了输出:

0 // compare method of 1st CompareID object executed
1 // compare method of 1st CompareID object executed
2 // compare method of 1st CompareID object executed
3 // compare method of 1st CompareID object executed
0 // compare method of 2nd CompareID object executed
1 // compare method of 2nd CompareID object executed      
2 // compare method of 2nd CompareID object executed
3 // compare method of 2nd CompareID object executed
4 // compare method of 1st CompareID object executed
5 // compare method of 1st CompareID object executed
6 // compare method of 1st CompareID object executed
7 // compare method of 1st CompareID object executed
8 // compare method of 1st CompareID object executed
4 // compare method of 2nd CompareID object executed
5 // compare method of 2nd CompareID object executed
6 // compare method of 2nd CompareID object executed
7 // compare method of 2nd CompareID object executed
8 // compare method of 2nd CompareID object executed

编辑:首先,您要向第一个TreeSet添加元素(因此compare第一个Comparator连续多次调用),然后您将元素添加到第二个TreeSet (因此compare第二个Comparator连续多次调用),然后你调用treeSetA.containsAll(treeSetB) ,它导致第一个Comparator被连续多次调用,最后你调用treeSetB.containsAll(treeSetA) ,这导致第二个Comparator被多次调用一排。


I'm not sure what you are finding strange. You have two TreeSet instances, each having its own CompareID instance serving as Comparator, and each CompareID instance maintains it's own counter.

Therefore it's not a surprise to see each counter values (0,1,2,etc...) appearing twice. As for the order of appearance of the counter values, that depends on the internal implementation of TreeSet.

As for

Equal set of tree // is that correct output ?

Yes, both sets contain the same elements. The order doesn't matter. To clarify - the methods contains and containsAll of TreeSet consider an element x to be contained in the TreeSet if compare(x,y)==0 for some element y of the TreeSet, where compare is the compare method of the supplied Comparator. Therefore, in your example, only the id property determines if two elements are equal.

The following scenario explains the output :

0 // compare method of 1st CompareID object executed
1 // compare method of 1st CompareID object executed
2 // compare method of 1st CompareID object executed
3 // compare method of 1st CompareID object executed
0 // compare method of 2nd CompareID object executed
1 // compare method of 2nd CompareID object executed      
2 // compare method of 2nd CompareID object executed
3 // compare method of 2nd CompareID object executed
4 // compare method of 1st CompareID object executed
5 // compare method of 1st CompareID object executed
6 // compare method of 1st CompareID object executed
7 // compare method of 1st CompareID object executed
8 // compare method of 1st CompareID object executed
4 // compare method of 2nd CompareID object executed
5 // compare method of 2nd CompareID object executed
6 // compare method of 2nd CompareID object executed
7 // compare method of 2nd CompareID object executed
8 // compare method of 2nd CompareID object executed

EDIT : First you are adding elements to the first TreeSet (hence compare of the first Comparator is called several times in a row), then you are adding elements to the second TreeSet (hence compare of the second Comparator is called several times in a row), then you call treeSetA.containsAll(treeSetB) which causes compare of the first Comparator to be called several times in a row, and finally you call treeSetB.containsAll(treeSetA) which causes compare of the second Comparator to be called several times in a row.

相关问答

更多
  • 你不能像这样引用一个尚未完成创建的对象,但听起来你想在这里使用一个getter: var pizzaOrder = { id: "pizza", counter: 0, get sentence() { return this.id + this.counter; } }; pizzaOrder.counter++; console.log(pizzaOrder.sentence); You can't reference an object that hasn' ...
  • 您的代码在特定测试中运行良好。 这并不意味着它总能正常工作。 尝试迭代很多次,你可能会开始看到异常。 您的测试有点像,如果您测试过一座桥可以通过搭载一辆车在桥上支撑20辆卡车。 它没有展示任何东西。 并且使用测试证明代码是线程安全的几乎是不可能的。 只有仔细阅读和理解所有潜在的问题才能保证。 一个非线程安全的程序可能会工作多年,并突然有一个bug。 为了使您的计数器安全,请使用AtomicInteger。 编辑: 另外,正如@SpringRush所说,你不是在线程之间共享一个计数器。 每个线程都有自己的计数 ...
  • 你传递的是counter的值 ,而不是变量本身。 您的递归调用不能修改外部调用中counter的值。 Java是通过价值传递的。 一个可能的解决方案是做 counter = foo(num, counter); 在递归调用中。 或者,也可以return foo(num, counter) ,因为除了返回它之外,你不会对任何事情做任何事情。 You're passing the value of counter, not the variable itself. Your recursive call c ...
  • p > 4背后的逻辑是什么? 如果你确定知道有4人需要输入,你可以使用一个简单的for循环。 for (int i = 0; i < 4; i++) { System.out.print("Enter the best time available for person " + (i+1) + "."); p=kbd.nextInt(); if (p == 1) morning++; else if (p == 2) noon++; else if (p == 3) ...
  • Collections.sort被实现为mergesort。 从源头上看,所有比较条件都是>0或<=0 ,偶然地将负面情况视为与情况相同。 不同的排序实现可能会失败。 Per jbellis的评论,“ 这不完全是”只是运气“,尽管 - Collections.sort保证是”稳定的“,这意味着相同的元素必须在排序后处于相同的相对顺序。我不确定这是不可能的拿出一个稳定的排序实现,但是这个比较失败了,但我想不出我的头顶。 “ private static void mergeSort(Object[] src, ...
  • 你测试i1 i2,而是i2> i1。 将您的比较器更改为 return i1 < i2 ? -1 : (i1 > i2) ? 1 : 0; 添加 为了对另外两个答案做出新的贡献,这两个答案说了同样的话,并且让我大吃一惊,大多数包装类都内置了compareTo()方法,可以让您免于任何想法。 比如你的比较器可以调用Integer.compareTo(),即 return i1.compareTo(i2); You test for i1 < i2, and, i ...
  • 像这样的东西应该工作: for ($i = 3; $i <= 14; ++$i) { if (empty(${"f$i"})) { ${"f$i"} = ${"bla$i"}(); } } 最重要的是,您可以使用${"f$i"}来查找变量变量。 Something like this should work: for ($i = 3; $i <= 14; ++$i) { if (empty(${"f$i"})) { ${"f$i"} = ${"bla ...
  • 要使某个类变量而不是实例变量,我们需要将其设置为static 。 有关static变量的更多信息以及它们与常规变量的不同之处: https : //en.wikipedia.org/wiki/Static_variable TLDR:你的第一个解决方案是正确的,尽管我认为它应该读取private static int counter = 0; To make something a class variable rather than an instance variable, we need to mak ...
  • 我猜你试图找到这两个数组的匹配数,但是相等元素的相对索引应该是不同的,这样我们就可以将它们添加到伪计数器机制中。 所以看看这个方法并尝试一下: public int psuedoMatch(int[] guess) { int psuedoCount = 0; if (lotteryNumbers.length != guess.length) return 0; int size = lotteryNumbers.length; // Determin ...
  • 我不确定你在找什么奇怪的东西。 您有两个 TreeSet实例,每个实例都有自己的CompareID实例作为Comparator ,每个CompareID实例都维护它自己的计数器。 因此,看到每个计数器值(0,1,2等)出现两次并不奇怪。 至于计数器值的出现顺序,取决于TreeSet的内部实现。 至于 等于一组树是否是正确的输出? 是的,两个集都包含相同的元素。 订单无关紧要。 为了澄清 - 如果对于TreeSet的某个元素y , compare(x,y)==0则contains和containsAll Tr ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)