从方法返回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)
jquery addClass - 等待,延迟,速度,超时或其他(jquery addClass - wait, delay, speed, timeout or other)
在初始化之前设置延迟或超时(Set delay or timeout before initializing)
在ssrs中延迟通知问题(Delay notification issue in ssrs)
使用jquery延迟(using jquery delay)
Ajax jquery需要加载超时或延迟(Need a timeout or delay on loading, Ajax jquery)
jQuery:具有通知功能的超时/延迟?(jQuery: Timeout/Delay with a notification function?)
jQuery超时功能无法正常工作(jQuery Timeout Function not working)
使用jquery设置延迟或超时(Setting delay or timeout with jquery)
相关文章
更多Guava Objects类详解
java通用返回对象
Solr Cache使用介绍及分析,包括LRUCache、filterCache、queryResultCache、documentCache、Generic Caches
java分页返回对象
研磨设计模式之工厂方法模式(Factory Method)-场景问题
研磨设计模式之工厂方法模式(Factory Method)-模式讲解2
Java 方法
研磨设计模式之工厂方法模式(Factory Method)-模式讲解1
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!