类型和类别有什么区别?

发布于 2024-07-11 22:41:20 字数 48 浏览 12 评论 0原文

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

(在与语言无关的一般意义上)

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(22

子栖 2024-07-18 22:41:21

我始终将“类型”视为“类”和“基元”的总称。

int foo; // 类型为 int,类不存在。

MyClass foo; // 类型是MyClass,类是MyClass

I always think of a 'type' as an umbrella term for 'classes' and 'primitives'.

int foo; // Type is int, class is nonexistent.

MyClass foo; // Type is MyClass, class is MyClass

叶落知秋 2024-07-18 22:41:21

受到维基百科的启发...

类型理论术语中;

  • 类型是一个抽象接口。
    类型通常表示名词,例如人、地点或事物,或者名词化的东西,

  • 表示类型的实现。
    它是一个具体的数据结构和子程序的集合

    不同的具体类可以生成相同抽象类型的对象(取决于类型系统)。

    *例如,可以使用两个来实现类型 StackSmallStack(对于小型应用来说速度很快)堆栈,但扩展性较差)和 ScalableStack (扩展性良好,但小堆栈的开销较高)。*

    同样,给定的类可能有多个不同的构造函数

在此处输入图像描述

香蕉示例。

  • Banana 类型代表香蕉的一般属性和功能。

  • ABCBananaXYZBanana 代表生产香蕉的方法。
    (现实生活中不同的香蕉供应商,或者在视频游戏中表示和绘制香蕉的不同数据结构和函数)。

    ABCBanana 类可以生成特定的香蕉,它们是
    ABCBanana 实例,它们将是<的对象 em>类型香蕉

程序员为某种类型提供单一且唯一的实现的情况并不罕见。 在这种情况下,名称通常与类型名称相同。 但仍然有一个类型(如果需要,可以在接口中提取)和一个构建类实例(对象)的实现(将实现单独的接口)。

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) and ScalableStack (scales well but high overhead for small stacks).*

    Similarly, a given class may have several different constructors.

enter image description here

The banana example.

  • A Banana type would represent the properties and functionality of bananas in general.

  • The ABCBanana and XYZBanana 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).

    The ABCBanana class could then produce particular bananas which are
    instances of the ABCBanana class, they would be objects of type Banana.

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.

寻找我们的幸福 2024-07-18 22:41:21

类型是所有可用对象模板或概念的总称。 类就是这样一种对象模板。 结构体类型、整数类型、接口类型等也是如此。这些都是类型。

如果你愿意,你可以这样看:类型是父概念。 所有其他概念:类、接口、结构、整数等都继承自这个概念。即它们是类型

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

私藏温柔 2024-07-18 22:41:21

摘自 GoF 引文如下:

对象的定义了如何
对象已实现。类
定义对象的内部状态和
其实施
操作。

相反,对象的
类型仅指其接口 -
它可以处理的一组请求
回复。

我想提供一个使用 Java 的示例:

public interface IType {
}

public class A implements IType {
    public A{};
}

public class B implements IType {
    public B{};
}

AB 类都实现了接口,因此都是 IType 类型。 此外,在 Java 中,这两个类都会生成自己的类型(分别对应于它们的类名)。 因此,类A的类型为AIType,类B为类型 B IType 满足:

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

子类型和子类之间的区别可能也有助于理解这个问题:

https://www.cs.princeton.edu/courses/archive/fall98/cs441/mainus/node12.html

Taken from the GoF citation from below:

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:

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:

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

纸伞微斜 2024-07-18 22:41:21

与语言无关的意义上 - 类型实现

通常,当这是该类型的唯一实现时,您可以在某些上下文中使用这两个术语来引用它。

相反,例如,在 C# 上下文中 - Class 只是 更多实现类型概念,如原语、结构体、指针等。

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.

归属感 2024-07-18 22:41:21

类型包含数据的描述(即属性、操作等),

类是特定类型 - 它是创建 对象

严格来说,类是一个特殊的概念,它可以看作是一个包含元数据描述的子集的包物体的某些方面。

例如,在 C# 中,您可以找到接口和类。 两者都是类型,但是接口只能定义一些契约,并且不能像类那样被实例化。

简单地说,是一种专用类型,用于封装对象的属性和行为。

维基百科可以给你一个更完整的答案:

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.

Simply speaking class is a specialized type used to encapsulate properties and behavior of an object.

Wikipedia can give you a more complete answer:

赴月观长安 2024-07-18 22:41:21

类型在概念上是类的超集。 从更广泛的意义上讲,类是类型的一种形式。

与类密切相关的是接口,它可以被视为一种非常特殊的类——纯粹的抽象类。 这些也是类型。

因此,“类型”包含类、接口,在大多数语言中也包含原语。 此外,像 dot-net CLR 这样的平台也有结构类型。

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.

甲如呢乙后呢 2024-07-18 22:41:21

以最快的方式说明它:

结构是一种类型,但结构不是类。

正如您所看到的,类型是一个“抽象”术语,不仅适用于类的定义,还适用于结构和基本数据类型(如 float、int、bool)。

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.

段念尘 2024-07-18 22:41:21

我认为类型是可以用特定值执行的一组操作。 例如,如果您有一个整数值,则可以将其添加到其他整数(或执行其他算术运算),或将其传递给接受整数参数的函数。 如果您有一个对象值,则可以调用其类定义的方法。

因为类定义了可以对该类的对象执行的操作,所以类定义了类型。 然而,类的意义远不止于此,因为它还提供了如何实现方法(类型未暗示的东西)以及如何布局对象的字段的描述。

另请注意,一个对象值只能有一个类,但它可以有多种类型,因为每个超类都提供对象类中可用功能的子集。

因此,虽然对象和类型密切相关,但它们实际上不是同一件事。

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.

甜尕妞 2024-07-18 22:41:21

类型通常指原始值的分类——整数、字符串、数组、布尔值、null等。通常,您不能创建任何新类型。

是指对象在创建时与之关联的命名属性和方法集。 您通常可以根据需要定义任意数量的新类,尽管在某些语言中您必须创建一个新对象,然后为其附加方法。

这个定义基本上是正确的,但是一些语言尝试以各种方式组合类型和类,并产生了各种有益的结果。

Type generally refers to the classification of primitive values - integers, strings, arrays, booleans, null, etc. Usually, you can't create any new types.

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.

酒浓于脸红 2024-07-18 22:41:21

添加另一个区别示例:在 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"

请注意,仅涉及一个类,但可以使用近乎无限数量的类型。 在某些语言中,函数被视为“第一类对象”,在这种情况下,函数的类型是类。 在其他情况下,函数的类型只是一个指针。 类通常具有能够保存数据以及对该数据进行操作的概念。

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.

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.

苏辞 2024-07-18 22:41:21

我的想法与 aku 的回答非常一致。

我将类视为构建对象的模板,而类型是对这些对象进行分类的一种方式,并为我们提供了它们的接口。

Python 还添加了元类,这只是构建类的一种机制,就像类构建对象一样(好吧,类和元类都是对象)。

在我看来,对兰巴终极中同一问题的回答完美的解释。

My thoughts are pretty much in line with aku's answer.

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).

This response to the same question in lamba the ultimate seems to me like a perfect explanation.

天暗了我发光 2024-07-18 22:41:21

C 中的类型(例如 Int Float、char 等)定义了可以使用可对其进行操作的特定方法进行操作的数据。 没有比这更复杂的了。 就像 int 一样,我可以加、减、乘,也许还可以除。 这些是我的 int 方法(或操作)。 类只是新类型的定义。 我首先定义数据是什么样的。 也许只是一点点。 也许这是两个词,就像一个具有实部和虚部的复合体。 或者也许是这个复杂的东西,有 309734325 字节代表木星上一个奇怪粒子的原子组成。 我不在乎。 就像整数一样,我可以用这种新数据类型进行操作。 对于整数,我进行了加法、减法等操作。使用这种新的数据类型,我可以定义我认为有意义的任何操作。 他们可能会加减法等,但他们可能会添加其他东西。 这些是我决定添加到我的类中的任何方法。

最重要的是,对于 C 中的类型,您可以定义数据是什么,即; 字节、字、浮点数、字符等。但其中任何一个也意味着哪些操作是合法的并且会产生可靠的结果。

类没有什么不同,只是由您来定义接口和可接受的操作。 类定义了这些东西,当您在对象中实例化它时,它定义了对象的行为,就像类型定义定义了对整数进行操作时的行为一样。

类只是让您能够灵活地定义新类型以及有关它们如何操作的所有内容。

一旦定义了这个,每次我实例化“thingy”类的对象时,它都会具有我定义的数据结构和我说你可以用它执行的操作(方法)。 “thingy”类显然只不过是 C++ 让我定义的一个新类型。

Types in C, like Int Float, char etc define data that can be acted on with specific methods that can operate on them. It's no more complicated than that. Like for int I can add, subtract multiply and maybe divide. Those are my methods (or operations) for int. A Class is simply a definition of a new type. I first define what the data looks like. Maybe its a single bit. Maybe it's two words like a complex with a real and imaginary part. Or maybe its this complex thingy with 309734325 bytes representing the atomic makeup of a weird particle on Jupiter. I don't care. Just like an integer, I get to make up the operations I can do with this new data type. In the case of the integer I had add, subtract, etc. With this new data type I can define whatever operations I think make sense. They might be add subtract etc. but they may add other things. These are whatever methods I decide to add to my class.

The bottom line is that with a type in C, you have a definition of what the data is, ie; a byte, word, float, char etc. But any of these also implies what operations are legal and will produce reliable results.

A class is no different except it is up to you to define the interface and acceptable operations. The class defines these things and when you instantiate it in an Object it defines the behavior of the object just like a type definition defines the behavior of an integer when you operate on it.

Classes just give you the flexibility to define new types and everything about how they operate.

Once this is defined, every time I instantiate an object of class "thingy", it has the data structure I defined and the operations (methods) that I said you can do with it. The class "thingy" is clearly nothing more or less than a new type that C++ lets me define.

剧终人散尽 2024-07-18 22:41:21

类型和类相关但不相同。 我的看法是,类用于实现继承,而类型用于运行时替换。

这里是一个链接,解释了替换原则以及为什么子类和子类型并不总是相同的(例如在Java中)。 关于协方差和逆变的 wikipedia 页面提供了有关此区别的更多信息。

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.

你是暖光i 2024-07-18 22:41:21

在像 Haskell 这样的语言中,类的概念不存在。 它只有类型。 (还有类型类。不要与混淆,类型类更多的是类型的抽象版本)。

Monad 是一个类型类

class Monad m where
  (>>=)  :: m a -> (  a -> m b) -> m b
  (>>)   :: m a ->  m b         -> m b
  return ::   a                 -> m a
  fail   :: String -> m a

从(纯)函数式编程的角度来看,类型比类更基础,因为我们可以将其根源追溯到类型理论 (例如,从 PTL 的角度来看,具有类型和不具有类型的 lambda 演算的行为完全不同),而 Class 实际上只是一个启用 OO 的构造。

在只支持 Type 而不支持 Class 的语言中,函数通常被视为一等公民。

同时,当一种语言区分类型和类时,函数更像是可以附加到对象等的二等公民。是的,通常您可以将函数附加到类本身(也称为静态函数) 。

In langugages like Haskell, the concept of Class doesn't exist. It only has Types. (And Type Class. Not to be confused with Class, Type Class is more of an abstracted version of Type).

Monad is a Type Class.

class Monad m where
  (>>=)  :: m a -> (  a -> m b) -> m b
  (>>)   :: m a ->  m b         -> m b
  return ::   a                 -> m a
  fail   :: String -> m a

From a (pure) functional programming perspective, Type is more fundemental than Class as one can trace its root to Type Theory (e.g. from a PTL perspective, lambda calculus with types and without types behave quite differently), while Class is really just a construct to enable OO.

In languages that only support Type and don't support Class, functions are often treated as first-class citizen.

Meanwhile, when a language makes a distinction between Type and Class, functions are more of a second-class citizens that can be attached to Objects, etc. And yup, often you can attach a function onto a Class itself (aka a static function).

倾听心声的旋律 2024-07-18 22:41:21

有趣的问题。 我认为阿库的回答是正确的。 以 java ArrayList 类为例

public class ArrayList<E> extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable

ArrayList 类的实例被认为是它扩展的每个超类和它实现的每个接口的类型。 因此,ArrayList 类的实例具有ArrayListRandomAccessCloneable 等类型。 换句话说,值(或实例)属于一种或多种类型,类定义这些类型是什么。

Interesting question. I think aku's answer is spot on. Take the java ArrayList class as an example

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.

我的奇迹 2024-07-18 22:41:21

不同的类可以描述相同的类型。

类型由以下部分组成:

  1. 操作=语法
  2. 操作描述=语义

类由以下部分组成:

  1. 操作=语法
  2. 实现(=各种实现描述相同的语义)

一些注意事项:

  • 接口(如 Java 中)不是类型,因为它不描述语义(仅描述语法)

  • < p>子类不是子类型,因为子类可以改变超类中定义的语义,子类型不能改变超类型语义(参见里氏替换原则,例如 这个 LSP 示例)。

Different classes may describe the same type.

Type consists of these parts:

  1. Operations = syntax
  2. Description of operations = semantics

Class consists of these parts:

  1. Operations = syntax
  2. Implementation (= various implementations describe same semantics)

Some notes:

  • Interface (as in Java) is not type, because it does not describe semantics (describes only syntax)

  • 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).

菊凝晚露 2024-07-18 22:41:21

显然,由于存在类型系统的语言不是面向对象编程语言,因此 type 必须是比 class 更广泛的概念,

即使在 Java、int 是一个(原始)类型,但不是一个类。

因此:每个类都是一个类型,但并非每个类型都是一个类。

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.

Hence: every class is a type, but not every type is a class.

川水往事 2024-07-18 22:41:21

如果我们在 C# 上下文中思考这个问题,我们会得到以下答案。

C# 类型系统分为以下几类:

值类型:

  • 简单类型:如 int、long、float 等
  • 枚举
  • 类型
  • 结构类型可空类型

引用类型:

  • 类类型
  • 接口类型
  • 数组类型
  • 委托类型

如您所见,有很多类型C#中的Class只是其中之一。
只有一个重要的注意事项:
C# 的类型系统是统一的,任何类型的值都可以被视为对象。 C#中的每个类型都直接或间接地派生自object类类型,而object是所有类型的最终基类。 只需将引用类型的值视为类型对象即可将其视为对象。 通过执行装箱和拆箱操作,将值类型的值视为对象。

正如我所见,类型是许多项目的保护伞,而类就是其中之一。

参考:CSahrp 语言规范文档,第 4 页

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

久而酒知 2024-07-18 22:41:21

这对我来说是一个很好的问题,这让我认真思考。 我敢说 Class 是一个编译时的东西,而 Type 是一个运行时的东西。 我这样说是因为你编写的是类而不是类型。 然后,编译器从类创建类型,运行时使用类型创建对象的实例。

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.

狼性发作 2024-07-18 22:41:21

类型是编程结构,可帮助编译器执行类型检查并确保变量具有适合操作的正确属性。

类是用户定义的类型,引用它们的对象或变量可以具有这些类型。 这些也受到类型检查。

types are programming constructs that helps the compiler to perform type checking and ensure that the variables have the right properties for an operation.

classes are user defined types that an objects or variables referencing them could have. These are also subjected to type checking.

岁吢 2024-07-18 22:41:20

以下答案来自Gof书(设计模式

对象的定义了如何
对象已实现。 班上
定义对象的内部状态和
其实施
操作。

相反,一个对象的
type 仅指其接口 - a
它可以处理的一组请求
回复。

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

//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函数需要一个具有操作>的类型,并以其自己的类型作为其接口之一,任何满足上述要求的类都可以用来生成特定的max<特定类/基元类型>; 该类的函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文