所有面向对象的语言都会在内存中创建大量重复信息吗?

Do all object-oriented languages create a lot of duplicate information in memory?

我正在阅读一本关于 JS 和其他网络内容的书,这就是我遇到的关于原型关键字的内容:

The prototype keyword can save you a lot of memory. In the User class, every instance will contain the three properties and the method. Therefore, if you have 1,000 of these objects in memory, the method showUser will also be replicated 1,000 times. However, because the method is identical in every case, you can specify that new objects should refer to a single instance of the method instead of creating a copy of it.

任何面向对象的语言不都是这样的吗?我读过一些关于 C++、C# 的书,但从未被告知过。我认为在 class 中创建静态方法并从非静态方法调用它们,或者这在其他语言中不是问题,我什至不记得了?

在 C# 和其他静态类型语言中,每个 class 用户的方法都定义一次,然后该对象的每个实例化仅引用单个定义。因此,不需要与 JS 中的原型相同的构造。

Prototypes 是一种非常不同的面向对象编程方式。

使用这种范式,对象既包含代码又包含数据。这就是为什么如果您将对象复制数千次,就会有数千个方法副本。在更常见的继承范式中,类只有对其方法的引用,方法在对象之外。

很少有语言遵循这种范式,但 Javascript 就是其中之一。大多数 OOP 语言,如 C# 或 C++,没有您所说的那种原型,因此它们没有这个特殊问题。