Attributes / member variables in interfaces?

前端 未结 7 1422
眼角桃花
眼角桃花 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:46

    In Java you can't. Interface has to do with methods and signature, it does not have to do with the internal state of an object -- that is an implementation question. And this makes sense too -- I mean, simply because certain attributes exist, it does not mean that they have to be used by the implementing class. getHeight could actually point to the width variable (assuming that the implementer is a sadist).

    (As a note -- this is not true of all languages, ActionScript allows for declaration of pseudo attributes, and I believe C# does too)

    0 讨论(0)
提交回复
热议问题