Attributes / member variables in interfaces?

前端 未结 7 1428
眼角桃花
眼角桃花 2020-12-04 14:09

I wish to know is there any way in which I can make it compulsory for the implementer class to declare the objects handles/primitives as they do with methods. for e.g.:

7条回答
  •  北海茫月
    2020-12-04 14:35

    Fields in interfaces are implicitly public static final. (Also methods are implicitly public, so you can drop the public keyword.) Even if you use an abstract class instead of an interface, I strongly suggest making all non-constant (public static final of a primitive or immutable object reference) private. More generally "prefer composition to inheritance" - a Tile is-not-a Rectangle (of course, you can play word games with "is-a" and "has-a").

提交回复
热议问题