类型和类有什么区别? [英] What is the difference between Type and Class?

查看:42
本文介绍了类型和类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么使类型与类不同,反之亦然?

(在一般语言不可知的意义上)

解决方案

以下回答来自Gof book (设计模式)

<块引用>

一个对象的定义了对象实现.班上定义对象的内部状态和其实施操作.

<块引用>

相反,对象的type 只指它的接口——一个它可以处理的一组请求回应.

<块引用>

一个对象可以有多种类型,并且不同类的对象可以具有相同的类型.

//c++中的例子模板const T &max(T const &a,T const &b){返回 a>b?a:b;//>该类型的运算符用于比较}

max 函数需要一个带有操作>的类型,它自己的类型作为它的接口之一,任何满足上述要求的类都可以用来生成特定的max<特定类/原始类型>.该类的函数.

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 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<particular class/primitive type> function for that class.

这篇关于类型和类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆