What is the difference between Type and Class?

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

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

20条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 
    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.

提交回复
热议问题