类型和类之间有什么区别? [英] What is the difference between Type and Class?
问题描述
以下答案来自Gof book(Design Patterns)
> class 定义如何实现
对象。类
定义对象的内部状态,
执行
操作。
相比之下,对象的
类型只指向它的接口 -
的请求集合,它可以响应
。 p>
一个对象可以有多种类型,
和不同类的对象可以
具有相同的类型。
// c ++中的示例
template< typename T>
const T& max(T const& a,T const& b)
{
return a> b?a:b; //>类型的运算符用于比较
}
max函数需要一个类型 operation> 使用自己的类型作为其中一个接口满足上述要求的任何类可以用于为该类生成特定的最大函数。
What makes a type different from class and vice versa?
(In the general language-agnostic sense)
The following answer is from Gof book (Design Patterns )
An objects's class defines how the object is implemented .The class defines object's internal state and the implementation of its operations.
In contrast, an objects's type only refers to its interface -the set of requests to which it can respond.
An object can have many type, and object 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 comparision
}
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.
这篇关于类型和类之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!