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

发布于 2024-08-08 15:09:23 字数 572 浏览 6 评论 0原文

许多年前,当我开始面向对象编程时,我的印象是变量(如果这个词正确的话)要么是“基元”(int、double 等),要么是一等对象(String、JPane 等)。最近关于 Java 和 C# 中原语的回答强化了这一点(@Daniel Pryden: Java 和 C# 中的基本类型不同吗?)。但是不知道 C# ValueType 是否是基元、对象或其他一些野兽(例如二类对象)。我发现 SO 只有一次使用 first-class 标签,所以也许它不再是一个有用的术语。

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

澄清:我想了解“一流”一词及其使用范围。

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-class tag so maybe it is no longer a useful term.

I did not find the Wikipedia article useful ("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).

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

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

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

发布评论

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

评论(7

死开点丶别碍眼 2024-08-15 15:09:24

问题在于“第一类对象”不是一个明确定义的概念。

正常用法是,有人说“对象”是一类应该具有所有属性 X、Y 和 Z 的事物。但还有其他事物不具有所有这些属性属性,但它们有点像对象。所以我们将前面的称为“第一类”对象,其余的不是“第一类”......并且可能不是对象。

问题在于,对于一​​个事物要使其成为“一流”对象所需的属性,有许多种观点。持相反观点的人不可能达成共识。 (例如,Javascript 语言专家可能会极力争辩说,只有基于模板的对象才是一流的。)

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

因此,“第一类 Java 对象”或“第一类 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 should have 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.

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

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.

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

Well that's my opinion ...

淡淡绿茶香 2024-08-15 15:09:24

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

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

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

编辑。当您阅读“一流对象”(或类)时,它意味着“全功能对象”,即具有与任何其他系统类或用户创建的类相同功能的类。这是为了与“有限的原始类型”区分开来。

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 Object class as well.

When you write int in C#, for example, it is just a language shortcut for the Int32 struct. 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.

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 Integer class. That's one of the things that I don't like of Java compared to .NET.

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

活雷疯 2024-08-15 15:09:24

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

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

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

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

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.

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.

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

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

心奴独伤 2024-08-15 15:09:24

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

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

除了 C# 之外,你可以在其他语言中找到不常见的第一类对象的另一个例子是表达式

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

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

橘味果▽酱 2024-08-15 15:09:24

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

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

这意味着,在Lua中,函数是一个
具有相同权利的价值
常规值,例如数字和
字符串。函数可以存储在
变量(全局变量和局部变量)和
在表中,可以作为参数传递,
并且可以被其他人退回
功能。

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

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

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.

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

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.

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

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.

埋葬我深情 2024-08-15 15:09:24

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

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

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

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

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

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.

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

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.

初与友歌 2024-08-15 15:09:23

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

  • 它们可以通过变量命名。
  • 它们可以作为参数传递给过程。
  • 它们可以作为程序的结果返回。
  • 它们可能包含在数据结构中。

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

The notion of "first-class citizen" or "first-class element" in a programming language was introduced by British computer scientist Christopher Strachey in the 1960s in the context of first-class functions. The most famous formulation of this principle is probably in Structure and Interpretation of Computer Programs by Gerald Jay Sussman and 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.

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