What is the difference between Type and Class?

前端 未结 20 2219
梦谈多话
梦谈多话 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:15

    Type is conceptually a superset of class. In the broader sense, a class is one form of type.

    Closely related to classes are interfaces, which can bee seen as a very special kind of class - a purely abstract one. These too are types.

    So "type" encompasses classes, interfaces and in most languages primitives too. Also platforms like the dot-net CLR have structure types too.

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

    I think of a type as being the set of things you can do with a particular value. For instance, if you have an integer value, you can add it to other integers (or perform other arithmetic operations), or pass it to functions which accept an integer argument. If you have an object value, you can call methods on it that are defined by its class.

    Because a class defines what you can do with objects of that class, a class defines a type. A class is more than that though, since it also provides a description of how the methods are implemented (something not implied by the type) and how the fields of the object are laid out.

    Note also that an object value can only have one class, but it may have multiple types, since every superclass provides a subset of the functionality available in the object's class.

    So although objects and types are closely related, they are really not the same thing.

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

    If we think to this question in C# context, we reach bellow answer.

    C# type system is divided into following categories:

    Value types:

    • Simple types: like int, long, float, etc.
    • Enum types
    • Struct types
    • Nullable types

    Reference types:

    • Class types
    • Interface types
    • Array types
    • Delegate types

    As you can see there are many types in C# which Class is only one of them. There is just one important note: C#’s type system is unified such that a value of any type can be treated as an object. Every type in C# directly or indirectly derives from the object class type, and object is the ultimate base class of all types. Values of reference types are treated as objects simply by viewing the values as type object. Values of value types are treated as objects by performing boxing and unboxing operations.

    so as I see, type is an umbrella over many items which class is one of them.

    Referece: CSahrp Language Specification doc, page 4

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

    In general language-agnostic sense - Class is an realization of the Type.

    Often when this is the only realization of that type, you can use both terms to reference it in some context.

    On the contrary, for example, in C# context - Class is just one of the many more implementations of a Type concept like primitives, structs, pointers etc.

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

    This was a good question for me, which made me think hard. I would dare to say that Class is a compiletime thingy and Type is a runtime thingy. I say this because you write classes not types. The compiler then creates types from classes, and the runtime use types to create instances of objects.

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

    The following answer is from Gof book (Design Patterns)

    An object's class defines how the object is implemented .The class defines object's internal state and the implementation of its operations.

    In contrast, an object's type only refers to its interface - a set of requests to which it can respond.

    An object can have many types, and objects of different classes can have the same type.

    //example in c++
    template<typename T> 
    const T & max(T const & a,T const &b)
    {
    return a>b?a:b;  //> operator of the type is used for comparison
    }
    

    max function requires a type with operation > with its own type as one of it interface any class that satisfies the above requirement can be used to generate specific max function for that class.

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