What's the difference between an object and a struct in OOP?

后端 未结 10 1875
旧时难觅i
旧时难觅i 2021-01-30 09:05
  • What distinguishes and object from a struct?
  • When and why do we use an object as opposed to a struct?
  • How does an array differ from both, and when and wh
10条回答
  •  攒了一身酷
    2021-01-30 09:31

    • As I see it an object at the basic level is a number of variables and a number of methods that manipulate those variables, while a struct on the other hand is only a number of variables.
    • I use an object when you want to include methods, I use a struct when I just want a collection of variables to pass around.
    • An array and a struct is kind of similar in principle, they're both a number of variables. Howoever it's more readable to write myStruct.myVar than myArray[4]. You could use an enum to specify the array indexes to get myArray[indexOfMyVar] and basically get the same functionality as a struct.

    Of course you can use constants or something else instead of variables, I'm just trying to show the basic principles.

提交回复
热议问题