Object-oriented language
(Translated from Martin Abadi, Luca Cardelli Object Theory Book 1)
Translator's preface
This book is our textbook we have to object type. At the time, when this class was full, I didn't think of it. I feel that my C and OO have been quite paper, C and Java type system does not say back to the back, and the light car is ripe. Is this lesson not naivin? Haha!
But let's get up, I found myself like a frog in the well. Old days, it turned out that there is so much way to say so much! In addition to C , Java, there are so many things that have never seen anything else!
The first few chapters are barely to get it. But behind the modeling system model, the proof of Subject Reduction found that he was like returning to the undergraduate era, this, this, this is mathematics!
These two days of heartbroken tide. I want to translate it. The hard place behind the back is too shallow, and it is not self-tempered. However, you can translate the instead of the previous chapters. If you can help everyone the role of your eyes, you will not be white.
Chapter 2, based on class-oriented language
The class-oriented language is the mainstream of the object-oriented world. it includes:
Simula, the first object-oriented language
Smalltalk, the first language that supports dynamic types
C , most of its class-based characteristics inheritance from SIMULA.
Etc., etc.
Corresponding to the class-based language is based on object-oriented language. The "object-based" concept is different from the concept of the Visual Basic-based object. The "object-based" based on the object here refers to a language that is only the concept of the object, there is no class, similar to the language like Python.
Now let's introduce some common features based on class-oriented language-oriented language.
1. Class and object
Let us first look at a class definition:
Class Cell IS
VAR Contents: = 0;
Method get (): Integer IS
Return Self.Contents;
END;
Method Set (N: Integer) IS
Self.Contents: = N;
END;
END;
A class is a common structure for describing all objects belonging to this class. This Cell class represents an integer attribute called Contents, which is initialized into 0. It also describes two ways to operate CONTENTS. GET and SET. The content of these two methods is very intuitive. The Self variable represents this object yourself.
The dynamic semantics of the object can be understood:
An object is represented internally as a pointer to a set of properties. Any of the operations of this object will pass through this pointer to operate objects. When the object is assigned or transmitted as a parameter, the transmitted is only a pointer, so that the same set of properties can be shared.
(Note, some languages such as C , clearly distinguish between the pointers and attribute groups themselves, and some other languages hide this difference)
Objects can be instantiated from a class from a class. Quantitatively, New C allocates a set of properties.
And returns a pointer to this group of properties. This group of properties is given the initial value and includes code of the method defined by class C.
Let's consider the type. For an object generated by New C, we record its type as InstanceTypeOf (c). An example is:
Var mycell: instancetypeof (cell): = new cell;
Here, by introducing instancetypeof (Cell), we have begun to distinguish between Class and Type. We can also use the Cell itself as a type, but then, you will find that it will cause confusion. 2. Method analysis. (METHOD LOOKUP)
A method of calling o.m (...) is given, and a process called method parsing by each language is responsible for finding the correct method's code. (Translator Press: Do you think of VTABLE?).
Visually, the code's code can be embedded in each individual object, and for many object-oriented languages, similar grammar on attributes and methods, it is indeed impressive.
However, take into account space, very few languages. More popular methods are that language will generate a lot of Method Suite, and these Method Suite can be shared by the objects of the same class. The method parsing process will delay the pointer to the Method Suite in the object to find the method.
The method parsing will be more complicated in consideration of inheritance. Method Suite may be a tree, and resolution to a method may find a series of Method Suite. If you have multiple inheritance, Method Suite may even make a direction map, or a ring.
Method analysis may occur during compile, or it may occur during runtime.
In some languages, the method is in the embedded object, or exists in this detail in Method Suite, which is irrelevant to the programmer. Because all language features that can distinguish between two modes are generally not supported in class-oriented object language.
For example, the method does not take it out of the object as a function of the function as an attribute. Methods cannot be updated in objects like attributes. (That is, you update a method of an object, and this method of other objects of the same class remains unchanged.)
3. Subclass and inheritance (SUBCLASSING AND INHERITANCE)
Subclasses and general classes are also used to describe the structure of the object. However, it is progressively achieved by inheriting other classes.
The properties of the parent class are implicitly copied to subclasses, and the subclass can also add new properties. In some languages, subclasses can even operate the properties of the Override parent class (by changing the type of properties)
The method in the parent class can be copied to the subclass, or the subclass Override.
Examples of a subclass of code are as follows:
Subclass Recell of Cell IS
VAR backup: integer: = 0;
Override Set (N: Integer) IS
Self.backup: = self.contents;
Super.set (n);
END;
Method restore () IS
Self.Contents: = Self.Backup;
END;
END;
Analysis of Subclass's method is different depending on whether the language is static or dynamic type.
In static types of language (such as C , Java), the topology of the parent class, the subclass of Method Suite has been determined when compiling, so the method of the Method Suite of the parent class can be combined into the subclass of Method Suite Go, you don't have to search this Method Suite tree or picture when you analyze. (Translator Press: C VTABLE is this method)