Java:从 ByteBuffer 扩展

发布于 2024-12-15 02:49:43 字数 348 浏览 2 评论 0原文

Java 不允许我从 ByteBuffer 扩展,因为它实现了我没有重写的抽象方法,但这些方法适用于创建的任何 ByteBuffer 对象,例如 asDoubleBuffer()...

byte[] bytes = new byte[256];
ByteBuffer buf = ByteBuffer.wrap(bytes);
DoubleBuffer db = buf.asDoubleBuffer();

但是,如果我从 ByteBuffer 扩展一个新类,它迫使我实现 asDoubleBuffer(),即使超类已经实现了这个方法,显然,因为我可以很好地调用它。我完全不明白这里发生了什么...请向我解释一下。

Java will not allow me to extend from ByteBuffer because it implements abstract methods that I am not overriding, yet these methods work for any ByteBuffer objects that are created, such as asDoubleBuffer()...

byte[] bytes = new byte[256];
ByteBuffer buf = ByteBuffer.wrap(bytes);
DoubleBuffer db = buf.asDoubleBuffer();

Yet, if I extend a new class from ByteBuffer, it forces me to implement asDoubleBuffer(), even though the superclass already implements this method, obviously, since I can call it just fine. I'm totally not understanding what's going on here... Please explain this to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘴硬脾气大 2024-12-22 02:49:43

ByteBuffer 工厂方法返回一个实现 ByteBuffer 的类。

byte[] bytes = new byte[256];
ByteBuffer buf = ByteBuffer.wrap(bytes);
System.out.println(buf.getClass());

使用 ByteBuffer 是一个相对高级的工具,恕我直言

class java.nio.HeapByteBuffer

,在尝试扩展它之前,您需要很好地了解 Java。


通过查看代码就可以看到这一点。在我的 IDE 中,您可以通过执行+找到它。关于方法。

public static ByteBuffer wrap(byte[] array) {
    return wrap(array, 0, array.length);
}

顺便说

public static ByteBuffer wrap(byte[] array,
                                int offset, int length)
{
    try {
        return new HeapByteBuffer(array, offset, length);
    } catch (IllegalArgumentException x) {
        throw new IndexOutOfBoundsException();
    }
}

一句:我非常喜欢使用 ByteBuffer,但是您确实需要了解自己在做什么才能使用它。 esp 当从 ByteBuffer 解析数据时。拥有十年经验的开发人员发现它使用起来很棘手。

The ByteBuffer factory method returns a class which implements ByteBuffer.

byte[] bytes = new byte[256];
ByteBuffer buf = ByteBuffer.wrap(bytes);
System.out.println(buf.getClass());

prints

class java.nio.HeapByteBuffer

Using ByteBuffer is a relatively advanced tool, you need to understand Java pretty well before you try to extend it IMHO.


You can see this by looking at the code. In my IDE you can find it by doing a <shift>+<click> on the method.

public static ByteBuffer wrap(byte[] array) {
    return wrap(array, 0, array.length);
}

calls

public static ByteBuffer wrap(byte[] array,
                                int offset, int length)
{
    try {
        return new HeapByteBuffer(array, offset, length);
    } catch (IllegalArgumentException x) {
        throw new IndexOutOfBoundsException();
    }
}

BTW: I am a big fan of using ByteBuffer, however you really need to understand what you are doing to use it. esp when parsing data from a ByteBuffer. Developers with ten years experience find it tricky to use.

寂寞花火° 2024-12-22 02:49:43

没关系。我看到当分配一个新的 ByteBuffer 时,它会返回一个使用这些抽象方法的子类。创建的对象根本不是真正的 ByteBuffer。让我感到困惑。不确定他们为什么会使用这种方法。

Nevermind. I see that when a new ByteBuffer is allocated it returns a subclass that uses those abstract methods. The object created is not really a ByteBuffer at all. Confusing to me. Not sure why they would use this methodology.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文