What's the difference between an object and a

2019-03-08 14:12发布

  • 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 why would we use an array as opposed to an object or a struct?

I would like to get an idea of what each is intended for.

10条回答
虎瘦雄心在
2楼-- · 2019-03-08 15:03
  • 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.

查看更多
Explosion°爆炸
3楼-- · 2019-03-08 15:09

Obviously you can blur the distinctions according to your programming style, but generally a struct is a structured piece of data. An object is a sovereign entity that can perform some sort of task. In most systems, objects have some state and as a result have some structured data behind them. However, one of the primary functions of a well-designed class is data hiding — exactly how a class achieves whatever it does is opaque and irrelevant.

Since classes can be used to represent classic data structures such as arrays, hash maps, trees, etc, you often see them as the individual things within a block of structured data.

An array is a block of unstructured data. In many programming languages, every separate thing in an array must be of the same basic type (such as every one being an integer number, every one being a string, or similar) but that isn't true in many other languages.

As guidelines:

  • use an array as a place to put a large group of things with no other inherent structure or hierarchy, such as "all receipts from January" or "everything I bought in Denmark"
  • use structured data to compound several discrete bits of data into a single block, such as you might want to combine an x position and a y position to describe a point
  • use an object where there's a particular actor or thing that thinks or acts for itself

The implicit purpose of an object is therefore directly to associate tasks with the data on which they can operate and to bundle that all together so that no other part of the system can interfere. Obeying proper object-oriented design principles may require discipline at first but will ultimately massively improve your code structure and hence your ability to tackle larger projects and to work with others.

查看更多
相关推荐>>
4楼-- · 2019-03-08 15:09

Generally speaking, objects bring the full object oriented functionality (methods, data, virtual functions, inheritance, etc, etc) whereas structs are just organized memory. Structs may or may not have support for methods / functions, but they generally won't support inheritance and other full OOP features.

Note that I said generally speaking ... individual languages are free to overload terminology however they want to.

Arrays have nothing to do with OO. Indeed, pretty much every language around support arrays. Arrays are just blocks of memory, generally containing a series of similar items, usually indexable somehow.

查看更多
乱世女痞
5楼-- · 2019-03-08 15:09

Arrays are ordered collection of items that (usually) are of the same types. Items can be accessed by index. Classic arrays allow integer indices only, however modern languages often provide so called associative arrays (dictionaries, hashes etc.) that allow use e.g. strings as indices.

Structure is a collection of named values (fields) which may be of 'different types' (e.g. field a stores integer values, field b - string values etc.). They (a) group together logically connected values and (b) simplify code change by hiding details (e.g. changing structure layout don't affect signature of function working with this structure). The latter is called 'encapsulation'.

Theroretically, object is an instance of structure that demonstrates some behavior in response to messages being sent (i.e., in most languages, having some methods). Thus, the very usefullness of object is in this behavior, not its fields.

Different objects can demonstrate different behavior in response to the same messages (the same methods being called), which is called 'polymorphism'.

In many (but not all) languages objects belong to some classes and classes can form hierarchies (which is called 'inheritance').

Since object methods can work with its fields directly, fields can be hidden from access by any code except for this methods (e.g. by marking them as private). Thus encapsulation level for objects can be higher than for structs.

Note that different languages add different semantics to this terms.

E.g.:

  • in CLR languages (C#, VB.NET etc) structs are allocated on stack/in registers and objects are created in heap.

  • in C++ structs have all fields public by default, and objects (instances of classes) have all fields private.

  • in some dynamic languages objects are just associative arrays which store values and methods.

查看更多
登录 后发表回答