Java abstract static Workaround

前端 未结 15 2012
猫巷女王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:00

    There are lots of answers about 'this does'nt make sense..' but indeed I met a similar problem just yesterday.

    I wanted to use inheritance with my unit tests. I have an API and several its implementations. So I need only 1 set of unit tests for all implementations but with different setUp methods which are static.

    Workaround: all tests are abstract classes, with some static fields with protected access modifier. In all implementations I added static methods which set these static fields. It works rather nice, and I avoided copy and paste.

提交回复
热议问题