简体   繁体   English

Smalltalk超类vs元类?

[英]Smalltalk superclass vs metaclass?

I new to OOP, but with a "procedural" background. 我是OOP的新手,但具有“程序”背景。

I'm currently trying to get my head around OOP via GNU Smalltalk and Lovejoy's "Smalltalk: Getting The Message". 我目前正试图通过GNU Smalltalk和Lovejoy的“Smalltalk:获取消息”来了解OOP。

I'm confused as to the the heck the metaclass and Metaclass class are, vs superclass. 我对metaclass和Metaclass类与超类相比很困惑。 I can see the inheritance flow of superclass -> class -> subclass; 我可以看到超类的继承流 - > class - > subclass; but I don't see how/where metaclass fits in. TIA... 但是我不知道元类是如何适应的.TIA ......

There is an excellent description in the free online book Pharo by Example , Chapter 13 (Classes and metaclasses). 在免费的在线书籍Pharo by Example ,Chapter 13(Classes and metaclasses)中有一个很好的描述。 The things explained in this chapter are common to all Smalltalk implementations. 本章中解释的内容对于所有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. Smalltalk有一个特殊的方案,可以将作为对象传递。 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. 它不会干扰正常的实例继承,因此它不适合您使用的superclass -> class -> subclass图中的任何位置。

There are two different relations in class-based OO: instantiation and inheritance . 基于类的OO有两种不同的关系: 实例化继承

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. 实例化对象与其类new关键字等之间的关系。通常它是由任何对象的低级表示中的指针实现的。 In Smalltalk, anObject class traverses this pointer; 在Smalltalk中, anObject class遍历此指针; 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 . 您可以通过执行aClass superclass从一个类转到它的超aClass superclass ,并继续这样做,直到你到达Object类。 In Smalltalk, the superclass pointer is just an instance variable defined on all classes, and the superclass message is a normal accessor. 在Smalltalk中,超类指针只是在所有类上定义的实例变量,而superclass消息是普通访问器。

As a concept, a superclass is the parent of an object's Class. 作为一个概念, superclass是对象superclass的父类。 ie the Class one level higher in the Class hierarchy than the Class of the current object. 即Class层次结构中的Class级别高于当前对象的Class级别。

As a named method, it returns the name of the immediate superclass of the receiver. 作为命名方法,它返回接收者的直接超类的名称。 eg it's definedin Squeak Smalltalk (and also in its derivatives, Pharo and Cuis) as superclass "Answer the receiver's superclass, a Class." ^superclass 例如,它在Squeak Smalltalk(以及它的衍生物,Pharo和Cuis)中被定义为superclass "Answer the receiver's superclass, a Class." ^superclass 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)." 在Dolphin Smalltalk中,它被定义为“超类”答案是接收者的直接超类(或者如果没有)。“

^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. 因此,给定类是其实例的类是Class的MetaClass。

So, eg aSortedCollection is an object - an instance of the Class SortedCollection. 因此,例如aSortedCollection是一个对象 - Class SortedCollection的一个实例。

SortedCollection is a Class - named Class 'SortedCollection' in the browsable Class hierarchy. SortedCollection是可浏览的类层次结构中的类命名类“SortedCollection”。 Simultaneously, it is also an instance of a Metaclass - an anonymous Class which has a singleton object instance, which is a named Class. 同时,它也是 Metaclass的一个实例 - 一个匿名类,它有一个单例对象实例,它是一个命名类。 The named class is visible in the Class hierarchy, but the Metaclass (being anonymous) is much less visible. 命名类在类层次结构中可见,但Metaclass(匿名)更不可见。 It is there so that Smalltalk 它就是Smalltalk

Smalltalk maintains a Metaclass hierarchy, ie a hierarchy of the Classes of the Classes. Smalltalk维护一个Metaclass层次结构,即类的类的层次结构。 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. 它不太可见,因为它作为匿名系统对象保存,但您可以在类浏览器中找到Metaclass层次结构的顶级。 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 . Class Class和Class Metaclass都被认为是Class Behaviour的子类,它本身就是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. 人们说“在Smalltalk中,一切都是对象”的一个原因是因为类Object是所有其他类和对象的根 - 它位于对象层次结构的最顶层,它包含类层次结构和元类层次结构。

(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) (通常在这个阶段,我的大脑开始从我的耳朵流血,但是以下3点帮助将它全部推回到我的头骨中)

If you send the message anInstanceOfAClass class - you'll get 如果你发送消息anInstanceOfAClass class - 你会得到
the Class of the object anInstanceOfAClass returned. 返回的对象anInstanceOfAClass的类。

If you send the message anInstanceOfAClass class superclass - you'll get 如果你发送消息anInstanceOfAClass class superclass - 你会得到
the parent Class of the Class of the object anInstanceOfAClass returned. 返回的对象anInstanceOfAClass的类的父类。

If you send the message anInstanceOfAClass class class - you'll get the anonymous singleton Metaclass of the Class of the object anInstanceOfAClass returned. 如果发送消息anInstanceOfAClass class class - 您将获得对象anInstanceOfAClass返回的类的匿名单例Metaclass。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM