User Tools

Site Tools


tutorial:class

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial:class [2019/01/28 09:41]
admin
tutorial:class [2019/01/29 08:24] (current)
admin
Line 21: Line 21:
 |ไม่ระบุ| หมายถึง public |  ​ |ไม่ระบุ| หมายถึง public |  ​
  
 +\\
 +----
 +=====การสร้าง Class=====
 +การสร้าง Class อย่างง่าย มีรูปแบบดังนี้
  
 +  Type
 +   ​TMyClass = class
 +     ​{Field,​ Method, Property...}  ​
 +   end;
 +
 +การสร้าง Class สืบทอดจาก Class อื่น มีรูปแบบดังนี้ ()
 +
 +  Type
 +   ​TMyClass = class(TObject)
 +     ​{Field,​ Method, Property...} ​   ​
 +   end;
 +
 +__**หมายเหตุ**__ - ในการสร้าง Class ทุกๆครั้งที่ไม่ได้ระบุว่าสืบทอดมาจาก Class ใด จะเป็นการสร้าง Class สืบทอดจาก TObject ซึ่งเป็น Class พื้นฐานของ FPC เสมอ \\ 
 +
 +ตัวอย่างการสร้าง Class แบบละเอียด
 +<sxh delphi;>
 +Type
 +  TMyClass = class(TObject)
 +   ​private
 +     ​FName:​string;​
 +     ​FValue:​integer; ​     ​
 +   ​protected
 +     ​function GetIsOK:​boolean;​
 +   ​public
 +     ​constructor create;
 +     ​destructor destroy; override;
 +     ​procedure DoSomeThing; ​        
 +   ​published
 +     ​property Name:string read FName write FName;
 +     ​property Value:​integer read FValue write FValue default 0; 
 +     ​property IsOK:​boolean read GetIsOK; ​
 +   end;
 +</​sxh>​
 +\\
 +
 +----
 +=====Property=====
 +คือ คุณสมบัติของ Class (หรือ Object) เช่น ความยาว ความกว้าง ตำแหน่ง เป็นต้น โครงสร้างของ property นั้นประกอบไปด้วย Getter (คำสั่งหลัง read) และ Setter (คำสั่งหลัง write)\\ \\
 +เพื่อเป็นการอธิบายให้เห็นภาพ จะขอยกตัวอย่าง
 +
 +  property ValueX:​integer read GetValueX write SetValueX;
 +
 +จากตัวอย่างดังกล่าว Getter คือ GetValueX ส่วน Setter คือ SetValueX \\
 +Getter จะถูกเรียกใช้งาน เมื่อมีการเรียกใช้ค่าของ Property ดังเช่น
 +
 +  writeln(ValueX);​
 +  ​
 +Setter จะถูกเรียกใช้งาน เมื่อมีการใส่ค่าให้ Property เช่น
 +
 +  ValueX:=12;
 +  ​
 +ทำไมต้องมี Getter Setter ให้ยุ่งยาก แทนที่จะใช้ var หรือ Field อย่างเดียว?​ ประโยชน์ของการใช้งาน Property มีดังนี้ครับ
 +  *ใช้คัดกรองตัวแปร บางครั้งเรารับค่าเพื่อการคำนวณในช่วง 0-100 เท่านั้น หากผู้ใช้ใส่ค่าเกินกว่าค่าดังกล่าวมา จะสามารถคัดกรองให้เป็นค่าที่อยู่ในช่วง 0-100 แทนได้
 +  *เพื่อให้ค่าของ Property ได้มีการ update ตลอดเวลา โดยเฉพาะหาก property ของเรา ขึ้นอยู่กับค่าอื่น เมื่อใดที่มีการเปลี่ยนค่า ก็จะมีการ update ค่า property เราด้วยเสมอ
 +
 +
 +รูปแบบการเขียน Property มีหลักๆดังนี้
 +  *Property ที่มี Getter, Setter เป็น Field
 +
 +  property Value:​integer read FValue write FValue;
 +
 +  *property ที่มี Getter, Setter เป็น Procedure/​Function ซึ่งโดยปกติ Getter จะเป็น Function ส่วน Setter จะเป็น Procedure
 +
 +  property ValueX:​integer read GetValueX write SetValueX;
 +  function GetValueX:​integer;​
 +  procedure SetValueX(Value:​integer);​
 +
 +  *Property ที่แสดงค่าได้อย่างเดียว (ReadOnly)
 +
 +  property IsOK:​boolean read GetIsOK;
tutorial/class.1548643268.txt.gz · Last modified: 2019/01/28 09:41 by admin