Getting Started with C ++ and Java programmers

xiaoxiao2021-03-06  76

Getting Started with C and Java programmers

Original title: I Can Read C and Java But I Can't Read Smalltalk

Original author: Wilf LaLonde

Original link: http://www.eli.sdsu.edu/courses/spring01/cs635/readingsmalltalk.pdf

Introduction

Many people tell me that he is very familiar with C or Java, but completely read the code of SmallTalk. For them, SmallTalk is simply unable to understand! For this question, I have considered it for a long time and then the conclusion is that they are right. If I pick out some of my SMALLTalk code written for many years, then I only understand Java to read, I am sure I can't understand those code. In fact, it is necessary to read SmallTalk only to understand some very simple concepts, of course, some concepts are more subtle. If "Johnny reads smaltalk code", I have a way. My approach is to help novices quickly understand the concept of SmallTalk through practical examples. I assume that the reader understands what is object-oriented, and for those readers who have mastered SMALLTALK, I will pretend that I have not studied SMALLTALK.

Such simple grammar

Some grammar is easy to understand, such as using dual quotes to identify the gaze block; single quotes indicate strings, single characters plus $ (for example, $ x indicate character "x"). In addition, there is a concept of Symbol, which is a special string that is unique in the entire memory. When the source code is compiled, the compiler searches the entire memory. If the same Symbol is found, the existing one is used. There is no memory, but it is faster than Symbol (later explained).

"This is a comment"

'This is a string'

# 'This is a symbol'

#thisisasymboltoo

For assignment operations and equal numbers, the difference is not very big.

: = // Means Assignment

= // Means Equality Comparison

== // Means Identity Comparison

If you give me a reference to the object named by 'a' and 'b', I can tell you whether they are the same object (using a == b, naming equivalent) or looks the same substantially different objects ( Use a = B constructor equivalent). In an intuitive, == compares whether the addresses of the two reference objects are the same, and = then compares whether the entire contents of the two objects are the same.

SmallTalk code is rarely used in a comma because they are not part of the SmallTalk text method. That's why SmallTalk's array is no comma. E.g:

# (1 2 3 4 5)

However, it is good to be a operator of SmallTalk. So you will see that he is used to connect two strings, for example:

'String1', 'String2'

Unwanted keyword

The keyword in smalltalk is everywhere. The existence is to help understand the code rather than an increase. To understand how this is something, let's take a look at C and Java grammar. E.g,

T-> Rotate (A, V); // for C

T. Rotate (a, v); // for java

The above code to the object T sends a message (Note: That is the call class method) Rotate and specifies the parameters a and v. In order to understand this code, the reader usually needs to continue to see the declaration of variables of parameters and their types. Let us assume that it has the following statement: Transformation T;

Float a;

Vector v;

In SmallTalk, variables can reference any object, so there is no need to specify the type of variable when the variable is decremented. E.g:

| T a v |

Since there is no variable type, a good SmallTalk programmer uses a variable name that can hinder the type of variable. Therefore, the above declaration we usually said:

| ATRANSFORMATION ANGLE AVector |

But then allow me to continue using the previous variable name, because the magazine column is small to the layout of me. Let us further eliminate unnecessary things to continue "optimize" C and Java grammar. For example, the following code should still understand:

T.ROTATE (A, V); // original text

T rotate (a, v); // Who needs a period? (T and Rotate round dots)

T rotate a, v; // Who needs parentheses?

In order to further improve the above grammar, we need to know what parameters a and v are. Let us assume that the entire example is "Rotate the angle A around the endpoint V". Then the next step is as follows:

T.ROTATE BY a arround v; // Who needs a comma?

But how can you know what you mean in this statement? We know, in this example, T is a variable, Rotate is a name of a class method. By is a separator, A is a variable, and Around is another separator, and the last V is a variable. In order to eliminate ambiguity, we can assume that the following transformation: Add a colon after all separators. Then we get the following sentence:

T. Rotate by: a around: v; // Who needs ambiguity?

In the end, we ask all the separators to be part of the method name. That is, the name of the method we need is "Rotate By: Around:", finally let us go to the space, it will become "RotateBy: Around:". We'd better start the intermediate word, so "Rotateby: Around:", so our example will become:

T. Rotateby: a around: v; // this is SmallTalk

That is, the method name is divided into several parts. Fortunately, it is not difficult to imagine these parties into a whole. When a class method is defined, we may write to the following:

Self RotateBy: Angle Around: Vector

| Result |

RESULT: = compute anwser.

^ Result

At the time of execution, T and Self, A and Angle, V, and Vector have a one-to-one mapping relationship. It should be noted that ^ means returns, equivalent to the return keyword. Variable Self is equivalent to this. If the last no explicit return expression is not explicit, it is ^ Self. That is, there will be no problem without writing the returnite statement. This also means that the class method will return a reference to an object regardless of whether the caller needs it.

In fact, SmallTalk's grammar requires SELF to appear in front of the function name, as shown below:

RotateBy: Angle Around: Vector

| Result |

Result: = compute answer. ^ Result

The advantage of this keyword syntax of SmallTalk is that different keywords can be used for different methods. For example, we can define the second function like this:

T rotatearound: Vector by: angle

There is no need to deliberately remember the order of parameters, because the keyword hints their order. Of course, it is also possible to abuse keyword as a programmer. For example, if we define keywords like this:

T rotate: Angle and: Vector

Obviously, people who use this function cannot determine the order of the parameters through the keyword. This is a very bad programming style. If there is only one parameter, it doesn't matter. But we still need only one keyword name:

T Rotatearoundxby: Angle

T rotatearoundyby: Angle

I prefer to understand keywords as an illustrative parameter. But what should I do if there is no parameter?

T Makeidentity: // CAN a Colon At The End Make Sense?

If the keyword is an illustrative parameter, and the actual parameter is not, then we are not using the keyword. So a message with no keywords (class method) will be written below:

T Makeidentity // This Is Smalltalk

Of course, the keyword can also be used in the definition of binary operators (also class methods, similar to C languages), but the one-dollar operator is not available (the above MakeIdentity is a dollar message, not a one-dollar operator). When many messages are used simultaneously, we may get the following expression:

a negative | (B Between: C and: D)

IFTRUE: [A: = C NEGATED]

As a reader, I believe that you now know that the message NEGATIVE is sent to Object A (no parameters), then True or False will be returned; BetWeen: c and: D is sent to object B, and returns True or false. Then, the two returned results or the resulting result object is the recipient of the IFTRUE: [A: = C NEGATED] message. This is an If-kil condition expression, but it is not necessary to use a special syntax like C or Java. In SmallTalk, this is not a difference with other class methods, but the recipient of the message is a Boolean object, and the keyword of the message is iftrue. The parameter is [A: = C NEGATED] (this is actually A block object). In SmallTalk, you will not see A: = -c expressions because there is no one of the one-dollar operator. However, it can be written as a expression of -5, but the negative signage here is part of the constant definition.

Therefore, when you see an expression such as "-3.5 Negated Truncated Factorial, you should immediately realize that there is no keyword here. Therefore, -3.5 must be the recipient of the Negated message; the resulting result 3.5 is the recipient as a truncated message; further result 3 will be the recipient of the Factorial message, resulting results 6.

Of course, there is also a priority problem here, usually from left to right, the primary message priority, the highest priority of multi-parameter messages. This is important for programming people, but it does not affect its reading and understanding for readers. Oh, the priority from left to right is actually: 1 2 * 3 is equal to 9

There is no priority between operators. Of course, you may also see the following expression, that is because the writer knows that it may be difficult to do not familiarize with SmallTalk readers:

(1 2) * 3

Although parentheses here are not necessary.

Different sections and the number of completion

Most non-SmallTalk readers may think that the semicolon is the end of the expression, but is used in SmallTalk. So we will not write the following expression:

Account Deposit: 100 Dollars;

Collection Add: Transformation;

The correct way to write should be the following:

Account Deposit: 100 Dollars.

Collection Add: Transformation.

Oh, you may be very strange how you may have a Dollars message, in fact, this is nothing special. In order to legal this expression, there must be a definition of a DollarS method in the Integer class (100 will be treated as an Integer object). Although there is no such definition in the standard SmallTalk environment, we can add it yourself. SmallTalk's base class can be expanded by defining a new inheritance class.

Therefore, the completion of the expression is the end of the expression, but it can also be omitted after the last row expression (so you can also treat the journal as a separator between the expression). However, the semicolon is also legal, and he uses the syntax of the slot message syntax (a syntax to send multiple objects in an object). E.g:

| P |

P: = Client New.

p name: 'jack'.

P Age: 32.

p address: 'Earth'.

We should write as follows:

| P |

P: = Client New.

p

Name: 'jack';

Age: 32;

Address: 'Earth'.

Or simply write:

| P |

P: = Client New

Name: 'jack';

Age: 32;

Address: 'Earth'.

The format in the above example is not much related, as long as we can even write all the sentences into a line. A very critical semicolon indicates that the next message will continue to be sent to this acceptance object after the previous message is sent to an acceptance object and changes its status. (Instead of sending a return value object of the previous message, the return value of the upper message will be abandoned or ignored).

In the last example, the NEW is sent to the class to generate an instance (as a return value). Then "Name: 'Jack'" is sent to this instance. The first semicolon indicates that the result of "Name: 'Jack'" message will be ignored, immediately "Age: 32" is sent to the previous instance. The second semicolon indicates that the result of "Age: 32" is ignored, immediately "Address: 'Earth'" is sent to the previous instance. Finally, the results returned by "Address: 'Earth'" are saved to the variable P. Modifying a class method of the recipient attribute is usually returned to the recipient itself. Therefore, the variable P is finally bound to the newly generated class instance object that has been modified several times.

We can replace the above sections into "and also" will feel easy to understand this code. The syntax of the SMALLTalk is similar to the syntax of the same object to continuously send messages. The semicolon can also be used in sub-expression, such as "P: = (Client New Name: 'Jack'; Age: 32; Address: 'Earth')" - Note that the parentheses here. Get / set method uses the same name as the instance variable

Similar to Name, AGE, Address is all private variables in SmallTalk. But the class method can provide access to these variables. In C (Java is also similar), for example, we can define how the following access class instance variable:

Long getage () {return agn;}

Void setage (long newage) {agn = newage;

If you have a few types of classes, and you will use the coding agreement above, you will find that there is a big pile of getting your code in your code. Of course, if you happen to get rid of these duplicate prefix so that you will find that the C compiler will not be able to compile because he cannot distinguish between variables and functions from the name. But Java's compiler has no problem with the code below.

Long Age () {Return Age;}

Void Age (long newage) {agn = new;

Can you distinguish between variables AGE and message AGE? You should be available. When you use this message, you need to add parentheses, just like "age () or age (22)"; when you quote these variables, you don't need to use parentheses. The same class method can be written in smalltalk:

Age ^ agn

Newage agn: = newage

However, we usually make your code more readable by writing two lines:

AGE

^ agn

Age: Newage

Age: = newage

There is no parentheses in SmallTalk to help you distinguish between variables and messages, but it is not difficult to distinguish. If you are, you should be able to see which variables are variables below. What is methods:

AGE AGE AGE: AGE AGE AGE AGE

Ok, the answer is 3; the first AGE is inevitable is a variable, the fourth is also (there must be a sub-expression after each keyword; all expressions must begin with variables), and seventh (Behind a binary operator, it must also be a child expression). Use simpler typical examples should be more easily understood, as follows:

Name size // Name is inevitable a variable

Self name // name is inevitably a class method

Wide use of Collection

Serialized abstraction No need to create new Class

in conclusion

转载请注明原文地址:https://www.9cbs.com/read-93908.html

New Post(0)