aijam
2020-02-07 14:46:26 +08:00
In Assembly or C, type determines the interpretation of a sequence of memory and the operations that can be performed on it. For example, if you interpret 4 bytes of data as int, you can perform arithmetic operations with it; but if you interpret the same 4 bytes as char[4], you will be able to iterate over it with some indices.
Such idea can be found in many other languages. Another example is when you do "type casting", you are basically indicating the compiler to treat the same sequence of bytes with a different interpretation, such that a different set of operations can be applied.
So, how can you create your own type, i.e. define your own interpretation and the set of operations on a piece of data? That's where classes come into the picture. In general, you can consider a class as a recipe of a user-defined type, in contrast to the built-in types which are already predefined for you.
Surely, types and classes are much more profound concepts, especially when putting in the context of different languages. If you want to dive deeper on this topic, I think you should learn a little more about types and "type class" in Haskell, and maybe try to find the connection between "type class" in Haskell and "abstract class"/"interface" in the OO world. Also, compare them with "struct" in C/C++ and see how those languages provide their own mechanisms for abstraction.