Basic OOP Concepts and Terminology
So what are the basic concepts of Smalltalk? They are: Objects, Fields, OOPs, Classes, Methods, Messages, Inheritance, Receiver, Dynamic Binding. We will define each of these in turn, although the reader may already be familiar with them.
The Smalltalk "object" is very similar to the Java or C++ object. In particular, each object is stored in memory and has several fields, which can point to other objects or contain primitive data values. Each object is an instance of a class.
An object is like a C "struct" or a Pascal record. The only difference between a record and an object is that the object contains a single special field that identifies which class the object belongs to. In C++, Java, and Smalltalk, there is a single hidden field in every object. In Smalltalk, this field is called the "class pointer." In other languages, this hidden field is called the "V-table pointer" or "dispatch table pointer".
In C++ we talk about "pointers" to objects while in Java we talk about "references" to objects. Both concepts are more or less the same and references are implemented by straight pointers, which are just memory addresses. In Smalltalk, references are sometimes called "object-oriented pointers" or "OOPs". In Squeak, an OOP is implemented with a 32-bit pointer. So a Smalltalk object points to other objects, but we say the object "contains OOPs to other objects".
Each object belongs to exactly one class. We say that each object is an "instance" of its class. Classes are related in a tree-shaped hierarchy, so each class has exactly one superclass, except the class at the root of the tree. So Smalltalk has single-inheritance. Smalltalk uses the terms "subclass", "superclass", and "inherits from". C++ uses the term "derived class", but we prefer to say "subclass" instead.
When the programmer defines a new class, he or she is (in some sense) describing all the instances of that class that may later be created. Two things must be described: (1) how the objects will be represented, and (2) how the objects will behave. This is true in Smalltalk, C++, and Java.
To describe how the objects are represented, the programmer must list all the fields in the object, and give each field a name. To describe how the object will behave, the programmer will give a series of "methods".
A method is like a function, except that it is applied to a specific object. We also say that the method is "invoked" on an object or "sent to" an object. The object in question is called the "receiver." Every method is invoked on a receiving object. In C++ and Java, the receiver is called the "this object", but Smalltalk does not use this this terminology. The "this" terminology makes for awkward wordings.
Methods in Smalltalk are similar to methods in Java and C++, where they are called "member functions". Methods may take arguments and may return a result. The method body is a sequence of executable statements. Methods are invoked from expressions, just as in other languages.
There is an important distinction between "methods" and "messages". A method is a body of code, while a message is something that is sent. A method is similar to a function; in this analogy, sending a message is similar to calling a function. An expression which invokes a method is called a "message sending expression."
Smalltalk terminology makes a clear distinction between "message" and "method", but Java and C++ terminology sometimes confuses these concepts. A message-sending expression will send a message to the object. How the object responds to the message depends on the class of the object. Objects of different classes will respond to the same message differently, since they will invoke different methods.
When a message is sent to an object, a method will be selected and executed. Since we cannot know, in general, the class of the object until run-time, the method cannot be selected until the message is actually sent. This is called "dynamic binding", and Java, C++, and Smalltalk all have it. With straight functions, the compiler can look at a "call" statement and figure out at compile-time (i.e., "statically") which body of code to branch to. C++ (which always prefers efficiency over clarity) encourages static binding and refers to dynamically bound methods as "virtual" methods, and refers to the virtual table.
In the Smalltalk programming model, all binding is dynamic. However, the compiler and virtual machine will often bind methods statically for greater execution efficiency when it is can be done safely and without changing the program's behavior.
So far, we have discussed only the similarities between Smalltalk and other languages. As you can see, the fundamental concepts of OOP are all present in Smalltalk in very straightforward ways. The Smalltalk terminology is a little different, but often seems more direct. The terms: "Object, Field, Class, Method, Message, Inheritance, Receiver, and Dynamic Binding" all have their usual meanings.
We now turn to areas where there are differences between Smalltalk and other languages. We begin by talking about what objects look like.
Basic OOP Concepts and Terminology
So what are the basic concepts of Smalltalk? They are: Objects, Fields, OOPs, Classes, Methods, Messages, Inheritance, Receiver, Dynamic Binding. We will define each of these in turn, although the reader may already be familiar with them.
The Smalltalk "object" is very similar to the Java or C++ object. In particular, each object is stored in memory and has several fields, which can point to other objects or contain primitive data values. Each object is an instance of a class.
An object is like a C "struct" or a Pascal record. The only difference between a record and an object is that the object contains a single special field that identifies which class the object belongs to. In C++, Java, and Smalltalk, there is a single hidden field in every object. In Smalltalk, this field is called the "class pointer." In other languages, this hidden field is called the "V-table pointer" or "dispatch table pointer".
In C++ we talk about "pointers" to objects while in Java we talk about "references" to objects. Both concepts are more or less the same and references are implemented by straight pointers, which are just memory addresses. In Smalltalk, references are sometimes called "object-oriented pointers" or "OOPs". In Squeak, an OOP is implemented with a 32-bit pointer. So a Smalltalk object points to other objects, but we say the object "contains OOPs to other objects".
Each object belongs to exactly one class. We say that each object is an "instance" of its class. Classes are related in a tree-shaped hierarchy, so each class has exactly one superclass, except the class at the root of the tree. So Smalltalk has single-inheritance. Smalltalk uses the terms "subclass", "superclass", and "inherits from". C++ uses the term "derived class", but we prefer to say "subclass" instead.
When the programmer defines a new class, he or she is (in some sense) describing all the instances of that class that may later be created. Two things must be described: (1) how the objects will be represented, and (2) how the objects will behave. This is true in Smalltalk, C++, and Java.
To describe how the objects are represented, the programmer must list all the fields in the object, and give each field a name. To describe how the object will behave, the programmer will give a series of "methods".
A method is like a function, except that it is applied to a specific object. We also say that the method is "invoked" on an object or "sent to" an object. The object in question is called the "receiver." Every method is invoked on a receiving object. In C++ and Java, the receiver is called the "this object", but Smalltalk does not use this this terminology. The "this" terminology makes for awkward wordings.
Methods in Smalltalk are similar to methods in Java and C++, where they are called "member functions". Methods may take arguments and may return a result. The method body is a sequence of executable statements. Methods are invoked from expressions, just as in other languages.
There is an important distinction between "methods" and "messages". A method is a body of code, while a message is something that is sent. A method is similar to a function; in this analogy, sending a message is similar to calling a function. An expression which invokes a method is called a "message sending expression."
Smalltalk terminology makes a clear distinction between "message" and "method", but Java and C++ terminology sometimes confuses these concepts. A message-sending expression will send a message to the object. How the object responds to the message depends on the class of the object. Objects of different classes will respond to the same message differently, since they will invoke different methods.
When a message is sent to an object, a method will be selected and executed. Since we cannot know, in general, the class of the object until run-time, the method cannot be selected until the message is actually sent. This is called "dynamic binding", and Java, C++, and Smalltalk all have it. With straight functions, the compiler can look at a "call" statement and figure out at compile-time (i.e., "statically") which body of code to branch to. C++ (which always prefers efficiency over clarity) encourages static binding and refers to dynamically bound methods as "virtual" methods, and refers to the virtual table.
In the Smalltalk programming model, all binding is dynamic. However, the compiler and virtual machine will often bind methods statically for greater execution efficiency when it is can be done safely and without changing the program's behavior.
So far, we have discussed only the similarities between Smalltalk and other languages. As you can see, the fundamental concepts of OOP are all present in Smalltalk in very straightforward ways. The Smalltalk terminology is a little different, but often seems more direct. The terms: "Object, Field, Class, Method, Message, Inheritance, Receiver, and Dynamic Binding" all have their usual meanings.
We now turn to areas where there are differences between Smalltalk and other languages. We begin by talking about what objects look like.
การแปล กรุณารอสักครู่..

พื้นฐานแนวคิด OOP ศัพท์
ดังนั้นสิ่งที่เป็นแนวคิดพื้นฐานของ ธ ? พวกเขาเป็นวัตถุเขต , อุ๊ , เรียน , วิธี , ข้อความ , มรดก , รับ , แบบไดนามิกที่มีผลผูกพัน เราจะอธิบายแต่ละเหล่านี้ในการเปิด แต่ผู้อ่านอาจจะคุ้นเคยกับพวกเขา
Smalltalk " วัตถุ " จะคล้ายกับ Java หรือ C วัตถุ โดยเฉพาะอย่างยิ่งแต่ละวัตถุจะถูกเก็บไว้ในหน่วยความจำ และมีหลายสาขา ซึ่งสามารถชี้ไปยังวัตถุอื่น หรือมีข้อมูลแบบดั้งเดิมค่า แต่ละวัตถุที่เป็นตัวอย่างของชั้น
วัตถุเช่น C " ซอฟต์แวร์ " หรือเครื่องบันทึก ข้อแตกต่างระหว่างระเบียนและวัตถุนั้นเป็นวัตถุที่มีซิงเกิ้ลพิเศษเขตที่ระบุระดับที่วัตถุอยู่ ใน C , Java , Smalltalk และ ,มีเพียงเขตข้อมูลที่ซ่อนทุกวัตถุ ในภาษาฟิลิสไตน์ ฟิลด์นี้จะเรียกว่า " คลาสชี้ " ในภาษาอื่น ๆ เขตข้อมูลที่ซ่อนอยู่ นี้เรียกว่า " ตัวชี้ " หรือ " v-table โต๊ะส่งชี้ "
ใน C ที่เราพูดคุยเกี่ยวกับ " ชี้ " วัตถุในขณะที่ใน Java เราพูดคุยเกี่ยวกับ " อ้างอิง " วัตถุ ทั้งสองแนวคิดจะมากกว่าหรือน้อยกว่าอ้างอิงเดียวกัน และจะดำเนินการโดยตัวชี้ตรงซึ่งเป็นหน่วยความจำที่อยู่ ใน Smalltalk อ้างอิง บางครั้งเรียกว่า " การชี้ " หรือ " อุ๊บ " ในสารภาพเป็น OOP ใช้กับตัว 32 บิต และ 64 บิต ดังนั้นวัตถุ Smalltalk จุดวัตถุอื่น ๆ แต่เราว่าวัตถุ " มี อุ้ยกับวัตถุอื่น”
แต่ละวัตถุเป็นของอีกหนึ่งชั้น เราบอกว่า แต่ละวัตถุเป็น " ตัวอย่าง " ของคลาสนี้ชั้นเรียนที่เกี่ยวข้องในต้นไม้ที่มีรูปทรงลำดับชั้น ดังนั้น แต่ละชั้นมีอีกหนึ่งซูเปอร์คลาส ยกเว้นห้องที่รากของต้นไม้ งั้นเดี่ยว Smalltalk ได้มรดก Smalltalk ใช้เงื่อนไข " subclass " , " ซูเปอร์คลาส " และ " สืบทอดจาก " C ใช้คำว่า " โคม่า " แต่เราชอบพูดว่า " subclass " แทน
เมื่อโปรแกรมเมอร์กำหนดชั้นเรียนใหม่เขาหรือเธอ ( บ้าง ) ที่อธิบายทุกอินสแตนซ์ของคลาสที่ภายหลังอาจถูกสร้างขึ้น สองสิ่งที่ต้องอธิบาย : ( 1 ) วิธีวัตถุจะแสดง และ ( 2 ) วิธีการที่วัตถุจะประพฤติ นี้เป็นจริงใน Smalltalk , C และ Java
อธิบายว่าวัตถุจะแสดง โปรแกรมเมอร์ต้องรายการเขตข้อมูลทั้งหมดในวัตถุ และให้แต่ละเขตข้อมูลชื่ออธิบายวิธีวัตถุจะทำตัวดี โปรแกรมเมอร์จะให้ชุดของ " วิธีการ "
วิธีการก็เหมือนการทำงาน ยกเว้นว่ามันจะใช้กับวัตถุที่เฉพาะเจาะจง เราก็บอกว่า เป็นวิธีที่ " เรียก " วัตถุ " หรือ " ส่ง " วัตถุ วัตถุในคำถาม เรียกว่า " ผู้รับ " ทุกวิธีจะเรียกการรับวัตถุ ใน C และ Java ,ตัวรับสัญญาณจะเรียกว่า " วัตถุ " นี้ แต่ ธไม่ได้ใช้นี้ศัพท์ " นี้ " ศัพท์ที่เกี่ยวข้องทำให้กระอักกระอ่วน
ใช้ Smalltalk จะคล้ายกับวิธีการในชวาและที่พวกเขาจะเรียกว่า " ฟังก์ชัน " สมาชิก วิธีการอาจใช้อาร์กิวเมนต์และอาจส่งคืนผลลัพธ์ ร่างกายเป็นวิธีการลำดับของงบปฏิบัติการ วิธีเรียกจากนิพจน์เช่นเดียวกับภาษาอื่น ๆ
มีความแตกต่างที่สำคัญระหว่าง " วิธีการ " และ " ข้อความ " วิธีการคือร่างกายของรหัสในขณะที่ข้อความบางอย่างที่ถูกส่งมา วิธีการจะคล้ายกับฟังก์ชัน ในการเปรียบเทียบนี้ ส่งข้อความคล้ายกับการเรียกฟังก์ชัน การแสดงออกซึ่งจะเรียกวิธีการนี้เรียกว่า " การส่งข้อความการแสดงออก . "
Smalltalk ศัพท์ที่ทำให้แตกต่างที่ชัดเจนระหว่าง " ข้อความ " และ " วิธีการ " แต่ชวาและคำศัพท์บางครั้งสับสนแนวคิดเหล่านี้ ข้อความส่งสีหน้าจะส่งข้อความไปยังวัตถุ วิธีการวัตถุที่ตอบสนองต่อข้อความที่ขึ้นอยู่กับระดับของวัตถุ วัตถุของคลาสที่แตกต่างกันจะตอบสนองต่อข้อความเดียวกันที่แตกต่าง , เนื่องจากพวกเขาจะใช้วิธีการที่แตกต่างกัน
เมื่อข้อความถูกส่งไปยังวัตถุวิธีการจะถูกเลือกและดำเนินการ เพราะเราไม่สามารถรู้ได้ โดยทั่วไป คลาสของวัตถุจนครั้ง วิธีที่ไม่สามารถเลือกได้จนกว่าข้อความจะส่งจริง . นี้เรียกว่าแบบไดนามิก " ผูกพัน " และ Java , C , และ Smalltalk ทั้งหมดมี ด้วยฟังก์ชันตรง คอมไพเลอร์สามารถดูที่ " เรียก " งบและคิดออกที่รวบรวมเวลา ( เช่นE . " ผล " ) ซึ่งร่างกายของรหัสสาขา . C ( ซึ่งมักจะชอบประสิทธิภาพมากกว่าความคมชัด ) สนับสนุนให้คงที่ และหมายถึง " วิธีการแบบไดนามิกวิธีการผูกเป็นเสมือน " และอ้างถึงตารางเสมือน
ใน Smalltalk การเขียนโปรแกรมแบบทั้งหมดผูกเป็นแบบไดนามิก อย่างไรก็ตามคอมไพเลอร์และเครื่องเสมือนมักจะผูกวิธีประหารด้วยประสิทธิภาพมากขึ้นเมื่อมันสามารถทำได้อย่างปลอดภัยและไม่มีการเปลี่ยนแปลงพฤติกรรมของโปรแกรม
ดังนั้นไกล เราได้กล่าวถึงเพียงความคล้ายคลึงกันระหว่าง Smalltalk และภาษาอื่น ๆ ที่คุณสามารถดู , พื้นฐานแนวคิดของ OOP ทั้งหมดที่มีอยู่ใน Smalltalk ในตรงไปตรงมามากวิธีวัน Smalltalk ศัพท์เป็นน้อยแตกต่างกัน แต่มักจะดูเหมือนว่าโดยตรงมากขึ้น เงื่อนไข : " วัตถุ , เขต , ชั้น , วิธี , ข้อความ , มรดก , รับ , และแบบไดนามิกผูกพัน " ทั้งหมดมีความหมายที่ปกติของพวกเขา
ตอนนี้เราเปิดพื้นที่ซึ่งมีความแตกต่างระหว่าง Smalltalk และภาษาอื่น ๆ เราเริ่มต้นโดยพูดถึงสิ่งที่วัตถุมีลักษณะเหมือน
การแปล กรุณารอสักครู่..
