objects with state and behavior in oop

后端 未结 4 469
渐次进展
渐次进展 2021-02-04 05:14

I keep hearing the term object has behavior and state or just one of them. But what is the difference or what does it mean, and if anyone can give an example I would really appr

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 05:34

    class Door {
      boolean isOpen;
    
      void close(){
        isOpen = false;
      }
    }
    

    Look at this simple snippet. We have class Door, it has a state isOpen - variable describes current state of this door. Method close it is behaviour of door, when we call it, we change current state of object.

    I advice you to read good for beginners book about object oriented programming: Head First Object-Oriented Analysis and Design. If you read it you get better understanding.

提交回复
热议问题