Smalltalk 超类与元类?

发布于 2024-08-19 01:03:38 字数 175 浏览 10 评论 0原文

我是面向对象编程的新手,但具有“程序”背景。

我目前正在尝试通过 GNU Smalltalk 和 Lovejoy 的“Smalltalk:获取消息”来了解 OOP。

我对元类和元类与超类到底是什么感到困惑。我可以看到超类的继承流程->类->子类;但我不明白元类如何/在哪里适合。TIA ...

I new to OOP, but with a "procedural" background.

I'm currently trying to get my head around OOP via GNU Smalltalk and Lovejoy's "Smalltalk: Getting The Message".

I'm confused as to the the heck the metaclass and Metaclass class are, vs superclass. I can see the inheritance flow of superclass -> class -> subclass; but I don't see how/where metaclass fits in. TIA...

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

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

发布评论

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

评论(4

心奴独伤 2024-08-26 01:03:38

免费在线书籍中有一个很好的描述 Pharo 示例,第 10 章(Pharo 对象模型)。本章中解释的内容对于所有 Smalltalk 实现都是通用的。

There is an excellent description in the free online book Pharo by Example, Chapter 10 (The Pharo object model). The things explained in this chapter are common to all Smalltalk implementations.

瑾兮 2024-08-26 01:03:38

实际上有两个层次的继承:实例继承和类继承。

Smalltalk 有一个特殊的方案,可以将作为对象传递。这意味着类本身也是对象。元类“简单地”是类对象的类。

它不会干扰正常的实例继承,因此它不适合超类 -> 中的任何位置。类->您使用的子类图。

There are actually two levels of inheritance: instance inheritance and class inheritance.

Smalltalk has a special scheme that makes it possible to pass around classes as objects. That means classes are also objects in their own rights. The metaclass is "simply" the class of the class object.

It doesn't interfere with normal instance inheritance, so it doesn't fit anywhere in the superclass -> class -> subclass diagram you used.

铁憨憨 2024-08-26 01:03:38

基于类的面向对象中有两种不同关系:实例化继承

实例化是对象与其类之间的关系,new关键字等。通常它是通过低级表示中的指针来实现的任何物体的。在Smalltalk中,一个Object类遍历这个指针;类也是对象,类的类称为元类,但这与实例的关系相同。

继承是类之间的关系。您可以通过执行aClass superclass从一个类转到它的超类,并继续这样做,直到到达类Object。在Smalltalk中,超类指针只是在所有类上定义的实例变量,而superclass消息是一个普通的访问器。

There are two different relations in class-based OO: instantiation and inheritance.

Instantiation is the relation between an object and its class, the new keyword, etc. Usually it's implemented by a pointer in the low-level representation of any object. In Smalltalk, anObject class traverses this pointer; it also happens that classes are also objects, and classes of classes are called metaclasses, but this is the same relation as with instances.

Inheritance is a relationship between classes. You can go from a class to its superclass by doing aClass superclass, and keep doing so until you get to the class Object. In Smalltalk, the superclass pointer is just an instance variable defined on all classes, and the superclass message is a normal accessor.

本宫微胖 2024-08-26 01:03:38

作为一个概念,超类是对象类的父类。即类层次结构中比当前对象的类高一级的类。

作为命名方法,它返回接收者的直接超类的名称。
例如,它在 Squeak Smalltalk(及其衍生品 Pharo 和 Cuis)中定义为
超类
“回答接收者的超类,一个类。”
^superclass

在 Dolphin Smalltalk 中,它被定义为
`超类
“回答a,这是接收者的即时消息
超类(或者如果没有的话)。”

^superclass'

但是 - 类层次结构中的每个类实际上都是其父类的实例。因此,给定类是其实例的类,是该类的元类。

因此,例如
aSortedCollection 是一个对象 - SortedCollection 类的实例。

SortedCollection 是一个类 - 在可浏览的类层次结构中名为“SortedCollection”类。同时,它也是元类的一个实例 - 一个匿名类,它有一个单例对象实例,该实例是一个命名类。命名类在类层次结构中可见,但元类(匿名)则不太可见。正是在那里,Smalltalk

Smalltalk 维护了元类层次结构,即类的类的层次结构。它不太明显,因为它作为匿名系统对象保存,但您可以在类浏览器中找到元类层次结构的顶层。 Class Class 和 Class Metaclass 都可以作为 Class Behaviour 类的子类找到,而 Class Behaviour 本身是 Class 的子类对象

人们说“在 Smalltalk 中,一切都是对象”的原因之一是因为类 Object 是所有其他类和对象的根 - 它位于对象层次结构的最顶层,其中包含类层次结构和元类层次结构。

(通常在这个阶段,我的大脑开始从耳朵流血,但以下 3 点有助于将其全部推回我的头骨)

如果您发送消息 anInstanceOfAClass class - 您会获取
返回的对象 anInstanceOfAClass 的类。

如果您发送消息 anInstanceOfAClass class superclass - 您将得到
返回的对象 anInstanceOfAClass 的类的父类。

如果您发送消息 anInstanceOfAClass class class - 您将得到
返回的对象 anInstanceOfAClass 的类的匿名单例元类。

As a concept, a superclass is the parent of an object's Class. i.e. the Class one level higher in the Class hierarchy than the Class of the current object.

As a named method, it returns the name of the immediate superclass of the receiver.
e.g. it's definedin Squeak Smalltalk (and also in its derivatives, Pharo and Cuis) as
superclass
"Answer the receiver's superclass, a Class."
^superclass

In Dolphin Smalltalk, it's defined as
`superclass
"Answer a which is the receiver's immediate
superclass (or if none)."

^superclass'

But - every Class in the Class hierarchy is actually an instance of its parent class. So the Class that a given Class is an instance of, is the Class's MetaClass.

So, e.g.
aSortedCollection is an object - an instance of the Class SortedCollection.

SortedCollection is a Class - named Class 'SortedCollection' in the browsable Class hierarchy. Simultaneously, it is also an instance of a Metaclass - an anonymous Class which has a singleton object instance, which is a named Class. The named class is visible in the Class hierarchy, but the Metaclass (being anonymous) is much less visible. It is there so that Smalltalk

Smalltalk maintains a Metaclass hierarchy, i.e. a hierarchy of the Classes of the Classes. It's much less visible, as it's held as anonymous system objects, but you can find the top level of the Metaclass hierarchy in the Class browser. Both the Class Class and the Class Metaclass are to be found as sub-sub-classes of Class Behaviour, itself a sub-class of Class Object.

One reason that people say that "In Smalltalk, everything is an object" is because Class Object is the root of all the other Classes and objects - it is at the very top of the object hierarchy, which contains the Class hierarchy, and the Metaclass hierarchy.

(It's generally at this stage that my brain begins to bleed out of my ears, but th following 3 points help push it all back in to my skull)

If you send the message anInstanceOfAClass class - you'll get
the Class of the object anInstanceOfAClass returned.

If you send the message anInstanceOfAClass class superclass - you'll get
the parent Class of the Class of the object anInstanceOfAClass returned.

If you send the message anInstanceOfAClass class class - you'll get
the anonymous singleton Metaclass of the Class of the object anInstanceOfAClass returned.

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