简体   繁体   English

在继承的类上重写类变量/方法

[英]Override class variable/method on inherited classes

I've defined a class, and I'd like to have a class method (or variable, I don't really care) which needs to be overriden by each inherited class. 我已经定义了一个类,并且希望有一个类方法(或变量,我不在乎),每个继承的类都需要重写该方法。 My best approaches have been unsuccessful: 我最好的方法是不成功的:

public abstract String getHumanString(); , as abstract methods can't be static ,因为抽象方法不能是静态的

public static String getHumanString(); , as static methods can only be hidden, not overriden. ,因为静态方法只能被隐藏,不能被覆盖。

I don't know if there is another trick I could use. 我不知道我是否可以使用其他技巧。 I could also be happy to automatically define getHumanString() by myself on the parent class with something like humanize( class.getClass().getName() ) (I'd just make getName() look more beautiful, according to my needs), getName() is not accessible on static methods. 我也可以高兴地自动在父类上通过诸如getHumanString() class.getClass().getName() )之类的方法自动定义getHumanString() getName()根据我的需要,我会让getName()看起来更漂亮) , getName()在静态方法上不可访问。 So, to sum up, my question is: 因此,总而言之,我的问题是:

1. Can I have a static method/variable which could be edited on subclasses? 1.我可以有一个可以在子类上进行编辑的静态方法/变量吗?

OR, as another valid solution: 或者,作为另一个有效的解决方案:

2. Can I create a static method that plays with its own class name? 2.我可以创建一个使用自己的类名播放的静态方法吗?

Thanks in advance. 提前致谢。

EDIT: 编辑:

Imagine this situation (I'll skip useless lines, as I'm directly writing code here): 想象一下这种情况(我将在此处直接编写代码,所以我将跳过无用的行):

class ParentClass {
    public static (or whatever) String getHumanString() {
        return "My Parent Class";
    }

    public static showMyInfo() {
        System.out.println(getHumanString());
    }
}

class ChildClass {
    @Override
    public static (or whatever) String getHumanString() {
        return "My Child Class";
    }
}

... ...

public static void main()
{
   ChildClass.showMyInfo();
}

It should output (as I'd want) "My Child Class", but it actually outputs (as you all know) "My Parent Class". 它应该输出(如我所愿)“我的孩子班”,但实际上却输出(众所周知)“我的父母班”。

Static methods aren't polymorphic, no. 静态方法不是多态的,不是。 Nor are variables, by the way - you can't override variables, even instance ones. 顺便说一句,变量也不是-您不能覆盖变量,甚至不能覆盖实例变量。 You can hide them, but not override them. 您可以隐藏它们,但不能覆盖它们。

It's not really clear what you're trying to do, to be honest - but you may want to consider a corresponding type hierarchy so that the static methods become instance methods in the new types. 坦白说,您实际上并不清楚要做什么,但是您可能需要考虑一个对应的类型层次结构,以便静态方法成为新类型中的实例方法。

I'd like to have a readable name for each class (not just getClass().getName()), which does not depend on the object instance. 我希望每个类都有一个可读的名称(不仅仅是getClass()。getName()),它不依赖于对象实例。 In fact, I'd like to avoid instancing that class when I need getHumanString(). 实际上,当我需要getHumanString()时,我想避免实例化该类。

Have a properties file, with class name as key and human string as value. 有一个属性文件,其类名为键,人为字符串为值。 And a single static method somewhere to read it. 还有一个静态方法可以在某处读取它。

 static String getHumanString(Class<?> clazz){
     return humanStrings.getProperty(clazz.getName(), clazz.getSimpleName());
 }

Also lends itself to localization if necessary. 必要时还可以进行本地化。 Plus you can "enhance" existing classes whose definition you cannot change. 另外,您可以“增强”您无法更改其定义的现有类。

If this is a requirement I would suggest splitting the static members and fields into a class(es) of their own. 如果有此要求,我建议将static成员和字段拆分为自己的类。 This way your existing class can refer to a single static instance of a class which is polymorphic. 这样,您现有的类可以引用一个多态类的单个static实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM