首页 \ 问答 \ 为什么Java在泛型中有下限?(Why does Java have lower bounds in generics?)

为什么Java在泛型中有下限?(Why does Java have lower bounds in generics?)

我正在尝试设计自己的编程语言,并且正在考虑泛型。 我已经在Java工作了很长一段时间,并且知道extendssuper泛型边界。

我正在阅读这篇文章并试图了解下限的必要性。

在我的语言中,我打算像常规字段一样进行泛型,如果你说List<MyObject> ,你可以存储MyObjectMyObject任何子类型。 有道理吗?

在帖子上,它们具有以下类层次结构:

class Person implements Comparable<Person> {
  ...
}

class Student extends Person {
  ...
}

然后他们有一个排序方法:

public static <T extends Comparable<T>> void sort(List<T> list) {
  ...
}

我的想法是,你应该能够将List<Student>发送给这个方法。 当Student扩展Personcompare方法将由它的超类Person

出现错误消息的原因是编译器将sort方法的type参数推断为T:= Student,并且该类Student不是Comparable<Student> 。 它是Comparable<Person> ,但是不符合方法排序的类型参数的界限所强加的要求。 要求T (即Student )是Comparable<T> (即可Comparable<Student> ),实际上它不是。

以上对我没有任何意义......你应该能够做student.compare(person) ,为什么这不起作用?

也许它说Student应该实施它自己的可比方法,以便Student在比较中有发言权? 你不需要做任何特殊的事情,只需覆盖Person的方法。 您将无法保证与另一位Student进行比较,但可以使用instanceof进行检查。

这里有什么我想念的吗?

毕竟这个想法,我现在想知道extends的目的是什么。 根据我的理解,在List<MyType> ,您只能放入MyType ,而不能放入任何子类。 如上所述,这对我没有任何意义,你应该能够像字段一样将任何子类放在列表中。

我应该说清楚这一点,它不是“为什么它不能在Java中工作”,而是“为什么它在泛型理论中不起作用”。 我只是标记了java,因为这是我进行比较的地方。


I'm trying to design my own programming language, and am thinking about generics. I've been doing Java for quite a while now and know about the extends and super generic bounds.

I'm reading this post and trying to understand the need for the lower bounds.

In my language, I am planning to do generics the same way as a regular field, if you say List<MyObject>, you can store either a MyObject, or any subtype of MyObject. Makes sense right?

On the post, they have the following class hierarchy:

class Person implements Comparable<Person> {
  ...
}

class Student extends Person {
  ...
}

They then have a sort method:

public static <T extends Comparable<T>> void sort(List<T> list) {
  ...
}

What I think, is that you should be able to send a List<Student> to this method. As a Student extends Person, the compare method would be handled by it's superclass, Person.

The reason for the error message is that the compiler infers the type parameter of the sort method as T:=Student and that class Student is not Comparable<Student> . It is Comparable<Person> , but that does not meet the requirements imposed by the bound of the type parameter of method sort. It is required that T (i.e. Student ) is Comparable<T> (i.e. Comparable<Student> ), which in fact it is not.

The above doesn't make any sense to me...you should be able to do student.compare(person), so why doesn't this work?

Maybe it's saying that Student should implement it's own comparable method so that Student has a say in the comparison? You don't need to do anything special, just override Person's method. You won't be able to guarantee you are comparing to another Student, but that can be checked with instanceof.

Is there something I'm missing here?

And after all this thinking, I'm now wondering what the purpose of extends is. From my understanding, in a List<MyType>, you can only put a MyType in, not any of it's subclasses. As mentioned above, this doesn't make any sense to me and you should be able to put any subclass in the list like a field.

I should probably make this clear, it's not "why doesn't it work in Java", but "why doesn't it work in generics theory". I just tagged java because that is where I'm making my comparisons.


原文:https://stackoverflow.com/questions/35714431
更新时间:2023-04-08 09:04

最满意答案

这就是TCP的工作原理。 TCP连接是双向字节流 ,您必须将其视为这样。 从一端单个发送可能导致接收端的多次读取,反之亦然,多次发送可能最终在一次读取中,并且传输不保留应用程序消息边界。

您必须缓冲输入,直到您知道有完整的应用程序消息。 常见的方法是:

  • 固定长度的消息,
  • 消息前面的预先挂起的长度,
  • 使用特殊的“消息结束”分隔符分隔流。

This is how TCP works. TCP connection is a bi-directional stream of bytes, and you have to treat it as such. Single send from one end might result in multiple reads on the receiving side, and vice versa, multiple sends might end up in a single read, and application message boundaries are not preserved by the transport.

You have to buffer the input until you know you have a complete application message. Common approaches are:

  • fixed length messages,
  • pre-pending length in front of the message,
  • delimiting the stream with special "end-of-message" separator.

相关问答

更多
  • 好的,我去了一个大的缓冲区并分批发送,中间有一个休眠间隔来复制'并非所有收到的字节'所以我上面的代码没有收到所有的字节。 对于那些也使用ReceiveAsync(..)的人来说,这是我的代码 private byte[] ReadBySize(int size = 4) { var readEvent = new AutoResetEvent(false); var buffer = new byte[size]; //Receive buffer ...
  • 不要一起使用非阻塞和异步。 没有用。 异步本质上已经是非阻塞的,并且可以自行运行。 如果将套接字设置为非阻塞,则每次接收尝试都将立即完成。 使用数据或使用SocketError.WouldBlock 。 坦率地说,没有.NET程序应该使用非阻塞套接字。 正确使用非阻塞套接字的唯一方法是轮询套接字,并且鉴于对.NET提供的异步操作的良好支持,轮询是浪费且不必要的。 Don't use non-blocking and asynchronous together. It's pointless. Asynchr ...
  • 严格来说,是的。 该实现可以随意丢弃它希望的任何地方的数据包。 但是,如果它没有提供一些合理数量的缓冲,那将是一个相当糟糕的实现,而Linux也是如此。 Strictly speaking, yes. The implementation is free to discard packets any place it wishes to. However, it would be a pretty poor implementation if it didn't provide some reasonabl ...
  • 解决了这个问题。 因为缓冲区大小设置得太大。 “Hello World”仅包含12个字符。 所以我应该将Buffer size设置为12. readEventArgs.SetBuffer(new byte [12],0,12); Have solved this. because the buffer size is set too large. "Hello World" only contains 12 chars. so I should set the Buffer size as 12. read ...
  • 如果您正在从原始套接字读取数据包(如源代码中所示),则可以轻松读取来自同一套接字的所有数据包。 确保这是你打算做的。 原始套接字用于进行数据包检查,以进行故障排除,取证,安全或教育目的。 您无法通过这种方式轻松与其他系统进行通信。 同样,接收调用在此不会因协议而有所不同,因为您实际上并未使用 TCP或UDP,而只是接收这些协议构建和解码的原始数据包。 (1)如果我想读取TCP和UDP数据包,我应该使用recv()还是recvfrom()? 任何一个都可以工作。 recv()将只返回实际的数据包数据,而rec ...
  • 难道你不能只是做一个recvfrom()到一个临时缓冲区并放弃缓冲区? Can't you just do a recvfrom() into a temporary buffer and discard the buffer?
  • 在MessageReceiver类中提供新库中的ReceiveAsync方法: var messageReceiver = new MessageReceiver(SBConnString, QueueName, ReceiveMode.PeekLock); Message message = await messageReceiver.ReceiveAsync(); 使用MessageSender和MessageReceiver从服务总线队列开始发送和接收消息,请参阅完整示例。 In the new l ...
  • 这就是TCP的工作原理。 TCP连接是双向字节流 ,您必须将其视为这样。 从一端单个发送可能导致接收端的多次读取,反之亦然,多次发送可能最终在一次读取中,并且传输不保留应用程序消息边界。 您必须缓冲输入,直到您知道有完整的应用程序消息。 常见的方法是: 固定长度的消息, 消息前面的预先挂起的长度, 使用特殊的“消息结束”分隔符分隔流。 This is how TCP works. TCP connection is a bi-directional stream of bytes, and you have ...
  • 它应该工作正常。 只要你绑定到IN6ADDR_ANY ,然后加入组播组,你应该能够发送和接收单播数据包没有问题。 使用多播时,绑定到IN6ADDR_ANY (或IPv4的INADDR_ANY )很重要。 如果绑定到特定接口,则会破坏Linux系统上的多播。 It should work fine. As long as you bind to IN6ADDR_ANY, then join the multicast groups, you should be able to send and receive ...
  • 我在搜索你的问题时找到了这个。 我没试过这个。 可能这会起作用。 int v=0; v = PACKET_MASK_ANY & ~(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))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。