Java 和 C# 中的一等对象是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1599176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:14:04  来源:igfitidea点击:

What are first-class objects in Java and C#?

c#javafirst-class

提问by peter.murray.rust

When I started OO programming many years ago I gained the impression that variables (if that is the right word) were either "primitives" (int, double, etc.) or first-class objects (String, JPane, etc.). This is reinforced by a recent answer on primitives in Java and C# (@Daniel Pryden: Are primitive types different in Java and C#?). However don't know whether C# ValueTypes are primitives, objects or some other beast such as second-class objects. I see that SO has only one use of the first-classtag so maybe it is no longer a useful term.

多年前我开始 OO 编程时,我的印象是变量(如果这是正确的词)要么是“原始”(int、double 等),要么是一流的对象(String、JPane 等)。最近对 Java 和 C# 中的原语的回答加强了这一点(@Daniel Pryden:Java 和 C# 中的原语类型是否不同?)。但是不知道 C# ValueType 是基元、对象还是其他一些野兽,例如二类对象。我看到 SO 只有一个first-class标签的用途,所以它可能不再是一个有用的术语。

I did not find the Wikipedia articleuseful ("This article is in need of attention from an expert on the subject."). I'd be grateful for a taxonomy and current usage of terms, primarily related to Java and C# (though maybe other languages will shed enlightenment).

我没有发现维基百科文章有用(“这篇文章需要专家的关注。”)。我很感激术语的分类和当前用法,主要与 Java 和 C# 相关(尽管其他语言可能会有所启发)。

Clarification: I'd like to understand the term first-classand what its range of use is.

说明:我想了解一流的术语及其使用范围。

采纳答案by Stephen C

The problem is that "first class object" is not a well defined concept.

问题是“一流的对象”不是一个定义明确的概念。

The normal usage is that someone says that an "object" is a class of thing that shouldhave all of the properties X, Y and Z. But there are other things that don't have all of those properties, but they are sort of object-ish. So we'll call the former "first class" objects and the rest not "first class" ... and may be not objects.

通常的用法是有人说“对象”是一类应该具有所有属性 X、Y 和 Z 的事物。但是还有其他事物没有所有这些属性,但它们有点像对象式的。因此,我们将前者称为“一流”对象,而其余的称为“一流”……并且可能不是对象。

The problem is that there are any number of views on the properties that a thing needs to have to make it a "first class" object. And no prospect of the people with opposing views coming to a consensus. (For example, a Javascript language expert might argue strenuously that an object is only first class if it is template-based.)

问题是对于一个事物需要拥有的属性有任意数量的视图,以使其成为“第一类”对象。反对意见的人也没有希望达成共识。(例如,Javascript 语言专家可能会极力争辩说,如果对象是基于模板的,则它只是一流的。)

The only really solid insights about "first-classness" will be those that you can glean from the respective language specifications for Java and C#. And they only really apply within the scope of the respective languages / type systems ... and not across multiple languages.

关于“一流”的唯一真正可靠的见解将是您可以从 Java 和 C# 的相应语言规范中收集到的见解。而且它们只真正适用于各自语言/类型系统的范围内……而不是跨多种语言。

So "first class Java object" or "first class C# object" might be meaningful, but "first class object" taken out of context is not.

因此,“一流的 Java 对象”或“一流的 C# 对象”可能有意义,但脱离上下文的“一流的对象”则不然。

Well that's my opinion ...

嗯,这就是我的意见......

回答by J?rg W Mittag

The notion of "first-class citizen" or "first-class element"in a programming language was introduced by British computer scientist Christopher Stracheyin the 1960s in the context of first-class functions. The most famous formulation of this principle is probably in Structure and Interpretation of Computer Programsby Gerald Jay Sussman and Harry Abelson:

编程语言中“一等公民”或“一等元素”的概念是由英国计算机科学家Christopher Strachey在 1960 年代在一流函数的背景下引入的。这一原则最著名的表述可能是 Gerald Jay Sussman 和 Harry Abelson在计算机程序结构和解释中

  • They may be named by variables.
  • They may be passed as arguments to procedures.
  • They may be returned as the results of procedures.
  • They may be included in data structures.
  • 它们可以通过变量命名。
  • 它们可以作为参数传递给过程。
  • 它们可能作为程序的结果返回。
  • 它们可能包含在数据结构中。

Basically, it means that you can do with this programming language element everything that you can do with all other elements in the programming language.

基本上,这意味着您可以使用此编程语言元素执行您可以使用该编程语言中的所有其他元素执行的所有操作。

回答by Konamiman

In .NET you don't have primitive types vs classes. Instead, you have structs vs classes, but structs share many of the features of classes (such as the ability to have properties and methods), and inherit from the Objectclass as well.

在 .NET 中,您没有原始类型与类。相反,您有结构与类,但结构共享类的许多功能(例如具有属性和方法的能力),并且也从Object类继承。

When you write intin C#, for example, it is just a language shortcut for the Int32struct. You can do for example int i=int.Parse("34"), or even string s=1234.ToString(). In order to assign struct instances to variables of type Object, there is the boxing/unboxing mechanism.

int例如,当您用C#编写时,它只是Int32结构体的语言快捷方式。例如int i=int.Parse("34"),您可以这样做,甚至string s=1234.ToString()。为了将结构实例分配给类型的变量Object,有装箱/拆箱机制

In Java, on the other hand, you have indeed the primitive types vs classes dicotomy. So for example to perform operations on a variable of type int, you must use the auxiliary Integerclass. That's one of the things that I don't like of Java compared to .NET.

另一方面,在 Java 中,确实存在原始类型与类的二分法。因此,例如要对 type 变量执行操作int,您必须使用辅助Integer类。与 .NET 相比,这是我不喜欢 Java 的地方之一。

EDIT. When you read about "first-class objects" (or classes), it means "fully-powered objects", that is, classes that have the same capabilities as any other system classes or user-made classes. This is to distinguish from "limited primitive types".

编辑。当您读到“一流的对象”(或类)时,它的意思是“完全强大的对象”,即与任何其他系统类或用户创建的类具有相同功能的类。这是为了区别于“有限的原始类型”。

回答by Tarik

For each primitive data type in Java, the core class library provides a wrapper class that represents it as a Java object. For example, the Int32 class wraps the int data type, and the Double class wraps the double data type.

对于 Java 中的每种原始数据类型,核心类库都提供了一个包装类,将其表示为 Java 对象。比如Int32类包装了int数据类型,Double类包装了double数据类型。

On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int is the short name for System.Int32 and double is the short form of System.Double.

另一方面,C# 中的所有原始数据类型都是 System 命名空间中的对象。对于每种数据类型,都提供了一个短名称或别名。例如,int 是 System.Int32 的简称,double 是 System.Double 的简称。

The list of C# data types and their aliases is provided in the following table. As you can see, the first eight of these correspond to the primitive types available in Java. Note, however, that Java's boolean is called bool in C#.

下表提供了 C# 数据类型及其别名的列表。如您所见,其中的前八个对应于 Java 中可用的原始类型。但是请注意,Java 的布尔值在 C# 中称为 bool。

From : http://msdn.microsoft.com/en-us/library/ms228360%28VS.80,lightweight%29.aspx

来自:http: //msdn.microsoft.com/en-us/library/ms228360%28VS.80,lightweight%29.aspx

回答by Ilya Khaprov

http://onjava.com/onjava/2003/05/21/delegates.html

http://onjava.com/onjava/2003/05/21/delegates.html

in other words c# methods are first class object because we can pass it in another method. we can use methods like any other values(strings, numbers, user-created object).

换句话说,c# 方法是第一类对象,因为我们可以在另一个方法中传递它。我们可以像任何其他值(字符串、数字、用户创建的对象)一样使用方法。

Another example of first class objects that u can find uncommon in other languages but c# is Expressions

您可以在其他语言中发现不常见的一流对象的另一个示例,但 C# 是表达式

回答by PhiLho

Frankly, I have no idea of what a "first-class object" is...
But I first found usage of a similar idiom in Lua documentation and mailing list, saying that functions are first-class citizens, or first-class values.

坦率地说,我不知道什么是“一流的对象”……
但我首先在 Lua 文档和邮件列表中发现了类似的用法,说函数是一流的公民,或一流的值。

I let one of the authors of Lua to explain what it is: Programming in Lua : 6 - More about Functions

我让 Lua 的一位作者解释它是什么:Lua 中的编程:6 - 有关函数的更多信息

It means that, in Lua, a function is a value with the same rights as conventional values like numbers and strings. Functions can be stored in variables (both global and local) and in tables, can be passed as arguments, and can be returned by other functions.

这意味着,在 Lua 中,函数是与数字和字符串等常规值具有相同权限的值。函数可以存储在变量(全局和本地)和表中,可以作为参数传递,也可以由其他函数返回。

Somehow, this definition applies to objects in Java: you can store them in variables, in arrays, use them as function parameters and return them, use them as key of HashMap and other collections, etc.
Not sure if that's how the term is used for objects, but at least it makes sense... :-)

不知何故,这个定义适用于 Java 中的对象:你可以将它们存储在变量中,在数组中,将它们用作函数参数并返回它们,将它们用作 HashMap 和其他集合的键等。
不确定这个术语是否是这样使用的对于对象,但至少它是有道理的...... :-)

In a language like C, objects have to be made from scratch, using some tricks (re-creating C++, somehow...), so they are not first-class: you have to pass pointers around to manipulate them.

在像 C 这样的语言中,对象必须从头开始创建,使用一些技巧(重新创建 C++,不知何故......),所以它们不是一流的:你必须传递指针来操作它们。

回答by alsor.net

When we're talking about "first-class objects" by "objects" we mean some concepts of the language, not the objects that we create in that language. That is why there is also such terms like "first-class citizens".

当我们用“对象”谈论“一流的对象”时,我们指的是语言的一些概念,而不是我们用该语言创建的对象。这就是为什么还有“一等公民”这样的术语。

So, for example, Java has following concepts - Java-objects, Java-primitives, fields, methods and other (by Java-objects I mean anything that is instance of Object type). I'd say that in Java both Java-objects and Java-primitives are first-class citizens in the language.

因此,例如,Java 具有以下概念 - Java 对象、Java 原语、字段、方法和其他(Java 对象我指的是任何对象类型的实例)。我想说的是,在 Java 中,Java 对象和 Java 原语都是该语言中的一等公民。

In C# we have some additional concepts that we can "test" for first-class properties. For example, delegates. We can assign delegate ot variable (give a name), pass it to the method as an argument, return it from method, incorporate in data structures (have a Dictionary of delegates for example). So I think we can say that delegates are first-class objects in C#. You can continue for other concepts of C# - events, properties...

在 C# 中,我们有一些额外的概念,我们可以“测试”一流的属性。例如,代表。我们可以分配委托 ot 变量(给出一个名称),将它作为参数传递给方法,从方法返回它,合并到数据结构中(例如有一个委托字典)。所以我认为我们可以说委托是 C# 中的一流对象。您可以继续了解 C# 的其他概念 - 事件、属性...

Functional languages have concept of "function" and of course it is a first-class citizen in the any functional language. I'd say that we can call language a functional language if it has "function" as a first-class concept (name, pass, return, incorporate...).

函数式语言有“函数”的概念,当然它是任何函数式语言中的一等公民。我想说,如果语言将“函数”作为一流的概念(名称、传递、返回、合并……),我们就可以称它为函数式语言。

So, if some language bring some concepts we can "measure" the power of this concepts in the language it self.

所以,如果某种语言带来了一些概念,我们可以“衡量”这些概念在语言本身中的力量。