What makes a type different from class and vice versa?
什么使一个类型与类不同,反之亦然?
(In the general language-agnostic sense)
(在一般的语言不可知的意义上)
17 个解决方案
#1
91
The following answer is from Gof book (Design Patterns )
下面的答案来自Gof book(设计模式)
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 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.
max函数需要一个带有操作>的类型,它的类型为它的一个接口,任何满足上述需求的类都可以用来为该类生成特定的max函数。
#2
46
I always think of a 'type' as an umbrella term for 'classes' and 'primitives'.
我总是把“类型”看作是“类”和“原语”的统称。
int foo; // Type is int, class is nonexistent.
int foo;//类型是int型的,类是不存在的。
MyClass foo; // Type is MyClass, class is MyClass
MyClass foo;//类型是MyClass,类是MyClass。
#3
28
Inspired by Wikipedia...
灵感来自*…
In type theory terms;
在类型理论术语;
-
A type is an abstract interface.
Types generally represent nouns, such as a person, place or thing, or something nominalized,类型是一个抽象的接口。类型通常表示名词,例如人、地点或事物,或名词化的事物,
-
A class represents an implementation of the type.
It is a concrete data structure and collection of subroutines类表示类型的实现。它是一种具体的数据结构和子程序集。
Different concrete classes can produce objects of the same abstract type (depending on type system).
不同的具体类可以生成相同抽象类型的对象(取决于类型系统)。
*For example, one might implement the type
Stack
with two classes:SmallStack
(fast for small stacks, but scales poorly) andScalableStack
(scales well but high overhead for small stacks).*例如,你可以用两个类来实现类型堆栈:SmallStack(小堆栈快速,但伸缩性差)和ScalableStack(小栈的高开销)。
Similarly, a given class may have several different constructors.
同样,给定的类可能有几个不同的构造函数。
The banana example.
香蕉的例子。
A
Banana
type would represent the properties and functionality of bananas in general.香蕉的类型通常代表香蕉的特性和功能。
The
ABCBanana
andXYZBanana
classes would represent ways of producing bananas.
(Different banana suppliers in real life, or different data structures and functions to represent and draw bananas in a video game).香蕉和XYZBanana类会代表香蕉的生产方式。(在现实生活中,不同的香蕉供应商,或不同的数据结构和功能,在视频游戏中代表和绘制香蕉)。
The
ABCBanana
class could then produce particular bananas which are instances of theABCBanana
class, they would be objects of type Banana.ABCBanana类可以产生特定的香蕉,这些香蕉是ABCBanana类的实例,它们是香蕉的对象。
It is not rare the programmer provide a single and only implementation for a type. In this case the class name is often identical with the type name. But there is still a type (which could be extracted in an interface if required), and an implementation (which would implement the separate interface) which builds instances (objects) of the class.
程序员提供一种类型的单一和唯一实现并不少见。在这种情况下,类名通常与类型名相同。但是仍然有一种类型(如果需要,可以在接口中提取)和实现(它将实现单独的接口)来构建类的实例(对象)。
#4
13
Type is the umbrella term for all the available object templates or concepts. A class is one such object template. So is the structure type, the Integer type, the Interface type etc. These are all types
类型是所有可用对象模板或概念的统称。类就是一个这样的对象模板。结构类型,整数类型,接口类型等等都是类型。
If you want, you can look at it this way: A type is the parent concept. All the other concepts: Class, Interface, Structure, Integer etc inherit from this concept.i.e They are types
如果需要,可以这样看:类型是父概念。所有其他概念:类、接口、结构、整数等继承自这个概念。他们是类型
#5
4
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.
例如,在c#中,您可以找到接口和类。它们都是类型,但是接口只能定义一些契约,不能像类那样实例化。
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
- 数据类型的定义
#6
4
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.
正如您所看到的,类型是一个“抽象的”术语,它不仅用于类的定义,而且还包括结构和原始数据类型,例如float、int、bool。
#7
3
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.
所以“类型”包含类、接口和大多数语言原语。同样的平台,像网络的CLR也有结构类型。
#8
3
My thoughts are pretty much in line with aku's answer.
我的想法和aku的回答差不多。
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).
Python还添加了元类,这只是构建类的一种机制,与类构建对象的方式相同(当然,类和元类都是对象)。
This response to the same question in lamba the ultimate seems to me like a perfect explanation.
这对兰巴的同样问题的回答在我看来是一个完美的解释。
#9
2
To add another example of distinction: in C++ you have pointer and reference types which can refer to classes, but are not classes in and of themselves.
要添加另一个区别示例:在c++中,您有指针和引用类型,它们可以引用类,但它们本身并不是类。
Bar b; // b is of type "class Bar"
Bar *b2 = &b; // b2 is of type "pointer to Class Bar"
Bar &b3 = b; // b3 is of type "reference to Class Bar"
Bar *b4[7]; // b4 is of type "7-element array of pointers to Class Bar"
Bar ***b5; //b5 is of type "pointer to a pointer to a pointer to Class Bar"
Note that only one class is involved, but a near infinite number of types can be used. In some languages, function are considered "first-class-objects" in which case, the type of a function is a class. In others, the type of a function is merely a pointer. Classes generally have the concepts of being able to hold data, as well as operations on that data.
请注意,只涉及一个类,但可以使用近乎无限的类型。在某些语言中,函数被认为是“第一类对象”,在这种情况下,函数的类型是类。在另一些情况下,函数的类型只是一个指针。类通常有能够保存数据的概念,以及对该数据的操作。
#10
2
Taken from the GoF citation from below:
摘自以下的GoF引文:
An objects's class defines how the object is implemented .The class defines the 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.
相反,对象的类型只引用它的接口——它可以响应的请求集。
I want to provide an example using Java:
我想提供一个使用Java的例子:
public interface IType {
}
public class A implements IType {
public A{};
}
public class B implements IType {
public B{};
}
Both classes A
and B
implement the interface and thus are of the type IType
. Additionally in Java, both classes produce their own type (respectively to their class name). Thus the class A
is of type A
and IType
and the class B
is of type B
and IType
satisfying:
A和B类都实现了接口,因此是类型IType。此外,在Java中,这两个类都产生自己的类型(分别对应于它们的类名)。因此A类是A型和IType型,B类是B型和IType型:
An object can have many types, and objects of different classes can have the same type.
对象可以有许多类型,不同类的对象可以具有相同类型。
The difference between subtypes and subclass probably helps to understand that issue as well:
子类型和子类之间的区别可能有助于理解这个问题:
https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node12.html
https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node12.html
#11
1
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.
因此,尽管对象和类型是密切相关的,但它们实际上并不是一回事。
#12
1
Type generally refers to the classification of primitive values - integers, strings, arrays, booleans, null, etc. Usually, you can't create any new types.
类型通常指的是原始值的分类——整数、字符串、数组、布尔值、null等等。通常,您不能创建任何新类型。
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.
这一定义大部分是正确的,但有些语言试图以各种方式将类型和类结合起来,带来各种有益的结果。
#13
1
Types and classes are related but not identical. My take is that classes are used for implementation inheritance, whereas types are used for runtime substitution.
类型和类是相关的,但不是相同的。我认为类是用于实现继承的,而类型用于运行时替代。
Here is a link explaining the substitution principle and why subclasses and subtypes are not always the same thing (in Java for example). The wikipedia page on covariance and contravariance has more information on this distinction.
这里有一个解释替换原则的链接,以及为什么子类和子类型并不总是相同的东西(例如在Java中)。*上关于协方差和逆变的页面有更多关于这个区别的信息。
#14
0
Interesting question. I think aku's answer is spot on. Take the java ArrayList
class as an example
有趣的问题。我认为aku的答案是正确的。以java ArrayList类为例。
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
An instance of the ArrayList
class is said to be of type of every superclass it extends and every interface it implements. Therefore, an instance of the ArrayList
class has a type ArrayList
, RandomAccess
, Cloneable
, and so forth. In other words, values (or instances) belong to one or more types, classes define what these types are.
ArrayList类的一个实例据说是它扩展的每个超类的类型以及它所实现的每个接口。因此,ArrayList类的实例有一个类型ArrayList、RandomAccess、Cloneable,等等。换句话说,值(或实例)属于一个或多个类型,类定义了这些类型。
#15
0
Different classes may describe the same type.
不同的类可以描述相同的类型。
Type consists of these parts:
类型由以下部分组成:
- Operations = syntax
- 操作=语法
- Description of operations = semantics
- 操作说明=语义。
Class consists of these parts:
类由以下部分组成:
- Operations = syntax
- 操作=语法
- Implementation (= various implementations describe same semantics)
- 实现(=各种实现描述相同的语义)
Some notes:
一些注意事项:
-
Interface (as in Java) is not type, because it does not describe semantics (describes only syntax)
接口(在Java中)不是类型,因为它没有描述语义(只描述语法)
-
Subclass is not subtype, because subclass may change semantics defined in superclass, subtype cannot change supertype semantics (see Liskov Substitution Principle, e.g. this LSP example).
子类不是子类型,因为子类可能会改变在超类中定义的语义,subtype不能改变超类型语义(参见Liskov替换原则,例如这个LSP示例)。
#16
0
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.
即使在Java这样的语言中,int也是一种(原始的)类型,但不是类。
Hence: every class is a type, but not every type is a class.
因此:每个类都是一种类型,但不是每种类型都是类。
#17
0
If we think to this question in C# context, we reach bellow answer.
如果我们在c#上下文中思考这个问题,我们就会得出一个成熟的答案。
C# type system is divided into following categories:
c#类型系统分为以下几类:
Value types:
值类型:
- Simple types: like int, long, float, etc.
- 简单类型:像int, long, float等。
- 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.
正如您所看到的,c#中有许多类型,该类只是其中之一。有一个重要的注意事项:c#的类型系统是统一的,任何类型的值都可以作为对象来处理。c#中的每个类型都直接或间接地来自对象类类型,对象是所有类型的最终基类。引用类型的值被视为对象,只是将值视为类型对象。值类型的值通过执行装箱和拆箱操作来处理。
so as I see, type is an umbrella over many items which class is one of them.
正如我所看到的,类型是一把伞,上面有很多类的东西。
Referece: CSahrp Language Specification doc, page 4
推荐人:CSahrp语言规范,第4页。
#1
91
The following answer is from Gof book (Design Patterns )
下面的答案来自Gof book(设计模式)
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 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.
max函数需要一个带有操作>的类型,它的类型为它的一个接口,任何满足上述需求的类都可以用来为该类生成特定的max函数。
#2
46
I always think of a 'type' as an umbrella term for 'classes' and 'primitives'.
我总是把“类型”看作是“类”和“原语”的统称。
int foo; // Type is int, class is nonexistent.
int foo;//类型是int型的,类是不存在的。
MyClass foo; // Type is MyClass, class is MyClass
MyClass foo;//类型是MyClass,类是MyClass。
#3
28
Inspired by Wikipedia...
灵感来自*…
In type theory terms;
在类型理论术语;
-
A type is an abstract interface.
Types generally represent nouns, such as a person, place or thing, or something nominalized,类型是一个抽象的接口。类型通常表示名词,例如人、地点或事物,或名词化的事物,
-
A class represents an implementation of the type.
It is a concrete data structure and collection of subroutines类表示类型的实现。它是一种具体的数据结构和子程序集。
Different concrete classes can produce objects of the same abstract type (depending on type system).
不同的具体类可以生成相同抽象类型的对象(取决于类型系统)。
*For example, one might implement the type
Stack
with two classes:SmallStack
(fast for small stacks, but scales poorly) andScalableStack
(scales well but high overhead for small stacks).*例如,你可以用两个类来实现类型堆栈:SmallStack(小堆栈快速,但伸缩性差)和ScalableStack(小栈的高开销)。
Similarly, a given class may have several different constructors.
同样,给定的类可能有几个不同的构造函数。
The banana example.
香蕉的例子。
A
Banana
type would represent the properties and functionality of bananas in general.香蕉的类型通常代表香蕉的特性和功能。
The
ABCBanana
andXYZBanana
classes would represent ways of producing bananas.
(Different banana suppliers in real life, or different data structures and functions to represent and draw bananas in a video game).香蕉和XYZBanana类会代表香蕉的生产方式。(在现实生活中,不同的香蕉供应商,或不同的数据结构和功能,在视频游戏中代表和绘制香蕉)。
The
ABCBanana
class could then produce particular bananas which are instances of theABCBanana
class, they would be objects of type Banana.ABCBanana类可以产生特定的香蕉,这些香蕉是ABCBanana类的实例,它们是香蕉的对象。
It is not rare the programmer provide a single and only implementation for a type. In this case the class name is often identical with the type name. But there is still a type (which could be extracted in an interface if required), and an implementation (which would implement the separate interface) which builds instances (objects) of the class.
程序员提供一种类型的单一和唯一实现并不少见。在这种情况下,类名通常与类型名相同。但是仍然有一种类型(如果需要,可以在接口中提取)和实现(它将实现单独的接口)来构建类的实例(对象)。
#4
13
Type is the umbrella term for all the available object templates or concepts. A class is one such object template. So is the structure type, the Integer type, the Interface type etc. These are all types
类型是所有可用对象模板或概念的统称。类就是一个这样的对象模板。结构类型,整数类型,接口类型等等都是类型。
If you want, you can look at it this way: A type is the parent concept. All the other concepts: Class, Interface, Structure, Integer etc inherit from this concept.i.e They are types
如果需要,可以这样看:类型是父概念。所有其他概念:类、接口、结构、整数等继承自这个概念。他们是类型
#5
4
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.
例如,在c#中,您可以找到接口和类。它们都是类型,但是接口只能定义一些契约,不能像类那样实例化。
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
- 数据类型的定义
#6
4
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.
正如您所看到的,类型是一个“抽象的”术语,它不仅用于类的定义,而且还包括结构和原始数据类型,例如float、int、bool。
#7
3
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.
所以“类型”包含类、接口和大多数语言原语。同样的平台,像网络的CLR也有结构类型。
#8
3
My thoughts are pretty much in line with aku's answer.
我的想法和aku的回答差不多。
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).
Python还添加了元类,这只是构建类的一种机制,与类构建对象的方式相同(当然,类和元类都是对象)。
This response to the same question in lamba the ultimate seems to me like a perfect explanation.
这对兰巴的同样问题的回答在我看来是一个完美的解释。
#9
2
To add another example of distinction: in C++ you have pointer and reference types which can refer to classes, but are not classes in and of themselves.
要添加另一个区别示例:在c++中,您有指针和引用类型,它们可以引用类,但它们本身并不是类。
Bar b; // b is of type "class Bar"
Bar *b2 = &b; // b2 is of type "pointer to Class Bar"
Bar &b3 = b; // b3 is of type "reference to Class Bar"
Bar *b4[7]; // b4 is of type "7-element array of pointers to Class Bar"
Bar ***b5; //b5 is of type "pointer to a pointer to a pointer to Class Bar"
Note that only one class is involved, but a near infinite number of types can be used. In some languages, function are considered "first-class-objects" in which case, the type of a function is a class. In others, the type of a function is merely a pointer. Classes generally have the concepts of being able to hold data, as well as operations on that data.
请注意,只涉及一个类,但可以使用近乎无限的类型。在某些语言中,函数被认为是“第一类对象”,在这种情况下,函数的类型是类。在另一些情况下,函数的类型只是一个指针。类通常有能够保存数据的概念,以及对该数据的操作。
#10
2
Taken from the GoF citation from below:
摘自以下的GoF引文:
An objects's class defines how the object is implemented .The class defines the 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.
相反,对象的类型只引用它的接口——它可以响应的请求集。
I want to provide an example using Java:
我想提供一个使用Java的例子:
public interface IType {
}
public class A implements IType {
public A{};
}
public class B implements IType {
public B{};
}
Both classes A
and B
implement the interface and thus are of the type IType
. Additionally in Java, both classes produce their own type (respectively to their class name). Thus the class A
is of type A
and IType
and the class B
is of type B
and IType
satisfying:
A和B类都实现了接口,因此是类型IType。此外,在Java中,这两个类都产生自己的类型(分别对应于它们的类名)。因此A类是A型和IType型,B类是B型和IType型:
An object can have many types, and objects of different classes can have the same type.
对象可以有许多类型,不同类的对象可以具有相同类型。
The difference between subtypes and subclass probably helps to understand that issue as well:
子类型和子类之间的区别可能有助于理解这个问题:
https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node12.html
https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node12.html
#11
1
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.
因此,尽管对象和类型是密切相关的,但它们实际上并不是一回事。
#12
1
Type generally refers to the classification of primitive values - integers, strings, arrays, booleans, null, etc. Usually, you can't create any new types.
类型通常指的是原始值的分类——整数、字符串、数组、布尔值、null等等。通常,您不能创建任何新类型。
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.
这一定义大部分是正确的,但有些语言试图以各种方式将类型和类结合起来,带来各种有益的结果。
#13
1
Types and classes are related but not identical. My take is that classes are used for implementation inheritance, whereas types are used for runtime substitution.
类型和类是相关的,但不是相同的。我认为类是用于实现继承的,而类型用于运行时替代。
Here is a link explaining the substitution principle and why subclasses and subtypes are not always the same thing (in Java for example). The wikipedia page on covariance and contravariance has more information on this distinction.
这里有一个解释替换原则的链接,以及为什么子类和子类型并不总是相同的东西(例如在Java中)。*上关于协方差和逆变的页面有更多关于这个区别的信息。
#14
0
Interesting question. I think aku's answer is spot on. Take the java ArrayList
class as an example
有趣的问题。我认为aku的答案是正确的。以java ArrayList类为例。
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
An instance of the ArrayList
class is said to be of type of every superclass it extends and every interface it implements. Therefore, an instance of the ArrayList
class has a type ArrayList
, RandomAccess
, Cloneable
, and so forth. In other words, values (or instances) belong to one or more types, classes define what these types are.
ArrayList类的一个实例据说是它扩展的每个超类的类型以及它所实现的每个接口。因此,ArrayList类的实例有一个类型ArrayList、RandomAccess、Cloneable,等等。换句话说,值(或实例)属于一个或多个类型,类定义了这些类型。
#15
0
Different classes may describe the same type.
不同的类可以描述相同的类型。
Type consists of these parts:
类型由以下部分组成:
- Operations = syntax
- 操作=语法
- Description of operations = semantics
- 操作说明=语义。
Class consists of these parts:
类由以下部分组成:
- Operations = syntax
- 操作=语法
- Implementation (= various implementations describe same semantics)
- 实现(=各种实现描述相同的语义)
Some notes:
一些注意事项:
-
Interface (as in Java) is not type, because it does not describe semantics (describes only syntax)
接口(在Java中)不是类型,因为它没有描述语义(只描述语法)
-
Subclass is not subtype, because subclass may change semantics defined in superclass, subtype cannot change supertype semantics (see Liskov Substitution Principle, e.g. this LSP example).
子类不是子类型,因为子类可能会改变在超类中定义的语义,subtype不能改变超类型语义(参见Liskov替换原则,例如这个LSP示例)。
#16
0
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.
即使在Java这样的语言中,int也是一种(原始的)类型,但不是类。
Hence: every class is a type, but not every type is a class.
因此:每个类都是一种类型,但不是每种类型都是类。
#17
0
If we think to this question in C# context, we reach bellow answer.
如果我们在c#上下文中思考这个问题,我们就会得出一个成熟的答案。
C# type system is divided into following categories:
c#类型系统分为以下几类:
Value types:
值类型:
- Simple types: like int, long, float, etc.
- 简单类型:像int, long, float等。
- 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.
正如您所看到的,c#中有许多类型,该类只是其中之一。有一个重要的注意事项:c#的类型系统是统一的,任何类型的值都可以作为对象来处理。c#中的每个类型都直接或间接地来自对象类类型,对象是所有类型的最终基类。引用类型的值被视为对象,只是将值视为类型对象。值类型的值通过执行装箱和拆箱操作来处理。
so as I see, type is an umbrella over many items which class is one of them.
正如我所看到的,类型是一把伞,上面有很多类的东西。
Referece: CSahrp Language Specification doc, page 4
推荐人:CSahrp语言规范,第4页。