vim*_*loc 1 java abstract-class typechecking
检查传递给方法的对象的最佳方法是扩展给定的类?
目前我有一个方法需要发送ByteBuffer数据和我编写的'播放器'类,并将数据排队到IO服务器上发送给客户端:
public void send(ButeBuffer toSend, Player player)
{
// prep the byte buffer for sending, and queue it on the IO server
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做的是让传入的玩家对象成为扩展玩家类的任何对象.我做了一些搜索,发现了这样的事情:
public void send(ByteBuffer toSend, Player<? extends Player> player)
{
// prep the byte buffer for sending, and queue it on the IO server
}
Run Code Online (Sandbox Code Playgroud)
但这给了我编译错误,我不明白到底发生了什么.这是正确的方法吗?如果是这样,任何人都可以解释这段代码具体做什么以及它为什么不起作用,或者将我链接到一篇更详细解释这一点的文章.
或者,我想我可以设置这样的东西:
public void send(ByteBuffer toSend, Object player)
{
// Check that the player extends Player, using instanceof or something
// along those lines
// Prep the ByteBuffer, and queue the data to send
}
Run Code Online (Sandbox Code Playgroud)
但是,与上面的代码相比,该代码对我来说有点脆弱.
我们非常欢迎任何帮助.谢谢 :)
| 归档时间: |
|
| 查看次数: |
149 次 |
| 最近记录: |