Java abstract static Workaround

前端 未结 15 1924
猫巷女王i
猫巷女王i 2020-12-29 04:43

I understand that neither a abstract class nor an interface can contain a method that is both abstract and static because of ambiguity problems, but is there a workaround?

15条回答
  •  有刺的猬
    2020-12-29 05:04

    For what it is worth I know exactly what you are trying to do.

    I found this article while searching for the reasons I can't do it either.

    In my case I have HUNDREDS of classes that inherit from a central base base and I want simply to get a reference like this:

    ValueImSearchingFor visf = StaticClass.someArbitraryValue()

    I do NOT want to write/maintain someArbitraryValue() for each and every one of hundreds of the inherited classes -- I just want to write logic once and have it calc a Unique Class-Sepcific value for each and every future written class WITHOUT touching the base class.

    Yes I completely get OO - I've been writing Java for about as long as it's been available.

    These specific classes are more like "Definitions" as opposed to actual Objects and I don't want to instantiate one every time I just need to see what someArbitraryValue() actually is.

    Think of it as a PUBLIC STATIC FINAL that allows you to run a Method ONCE to set it initially. (Kinda like you can do when you define an Enum actually...)

提交回复
热议问题