从方法返回Java有界通用对象(Returning Java bounded generic objects from method)
我知道Oracle教程和问题, 如何使方法返回类型通用? 但是我仍然无法从Java方法返回通用对象。
简要示例:我有一个网络
Packet
的层次结构和Handler
的层次结构,参数Packet
它们处理的Packet
。 最后我有一个Handler
的注册表 ,其中包含一个方法,它会返回给定数据包的正确处理程序。我想实现所有这一切,理想情况下没有警告手动压制。
class Packet {} class ThisPacket extends Packet {} class ThatPacket extends Packet {} interface PacketHandler<P extends Packet> { boolean handle(P p); } class ThisPacketHandler extends PacketHandler<ThisPacket> { boolean handle(ThisPacket p); } class ThatPacketHandler extends PacketHandler<ThatPacket> { boolean handle(ThatPacket p); }
这是非常规则的我相信,在我的实现中,我在中间有一些进一步的抽象类来塑造我的层次结构,但我认为现在可以忽略它。
关键部分是i)注册表:
class HandlersRegistry { static <<RETURN TYPE>> getHandler(Packet p) { if (p instanceof ThisPacket) return new ThisPacketHandler(); if (p instanceof ThatPacket) return new ThatPacketHandler(); return null; } } <<RETURN TYPE>> OPTIONS (I tried): // Raw-type Warning: A. PacketHandler // the registry user won't be able to use the handler: B. PacketHandler<? extends Packet> // Type mismatch: cannot convert from C. PacketHandler<Packet>
..和ii)和注册用户:
class HandlerSwitchExample { public static void main() { // [...] <<OBJECT TYPE>> handler = HandlersRegistry.getHandler(somePacket); handler.handle(somePacket); } }
希望这个例子相当清楚。 感谢任何有用的建议,甚至完整的重新设计策略。
I am aware of Oracle tutorials and questions like How do I make the method return type generic? but still I am having trouble returning generic objects from a Java method.
Brief example: I have a hierarchy of network
Packet
s and a hierarchy ofHandler
s, parametrized to thePacket
they handle. Eventually I have a registry ofHandler
s which includes a method to that would return me the proper handler of a given packet.I would like to implement all of this with ideally no warning to manually suppress.
class Packet {} class ThisPacket extends Packet {} class ThatPacket extends Packet {} interface PacketHandler<P extends Packet> { boolean handle(P p); } class ThisPacketHandler extends PacketHandler<ThisPacket> { boolean handle(ThisPacket p); } class ThatPacketHandler extends PacketHandler<ThatPacket> { boolean handle(ThatPacket p); }
This is quite regular I believe, in my implementation I have some further abstract classes in the middle to shape my hierarchy, but I think this can be ignored by now.
The critical part is i) the registry:
class HandlersRegistry { static <<RETURN TYPE>> getHandler(Packet p) { if (p instanceof ThisPacket) return new ThisPacketHandler(); if (p instanceof ThatPacket) return new ThatPacketHandler(); return null; } } <<RETURN TYPE>> OPTIONS (I tried): // Raw-type Warning: A. PacketHandler // the registry user won't be able to use the handler: B. PacketHandler<? extends Packet> // Type mismatch: cannot convert from C. PacketHandler<Packet>
..and ii) and the registry user:
class HandlerSwitchExample { public static void main() { // [...] <<OBJECT TYPE>> handler = HandlersRegistry.getHandler(somePacket); handler.handle(somePacket); } }
Hope the example is fairly clear. Thanks for any helpful suggestion, even complete redesign strategies.
原文:https://stackoverflow.com/questions/29119731
最满意答案
没有什么明显的。 我会尝试修剪代码,直到它按预期下滑。 删除回调,删除html-set,删除成功/警告类设置器,在输出到控制台之前选择note元素,用立即显示替换幻灯片等。
首先尝试在注释上调用
.stop(true,true)
:note.stop(true,true).slideDown();
。 这是因为它忙于其他一些动画并且正在排队下滑。Nothing obvious there. I would try trimming the code down until it slides down as expected. Remove the callback, remove the html-set, remove the success/warning class-setters, select the note-element before outputting to the console, replace the slide with an immediate show, etc.
Also try calling
.stop(true,true)
on the note first:note.stop(true,true).slideDown();
. This is in case it is busy with some other animation and the slide down is being queued.
相关问答
更多-
悬停时停止通知超时(Stop notification timeout on hover)[2023-10-21]
window.setTimeout()返回一个整数timerID,可以与window.clearTimeout() : var notificationTimer; function createTimer() { notificationTimer = setTimeout(function(){ alert('hi'); }, 10000); } function clearTimer() { clearTimeout(notificationTimer); } 另见: https : / ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
jquery addClass - 等待,延迟,速度,超时或其他(jquery addClass - wait, delay, speed, timeout or other)[2022-12-14]
我不明白为什么hoverIntent不起作用: $("#menu ul li").hoverIntent({ sensitivity: 3, // number = sensitivity threshold (must be 1 or higher) interval: 200, // number = milliseconds for onMouseOver polling interval timeout: 500, // number = millis ... -
你有这个: $(document).ready(function() { 把这个: $(document).ready(function() { setTimeout(function() { 然后你有这个: }); // wrapper for background animation functionality var BG = { 把这个: }, 2000); }); // wrapper for background animation functionality var ...
-
在ssrs中延迟通知问题(Delay notification issue in ssrs)[2022-01-29]
标准IT修复。 关闭电脑,然后再打开 Standard IT fix. Turn your PC off and on again -
使用jquery延迟(using jquery delay)[2023-07-04]
delay()只能用于动画,而不能用于像removeClass这样的函数,因为你需要一个timeOut。 不仅如此,Ajax调用也是异步的,所以您的类会立即被删除,并且不会等待Ajax调用完成。 你总是可以这样做: $(function(){ $(document).on('click', '.plus', function(e){ e.preventDefault(); var self = this, existingPath = $(t ... -
$('body').delay(2000).queue(function( nxt ) { $(this).load('home.php'); nxt(); }); $('body').delay(2000).queue(function( nxt ) { $(this).load('home.php'); nxt(); });
-
没有什么明显的。 我会尝试修剪代码,直到它按预期下滑。 删除回调,删除html-set,删除成功/警告类设置器,在输出到控制台之前选择note元素,用立即显示替换幻灯片等。 首先尝试在注释上调用.stop(true,true) : note.stop(true,true).slideDown(); 。 这是因为它忙于其他一些动画并且正在排队下滑。 Nothing obvious there. I would try trimming the code down until it slides down as ...
-
你不能只调用clearTimeout() (顺便说一句,它不是JQuery的一部分),你必须为它提供一个你要取消的计时器的标识符。 此外, setTimeout()和clearTimeout()不是JQuery或JavaScript的一部分。 它们是window对象的方法,由浏览器提供。 它们不是语言(JavaScript)或库(JQuery)的一部分。 另外, JQuery .hover()方法接受2个参数,你提供4.我已将它们组合在下面,但不知道你想要做什么,你可能需要调整它。 $(document). ...
-
你可以这样做: $('.showmenu') .bind("mouseenter", function (e) { if($(this).data("timer")) clearTimeout($(this).data("timer")); $('ul.secondul').show(); }) .bind("mouseleave", function (e) { $(this).data("timer", setTimeout(function(){ ...