Java:静态抽象(再次)-最佳实践如何解决 [英] Java: static abstract (again) - best practice how to work around

查看:23
本文介绍了Java:静态抽象(再次)-最佳实践如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从理论上理解 Java 中没有 abstract static 的原因,例如 为什么Java中的静态方法不能是抽象的 .

I theoretically understand the point why there is no abstract static in Java, as explained for instance in Why can't static methods be abstract in Java .

但是我该如何解决这样的问题呢?

我的应用程序使用几种类型的文件,我想分配静态属性,如该文件类型的描述(如数据文件",另一个是配置文件"等).显然,我会将它放入一个静态字符串中,以便无需实例化文件即可访问描述(对于 GUI f.i. 很有用).另一方面,显然所有的文件类型都应该有一些通用的方法,比如getStatus(),显然我想从一个通用的超类MyFileType继承.

My application uses files of a few types, which I want to assign static properties like a description of that file type (like "data file", the other being "config file", etc.). Obviously, I would put that into a static String so that the description is accessible without instancing a file (useful for the GUI f.i.). On the other hand, obviously all file types should have some common methods like getStatus(), which obviously I want to inherit from a common superclass MyFileType.

getDescription() 在超类中当然是抽象的.

getDescription() would of course be abstract in the superclass.

尝试使用超类和接口的组合,但类似的问题是:不允许抽象方法的静态实现.

Tried using a combination of a superclass and an interface, but similar problem: A static implementation of an abstract method is not allowed.

Java 大师将如何解决这个问题?我想要创建的实现真的如此糟糕吗?

How would a Java guru solve this? Is it really such a bad implementation that I want to create?

非常感谢,菲利普

推荐答案

重申问题:您希望每个文件类型的类具有关于类型的静态可用信息(例如,名称和描述).

To restate the problem: you want your per-file-type classes to have statically available information on the type (e.g., name and description).

我们可以很容易地做到这一点:为您的类型信息创建一个单独的类,并在每个文件类型的类中都有一个静态实例(适当地实例化).

We can easily get part-way there: create a separate class for your type info, and have a static instance of this (appropriately instantiated) in each per-file-type class.

package myFileAPI;

public class TypeInfo { 
    public final String name;
    public final String description;

    public TypeInfo(String name, String description) {
        this.name = name;
        this.description = description;
    }
}

然后说:

package myFileAPI;

public class TextFile {
    public static final TypeInfo typeInfo
                   = new TypeInfo("Text", "Contains text.");
}

然后您可以执行以下操作:

Then you can do stuff like:

System.out.println(TextFile.typeInfo.name);

(当然,你也可以在TypeInfo中使用getter来封装底层的字符串.)

(Of course, you could also use getters in TypeInfo to encapsulate the underlying strings.)

然而,正如您所说,我们真正想要的是在编译时强制在您所有的每个文件类型的类中存在一个特定的签名静态方法,但明显"的设计路径导致在公共超类中需要一个抽象静态方法,这是不允许的.

However, as you said, what we really want is to enforce the existence of a particular signature static method in all your per-file-type classes at compile time, but the 'obvious' design path leads to requiring an abstract static method in a common superclass which isn't allowed.

我们可以在运行时强制执行此操作,这可能足以确保正确编码.我们引入一个 File 超类:

We can enforce this at run-time though, which may be good enough to ensure it is coded correctly. We introduce a File superclass:

package myFileAPI;

public abstract class File {

    public static TypeInfo getTypeInfo() {
        throw new IllegalStateException(
                    "Type info hasn't been set up in the subclass");
    }

}

如果TextFile现在extends File,我们会在运行时调用TextFile.getTypeInfo()时得到这个异常,除非TextFile有相同的-签名方法.

If TextFile now extends File, we will get this exception when calling TextFile.getTypeInfo() at runtime, unless TextFile has a same-signature method.

这很微妙:带有 TextFile.getTypeInfo() 的代码仍然可以编译,即使 TextFile 中没有这样的方法.即使静态方法在编译时绑定,编译器仍然可以通过类层次结构来确定编译时静态调用目标.

This is quite subtle: code with TextFile.getTypeInfo() in still compiles, even when there is no such method in TextFile. Even though static methods are bound at compile time, the compiler can still look through the class hierarchy to determine the compile-time static call target.

所以,我们需要这样的代码:

So, we need code like:

package myFileAPI;

public class TextFile extends File {

    private static final TypeInfo typeInfo
                      = new TypeInfo("Text", "Contains text.");

    // Shadow the superclass static method
    public static TypeInfo getTypeInfo() {
        return typeInfo;
    }

}

请注意,我们仍然隐藏超类方法,因此 File.getTypeInfo() 仍然可以无意义地"调用.

Note that we are still shadowing the superclass method, and so File.getTypeInfo() can still be 'meaninglessly' called.

这篇关于Java:静态抽象(再次)-最佳实践如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆