What is the difference between Type and Class?

前端 未结 20 2221
梦谈多话
梦谈多话 2020-11-28 01:00

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

相关标签:
20条回答
  • 2020-11-28 01:21

    Type generally refers to the classification of primitive values - integers, strings, arrays, booleans, null, etc. Usually, you can't create any new types.

    Class refers to the named set of properties and methods which an object is associated with when it is created. You can usually define as many new classes as you want, although some languages you have to create a new object and then attach methods to it.

    This definition is mostly true, but some languages have attempted to combine types and classes in various ways, with various beneficial results.

    0 讨论(0)
  • 2020-11-28 01:24

    To illustrate it the fastest way:

    A Struct is a Type, but a Struct is not a Class.

    As you can see, a Type is an "abstract" term for not only definitions of classes, but also structs and primitive data types like float, int, bool.

    0 讨论(0)
  • 2020-11-28 01:32

    Obviously, as there are languages with type system that are not OO programming languages, type must be a broader concept than class

    Even in languages like Java, int is a (primitive) type, but not a class.

    Hence: every class is a type, but not every type is a class.

    0 讨论(0)
  • 2020-11-28 01:33

    Type contains description of the data (i.e. properties, operations, etc),

    Class is a specific type - it is a template to create instances of objects.

    Strictly speaking class is a special concept, it can be seen as a package containing subset of metadata describing some aspects of an object.

    For example in C# you can find interfaces and classes. Both of them are types, but interface can only define some contract and can not be instantiated unlike classes.

    Simply speaking class is a specialized type used to encapsulate properties and behavior of an object.

    Wikipedia can give you a more complete answer:

    • Definition of class
    • Definition of data type
    0 讨论(0)
  • 2020-11-28 01:33

    My thoughts are pretty much in line with aku's answer.

    I see classes as a template for building objects, while types are a way to classify those objects, and provide us with an interface to them.

    Python also adds metaclasses, that are just a mechanism to build classes, in the same way as classes build objects (and well, classes and metaclasses are both objects).

    This response to the same question in lamba the ultimate seems to me like a perfect explanation.

    0 讨论(0)
  • 2020-11-28 01:34

    Types in C, like Int Float, char etc define data that can be acted on with specific methods that can operate on them. It's no more complicated than that. Like for int I can add, subtract multiply and maybe divide. Those are my methods (or operations) for int. A Class is simply a definition of a new type. I first define what the data looks like. Maybe its a single bit. Maybe it's two words like a complex with a real and imaginary part. Or maybe its this complex thingy with 309734325 bytes representing the atomic makeup of a weird particle on Jupiter. I don't care. Just like an integer, I get to make up the operations I can do with this new data type. In the case of the integer I had add, subtract, etc. With this new data type I can define whatever operations I think make sense. They might be add subtract etc. but they may add other things. These are whatever methods I decide to add to my class.

    The bottom line is that with a type in C, you have a definition of what the data is, ie; a byte, word, float, char etc. But any of these also implies what operations are legal and will produce reliable results.

    A class is no different except it is up to you to define the interface and acceptable operations. The class defines these things and when you instantiate it in an Object it defines the behavior of the object just like a type definition defines the behavior of an integer when you operate on it.

    Classes just give you the flexibility to define new types and everything about how they operate.

    Once this is defined, every time I instantiate an object of class "thingy", it has the data structure I defined and the operations (methods) that I said you can do with it. The class "thingy" is clearly nothing more or less than a new type that C++ lets me define.

    0 讨论(0)
提交回复
热议问题