0

What's the different between state and properties in a class?

In an OOP class:

class Person {
  private doingWhat = undefined
  ...
  public eat() {
    this.doingWhat = eating
  }

  public sleep() {
    this.doingWhat = sleeping
  }

  public run() {
    this.doingWhat = running
  }
  
}

in this class I use the property doingWhat represent the state of the Person instance. So I have a question about it, whether the property equals to the state in an OPP class, they are the same thing?

lustre
  • 119
  • 1
  • 16
  • BTW, you're using the term "properties" as though you've so-far only experienced JavaScript/TypeScript, but not other languages like Java, C#, or C++ where the term "property" refers to other concepts entirely (Java and C++ don't have class properties at all, Java uses "properties" to refer to configuration-collections, while C# has class member properties like JavaScript, they work differently (in C# a "property" is part of a class' _interface_ (unrelated to `interface` types btw) and describes getter/setter methods but still requires backing fields. – Dai Jul 16 '23 at 11:07
  • Also, in the React ecosystem, both terms "state" and "props" (short for "properties") have specific interpretations: https://stackoverflow.com/questions/27991366/what-is-the-difference-between-state-and-props-in-react?answertab=scoredesc#tab-top - I am unsure if this is what you're asking about. – Dai Jul 16 '23 at 11:08

1 Answers1

0

State refers to the overall condition of an object, which can be represented by the values of its properties at a given time. Properties are the individual data members that hold the information contributing to the object's state. So, while closely related, they are not exactly the same thing in OOP.

In your example, the 'doingWhat' property is used to represent the state of a Person instance, indicating what the person is currently doing (eating, sleeping, running, etc.).

And also 'doingWhat' is a property of the Person class that holds the current activity of the person, contributing to the object's state.