This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
tutorial:genericandtemplate [2018/12/23 12:21] admin |
tutorial:genericandtemplate [2018/12/23 23:16] (current) admin [2. Generic Class] |
||
|---|---|---|---|
| Line 25: | Line 25: | ||
| </sxh> | | </sxh> | | ||
| - | การเรียกใช้ ทำได้ดังนี้ | + | การเรียกใช้ สมตติว่าเราจะนำมาใช้กับ integer จะเรียกใช้ได้ดังนี้ |
| - | generalized | + | specialize FunctionName<integer>(x): |
| Line 36: | Line 36: | ||
| result:=x+y; | result:=x+y; | ||
| end; | end; | ||
| + | |||
| + | var x,y:double; | ||
| + | |||
| + | Begin | ||
| + | x:=12.78; | ||
| + | y:=9.86; | ||
| + | writeln('sum(x,y) = ',specialize sum<double>(x,y)); | ||
| + | |||
| + | End. | ||
| </sxh> | </sxh> | ||
| </hidden> | </hidden> | ||
| Line 41: | Line 50: | ||
| \\ | \\ | ||
| + | **__หมายเหตุ__** - Generic Function ปัจจุบันสามารถใช้ได้เฉพาะ Lazarus เวอร์ชั่น Trunk (สำหรับ Developer) เท่านั้น ยังไม่สามารถใช้ได้กับเวอร์ชั่นทั่วไป | ||
| =====2. Generic Class===== | =====2. Generic Class===== | ||
| - | คือการสร้าง Class ที่ใช้กับ DataType ใดๆก็ได้ ไม่เฉพาะเจาะจง | + | คือการสร้าง Class ที่ใช้กับ DataType ใดๆก็ได้ ไม่เฉพาะเจาะจง คล้ายกับ Generic Procedure/Function แค่เปลี่ยนเป็น Class เท่านั้น อย่างไรก็ตาม สำหรับ Generic Class นั้น ก่อนการนำไปใช้ จะต้องมีการสร้าง Class เป้าหมาย ขึ้นมาก่อนเสมอ รูปแบบของการประกาศและการสร้าง Class สรุปได้ดังนี้ |
| + | |||
| + | ^ Topic ^ Format ^ | ||
| + | |การประกาศ Generic Class|<sxh delphi;> | ||
| + | type | ||
| + | generic TList<T> = class | ||
| + | Items: array of T; | ||
| + | procedure Add(Value: T); | ||
| + | end; | ||
| + | </sxh> | | ||
| + | |การสร้าง Class จาก Generic Class|<sxh delphi;> | ||
| + | Type | ||
| + | TIntegerList = specialize TList<Integer>; | ||
| + | TPointerList = specialize TList<Pointer>; | ||
| + | TStringList = specialize TList<string>; | ||
| + | </sxh> | | ||