This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
tutorial:record [2019/01/20 08:23] admin |
tutorial:record [2019/01/28 10:13] admin |
||
---|---|---|---|
Line 55: | Line 55: | ||
ปกติแล้ว Record จะมีแค่ Field Variable อยู่ภายใน แต่ยังมี Record อีกประเภทหนึ่งที่มี Method และ Property ได้ เรียกว่า Advanced Record \\ | ปกติแล้ว Record จะมีแค่ Field Variable อยู่ภายใน แต่ยังมี Record อีกประเภทหนึ่งที่มี Method และ Property ได้ เรียกว่า Advanced Record \\ | ||
- | Advanced Record ถูกสร้างขึ้นเพื่อให้มีความสามารถทางด้าน Object Oriented Programming เช่นเดียวกับ Object จะเรียกได้ว่า Advanced Record กับ Object นั้นเหมือนกันแทบทั้งหมดก็ว่าได้ \\ | + | Advanced Record ถูกสร้างขึ้นเพื่อให้มีความสามารถทางด้าน Object Oriented Programming \\ |
การประกาศ Advanced Record จำเป็นต้องมี Compiler Directive ต่อไปนี้ | การประกาศ Advanced Record จำเป็นต้องมี Compiler Directive ต่อไปนี้ | ||
Line 61: | Line 61: | ||
{$modeSwitch advancedRecords} | {$modeSwitch advancedRecords} | ||
| | ||
- | จากนั้น เราก็สามารถประกาศ Record ให้มีหน้าตาคล้ายกับ Object ได้แบบนี้ | + | จากนั้น เราก็สามารถประกาศ Procedure/Function บน Record ได้ดังนี้ |
Type | Type | ||
Line 136: | Line 136: | ||
---- | ---- | ||
=====Object===== | =====Object===== | ||
- | ถูกสร้างขึ้นมาตั้งแต่สมัยที่ยังใช้ Turbo Pascal โดยออกแบบให้เป็นไปตามหลักการ Object Oriented Programming (OOP) ลักษณะการใช้งานโดยทั่วไปจะคล้ายกับ Advanced Record \\ \\ | + | ถูกสร้างขึ้นมาตั้งแต่สมัยที่ยังใช้ Turbo Pascal โดยออกแบบให้เป็นไปตามหลักการ Object Oriented Programming (OOP) ลักษณะการใช้งานโดยทั่วไปจะเหมือนกันกับ Advanced Record \\ \\ |
สาเหตุที่มีให้ใช้ทั้ง Object และ Advanced Record นั้น เพราะว่าภายหลังจากที่ Delphi เปิดตัว ได้มีการนำ Class มาใช้แทน Object และผลักดันให้ เรียก Object ว่าเป็น Advanced Record แทน (อันนี้ผมเข้าใจว่า เพราะคำว่าว่า Object ในภาษาทั่วไป คือ instance ซึ่งแปลว่าผลิตภัณท์ที่เกิดจากต้นแบบ (Class) ซึ่งอาจสร้างความสับสนให้ผู้ใช้ได้) ทีนี้ทางฝั่งของ FPC ซึ่งมีการออกแบบให้โค๊ดทั้งหลายคล้ายคลึงกับทั้งทาง Turbo Pascal และ Delphi อยู่แล้วนั้น ได้ตัดสินใจเก็บ Object ไว้ เพื่อให้ยัง Compatible กับ Turbo Pascal อยู่นั่นเอง แต่ถึงอย่างไรทั้งสอง ก็ถูกใช้งานเหมือนกัน | สาเหตุที่มีให้ใช้ทั้ง Object และ Advanced Record นั้น เพราะว่าภายหลังจากที่ Delphi เปิดตัว ได้มีการนำ Class มาใช้แทน Object และผลักดันให้ เรียก Object ว่าเป็น Advanced Record แทน (อันนี้ผมเข้าใจว่า เพราะคำว่าว่า Object ในภาษาทั่วไป คือ instance ซึ่งแปลว่าผลิตภัณท์ที่เกิดจากต้นแบบ (Class) ซึ่งอาจสร้างความสับสนให้ผู้ใช้ได้) ทีนี้ทางฝั่งของ FPC ซึ่งมีการออกแบบให้โค๊ดทั้งหลายคล้ายคลึงกับทั้งทาง Turbo Pascal และ Delphi อยู่แล้วนั้น ได้ตัดสินใจเก็บ Object ไว้ เพื่อให้ยัง Compatible กับ Turbo Pascal อยู่นั่นเอง แต่ถึงอย่างไรทั้งสอง ก็ถูกใช้งานเหมือนกัน | ||
Line 169: | Line 169: | ||
\\ | \\ | ||
จากตัวอย่างโค๊ดข้างบน จะอธิบายส่วนประกอบต่างๆทีละส่วนดังนี้ | จากตัวอย่างโค๊ดข้างบน จะอธิบายส่วนประกอบต่างๆทีละส่วนดังนี้ | ||
- | ====Access Specifiers==== | + | ====Access Modifier==== |
type | type | ||
Line 224: | Line 224: | ||
property IsOK:boolean read GetIsOK; | property IsOK:boolean read GetIsOK; | ||
- | \\ | + | |
<hidden Example-3: Object without Properties> | <hidden Example-3: Object without Properties> | ||
Line 284: | Line 284: | ||
</sxh> | </sxh> | ||
</hidden> | </hidden> | ||
- | \\ | + | |
- | <hidden Example-4: Object with Properties> | + | <hidden Example-4: Object Properties> |
**ตัวอย่าง** การสร้าง Object ทรงกระบอก Cylinder เพื่อรับค่า รัศมีวงกลม (Radius) กับ ความสูง (Height) เข้ามาเป็น Input \\ จากนั้น | **ตัวอย่าง** การสร้าง Object ทรงกระบอก Cylinder เพื่อรับค่า รัศมีวงกลม (Radius) กับ ความสูง (Height) เข้ามาเป็น Input \\ จากนั้น | ||
จึงนำมาคำนวณพื้นที่ฐาน (Area) และปริมาตร (Volume) \\ \\ | จึงนำมาคำนวณพื้นที่ฐาน (Area) และปริมาตร (Volume) \\ \\ | ||
Line 292: | Line 292: | ||
<sxh delphi;highlight: []> | <sxh delphi;highlight: []> | ||
- | program Cylinder_Without_Properties; | + | program Cylinder_With_Properties; |
type | type | ||
Line 299: | Line 299: | ||
TMyCylinder = Object | TMyCylinder = Object | ||
+ | private | ||
+ | function GetRadius: real; | ||
+ | function GetVolume: real; | ||
+ | public | ||
Radius:real; //Input | Radius:real; //Input | ||
Height:real; //Input | Height:real; //Input | ||
- | Area:real; //Output | + | property Area:real read GetRadius; //Output |
- | Volume:real; //Output | + | property Volume:real read GetVolume; //Output |
- | procedure CalculateArea; | + | |
- | procedure CalculateVolume; | + | |
procedure ShowData; | procedure ShowData; | ||
end; | end; | ||
Line 310: | Line 312: | ||
{ TMyCylinder } | { TMyCylinder } | ||
- | procedure TMyCylinder.CalculateArea; | + | function TMyCylinder.GetRadius: real; |
begin | begin | ||
- | Area:=system.pi*(Radius*Radius); | + | result:=system.pi*(Radius*Radius); |
end; | end; | ||
- | procedure TMyCylinder.CalculateVolume; | + | function TMyCylinder.GetVolume: real; |
begin | begin | ||
- | Volume:=Area*Height; | + | result:=Area*Height; |
end; | end; | ||
Line 332: | Line 334: | ||
begin | begin | ||
Cylinder.Radius:=2; //Set Radius = 2 | Cylinder.Radius:=2; //Set Radius = 2 | ||
- | Cylinder.Height:=10; //Set Radius = 2 | + | Cylinder.Height:=10; //Set Height = 10 |
- | Cylinder.CalculateArea; //Calculate Area of Cylinder | + | |
- | Cylinder.CalculateVolume; //Calculate Volume of Cylinder | + | |
Cylinder.ShowData; //Show the results | Cylinder.ShowData; //Show the results | ||
Cylinder.Radius:=10; //Change Radius to 10 | Cylinder.Radius:=10; //Change Radius to 10 | ||
- | Cylinder.ShowData; //Show the result without calling CalculateArea | + | Cylinder.ShowData; //Show the results |
+ | |||
+ | Cylinder.Radius:=5; //Change Radius to 5 | ||
+ | Cylinder.Height:=2; //Change Height = 2 | ||
+ | Cylinder.ShowData; //Show the results | ||
readln; | readln; | ||
- | end. | + | end. |
</sxh> | </sxh> | ||
+ | Compiled Results: | ||
</hidden> | </hidden> | ||
\\ | \\ |