User Tools

Site Tools


tutorial:record

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
Next revision Both sides next revision
tutorial:record [2019/01/20 08:02]
admin
tutorial:record [2019/01/28 09:19]
admin
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>​
 **ตัวอย่าง** การสร้าง Object ทรงกระบอก Cylinder เพื่อรับค่า รัศมีวงกลม (Radius) กับ ความสูง (Height) เข้ามาเป็น Input \\ จากนั้น **ตัวอย่าง** การสร้าง Object ทรงกระบอก Cylinder เพื่อรับค่า รัศมีวงกลม (Radius) กับ ความสูง (Height) เข้ามาเป็น Input \\ จากนั้น
 จึงนำมาคำนวณพื้นที่ฐาน (Area) และปริมาตร (Volume) \\ \\ จึงนำมาคำนวณพื้นที่ฐาน (Area) และปริมาตร (Volume) \\ \\
-__ข้อสังเกต__:​ ตัวอย่างนี้ แสดงให้เห็นปัญหาของการไม่ใช้ Property จะสังเกตว่า ตอนแรกนั้นเราใส่ค่า Radius = 2 และ Height = 10 แล้วสั่งคำนวณค่าจาก CalculateArea,​ CalculateVolume จะได้ค่า Area, Volume มาตามปกติ \\ \\ แต่หลังจากนั้น เมื่อเปลี่ยนค่า Radius เป็น 10 แล้วลองไม่เรียก CalculateArea,​ CalculateVolume เมื่อเรียกดูค่า Area, Volume จะเห็นว่ายังเป็นค่าเดิม เพราะไม่ได้มีการ Update ค่า Area, Volume เหมือนตอนแรก\\ \\ ปัญหานี้เกิขึน เพระตามปกติเมื่อมีการเปลี่ยนค่า Radius หรือ Height จะต้องมีการเรียก CalculateArea,​ CalculateVolume ทุกครั้ง จึงจะได้ค่า Area, Volume ที่ถูกต้อง ​+__ข้อสังเกต__:​ ตัวอย่างนี้ แสดงให้เห็นปัญหาของการไม่ใช้ Property จะสังเกตว่า ตอนแรกนั้นเราใส่ค่า Radius = 2 และ Height = 10 แล้วสั่งคำนวณค่าจาก CalculateArea,​ CalculateVolume จะได้ค่า Area, Volume มาตามปกติ \\ \\ แต่หลังจากนั้น เมื่อเปลี่ยนค่า Radius เป็น 10 แล้วลองไม่เรียก CalculateArea,​ CalculateVolume เมื่อเรียกดูค่า Area, Volume จะเห็นว่ายังเป็นค่าเดิม เพราะไม่ได้มีการ Update ค่า Area, Volume เหมือนตอนแรก\\ \\ สรุด้ว่า เมื่อมีการเปลี่ยนค่า Radius หรือ Height จะต้องมีการเรียก CalculateArea,​ CalculateVolume ทุกครั้ง จึงจะได้ค่า Area, Volume ที่ถูกต้อง ​ลองจินตนาการถึงการเขียนโปรแกรมจริงๆว่าหากเรามี Input กับ Output ที่มากขึ้นในระดับ 10-20 ตัว ซึ่งแต่ละตัวมีการคำนวณค่าที่ซับซ้อน ที่ผ่าน Procedure/​Function หลายตัว คงเป็นการลำบากที่จะต้องมาเรียก Procedure/​Function เหล่านั้น ทุกครั้งที่มีการ update ค่า Input นี่จึงเป็นเหตุผลหนึ่ง ที่เราควรใช้งาน Property แทนที่ตัวแปร ​ Input/​Output พวกนี้ สำหรับการเปลี่ยนมาใช้ Property ให้ดูได้จาก Example-4
    
-<sxh delphi;​highlight:​ [4,13,30]>+<sxh delphi;​highlight:​ []>
 program Cylinder_Without_Properties;​ program Cylinder_Without_Properties;​
  
Line 284: Line 284:
 </​sxh> ​ </​sxh> ​
 </​hidden>​ </​hidden>​
-\\   +   
 +<hidden Example-4: Object Properties>​ 
 +**ตัวอย่าง** การสร้าง Object ทรงกระบอก Cylinder เพื่อรับค่า รัศมีวงกลม (Radius) กับ ความสูง (Height) เข้ามาเป็น Input \\ จากนั้น 
 +จึงนำมาคำนวณพื้นที่ฐาน (Area) และปริมาตร (Volume) \\ \\ 
 +__ข้อสังเกต__:​ ตัวอย่างนี้ ใช้ Property แทนตัวแปร Output และเปลี่ยน CalculateArea,​ CalculateVolume ไปเป็น Getter แทน \\ \\ 
 +จะสังเกตว่า เมื่อเราเปลี่ยนค่า Radius หรือ Height ค่าของ Area, Volume ก็จะถูก Update ตามเสมอ เพราะอย่างเช่นที่เคยอธิบายข้างบนไว้แล้วว่า Getter จะถูกเรียกทุกครั้ง ที่ Property ตัวนั้นถูกเรียกให้แสดงค่า ซึ่งในกรณีนี้ ถูกเรียกให้แสดงค่าใน ShowData นั่นเอง 
 +  
 +<sxh delphi;​highlight:​ []> 
 +program Cylinder_With_Properties;​ 
 + 
 +type 
 + 
 +  { TMyCylinder } 
 + 
 +  TMyCylinder = Object 
 +  private 
 +    function GetRadius: real; 
 +    function GetVolume: real; 
 +  public 
 +    Radius:​real; ​  ​//Input 
 +    Height:​real; ​  //​Input 
 +    property Area:real read GetRadius; ​   //Output 
 +    property Volume:real read GetVolume; ​ //Output 
 +    procedure ShowData; 
 +  end; 
 + 
 +{ TMyCylinder } 
 + 
 +function TMyCylinder.GetRadius:​ real; 
 +begin 
 +  result:​=system.pi*(Radius*Radius);​ 
 +end; 
 + 
 +function TMyCylinder.GetVolume:​ real; 
 +begin 
 +  result:​=Area*Height;​ 
 +end; 
 + 
 +procedure TMyCylinder.ShowData;​ 
 +begin 
 +  writeln('​Radius = ',​Radius);​ 
 +  writeln('​Area = ',​Area);​ 
 +  writeln('​Volume = ',​Volume);​ 
 +end; 
 + 
 +var 
 +  Cylinder:​TMyCylinder;​ 
 + 
 +begin 
 +  Cylinder.Radius:​=2;​ //Set Radius = 2 
 +  Cylinder.Height:​=10;​ //Set Height = 10 
 +  Cylinder.ShowData; ​ //Show the results 
 + 
 +  Cylinder.Radius:​=10;​ //Change Radius to 10 
 +  Cylinder.ShowData; ​ //Show the results 
 + 
 +  Cylinder.Radius:​=5;​ //Change Radius to 5 
 +  Cylinder.Height:​=2;​ //Change Height = 2 
 +  Cylinder.ShowData; ​ //Show the results 
 + 
 +  readln; 
 +end.   
 +</​sxh>​  
 +Compiled Results: 
 +</​hidden>​ 
 +\\ 
  
  
  
tutorial/record.txt · Last modified: 2019/01/28 10:14 by admin