静态方法继承

发布于 2024-10-08 04:02:08 字数 615 浏览 1 评论 0原文

我有一个父类,其中包含从数据库写入和读取它的方法。问题是我有一个我想概括的静态方法。这样做的最佳实践是什么?我提出了一些解决方案,但都不适用于静态方法。

有没有办法强制子对象实现抽象方法?

家长班:

public class Data_object  {

    public byte[] toByteArray() throws IOException  {
        return null;
    }
    public static Data_object fromByteArray(byte[] data){
        return null
    }
}

孩子班:

public class ModelObject extends Data_object {

    public static Data_object fromByteArray(byte[] data){
        ModelObject result = new ModelObject();
        //set data from byte arrray
        return result;
    }
}

谢谢

I have a parent class with methods for writing and reading it from the db. The problem is I have a static method which I want to generalise. What is the best practice for doing this? I've came up with some solutions but all don't work with the static method.

Is there a way to force the childobject to implement the abstract method?

Parent Class:

public class Data_object  {

    public byte[] toByteArray() throws IOException  {
        return null;
    }
    public static Data_object fromByteArray(byte[] data){
        return null
    }
}

Child class:

public class ModelObject extends Data_object {

    public static Data_object fromByteArray(byte[] data){
        ModelObject result = new ModelObject();
        //set data from byte arrray
        return result;
    }
}

thanks

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

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

发布评论

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

评论(3

下壹個目標 2024-10-15 04:02:08

Java 中的静态方法不能是抽象,并且您不能重写它。所以你不能强迫子类实现某个静态方法。

唯一的出路是按照约定要求静态方法(如随附的 JavaDoc 中的注释)并使用反射来确保在运行时(或构建时)子类实现所需的静态方法。


顺便说一句 - 这是一种常见的“模式”。 Serialized 没有定义任何方法,但它的文档提到了三种方法:

private void writeObject(java.io.ObjectOutputStream out)
   throws IOException
private void readObject(java.io.ObjectInputStream in)
   throws IOException, ClassNotFoundException;
private void readObjectNoData() 
   throws ObjectStreamException;

“对象序列化框架”现在使用反射来测试这些方法是否由类实现,如果是,则调用这些方法。

但我建议在使用此策略之前寻找其他解决方案。

A static method in Java can't be abstract and you can't override it. So you can't force a subclass to implement a certain static method.

The only way out is to require the static method by convention (like a note in the accompagnying JavaDoc) and using reflection to ensure at runtime (or at build time), that the subclass implements the required static method.


BTW - this is a common "pattern". Serializable does not define any methods, but it's documentation mentions three methods:

private void writeObject(java.io.ObjectOutputStream out)
   throws IOException
private void readObject(java.io.ObjectInputStream in)
   throws IOException, ClassNotFoundException;
private void readObjectNoData() 
   throws ObjectStreamException;

The "object serialization framework" now uses reflection to test, if those methods are implemented by a class and, if yes, invokes those methods.

But I recommend looking for other solutions before using this strategy.

梦初启 2024-10-15 04:02:08

静态方法不能被重写。您只能通过在子类中定义具有相同名称和签名的方法来隐藏超类方法,但这很混乱,因此通常不建议这样做。原始超类方法仍然可以通过 ((SuperClassName) subClassInstance).staticMethod() 等方式访问。

在您的情况下,最好将该方法设为非静态。

Static methods can't be overridden. You can only hide superclass methods by defining a method with same name and signature in the subclass, but it's quite confusing and therefore generally not recommended. The original superclass method could be still accessed by, for example, ((SuperClassName) subClassInstance).staticMethod().

In your case it's probably better to make the method non-static.

木槿暧夏七纪年 2024-10-15 04:02:08

继承不适用于这种方式的静态方法。您可以在特定类上调用静态方法,如下所示:

MonkeyModelObject.fromByteArray(data)

即使您从类“内部”执行此操作,您仍然在幕后调用该特定类上的特定方法。因此,如果您“覆盖”静态方法,您只是在另一个类(子类)中创建一个具有相同名称的新方法。静态方法没有运行时方法选择。

对于您正在处理的特定情况,您似乎正在对字节数组进行序列化。也许您正在序列化来自网络流的消息,或者类似的东西?我推荐以下方法,也许这个伪代码可以启发你:

abstract class Message
{
    void write(stream)
    {
        stream.write(getMessageTypeCode());
        writeParameters(stream);
    }
    abstract int getMessageTypeCode();
    abstract void writeParameters(stream);
    abstract void readParamters(stream);
}
class ChatMessage
{
    String text;
    int getMessageTypeCode() { return 1; }
    void writeParameters(stream)
    {
        stream.write(text);
    }
    void readParameters(stream)
    {
        text = stream.read();
    }
}
class MessageDecoder
{
    Message decode(stream)
    {
        int type = stream.read();
        message = createMessage(type);
        message.readParameters(stream);
        return message;
    }
    Message createMessage(int type)
    {
        if (type == 1)
        {
            return new ChatMessage();
        }
        throw new error;
    }
}

Inheritance does not apply to static methods that way. You call a static method on a specific class, like this:

MonkeyModelObject.fromByteArray(data)

Even when you do this from "inside" a class, you're behind-the-scenes still invoking that specific method on that specific class. So if you "override" a static method, you're just making a new method with the same name in another class (the child class). There's no run-time selection of methods for static methods.

For the specific case that you're working on, it seems like you're doing serialization from/to byte arrays. Perhaps you're serializing messages from a network stream, or something like that? I would recommend the following approach, maybe this pseudocode can inspire you:

abstract class Message
{
    void write(stream)
    {
        stream.write(getMessageTypeCode());
        writeParameters(stream);
    }
    abstract int getMessageTypeCode();
    abstract void writeParameters(stream);
    abstract void readParamters(stream);
}
class ChatMessage
{
    String text;
    int getMessageTypeCode() { return 1; }
    void writeParameters(stream)
    {
        stream.write(text);
    }
    void readParameters(stream)
    {
        text = stream.read();
    }
}
class MessageDecoder
{
    Message decode(stream)
    {
        int type = stream.read();
        message = createMessage(type);
        message.readParameters(stream);
        return message;
    }
    Message createMessage(int type)
    {
        if (type == 1)
        {
            return new ChatMessage();
        }
        throw new error;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文