OOP ใน Flash cs 5.5 ตอนที่ 2

จริง ๆ แล้วไม่ได้นับเป็น (เนื้อหา) ตอนที่ 2 ที่ต่อจากตอนที่แล้ว แต่เป็นตอบคำถามของอาจารย์ท่านหนึ่งตอนที่ 2 ครับ

มีอาจารย์ท่านหนึ่งถามผมตามนี้

"อยากให้อาจารย์ได้ให้ความรู้เกี่ยวกับ
1. ให้คำอธิบาย static methods พร้อมยกตัวอย่างการเขียน code การเข้าถึง การเรียกใช้งาน ฟังก์ชัน หรือ ตัวแปร ด้วยครับ
2. ให้คำอธิบาย instance methods พร้อมยกตัวอย่างการเขียน code การเข้าถึง การเรียกใช้งาน ฟังก์ชัน หรือ ตัวแปร ด้วยครับ
3. ให้คำอธิบาย Bound methods พร้อมยกตัวอย่างการเขียน code การเข้าถึง การเรียกใช้งาน ฟังก์ชัน หรือ ตัวแปร ด้วยครับ
4. ให้คำอธิบาย constructor methods พร้อมยกตัวอย่างการเขียน code การเข้าถึง การเรียกใช้งาน ฟังก์ชัน หรือ ตัวแปร ด้วยครับ

ขอขอบพระคุณมากครับ"

ขอตอบข้อ 3 ก่อนครับ

เราสามารถใช้ตัว method เองกำหนดค่าให้กับตัวแปรอื่น ๆ ได้ตรับ

เปรียบเสมิอนว่าตัวมันมีค่าอยู่ (จริง ๆ แล้วก็คือค่า อ้างอิงหรือ reference

หรือในภาษา C ก็คือ pointer to function) การใช้งานไม่ได้เหมือนกับการส่งค่ากลับมา

ของ method เวลาใส่ชื่อ function จะไม่ได้ใส่ วงเล็บเข้าไปด้วย ()

และเราสามารถเอาตวแปรนี้ไปเรียกใช้เหมือนกับ function ด้วยครับ

ลองดูจากตัวอย่างนะครับ

/******************/

package MyPackage
{
import flash.display.MovieClip;
public class MyClass1 extends MovieClip
{
private var obj;
public function MyClass1()
{
trace("This is MyClass1.");
obj = new MyClass2 ;
var MyBound = obj.MyMethod;
MyBound(10);
}
}
}

/******************************/

package MyPackage
{
internal class MyClass2 {;
private var var1:int;
public function MyClass2()
{
//Constructor
}
public function MyMethod(NewVar:int)
{
var1 = NewVar;
trace("This value of var1 is "+var1);
}
}
}

/*************************/

ผลการวิ่ง program ครับ

This is MyClass1.
This value of var1 is 10

-----------------------

จากบันทัด var MyBound = obj.MyMethod;

เป็นการ bound method ให้กับตัวแปร MyBound

จากบันทัด MyBound(10); เป็นการเรียกใช้

------------------------------

แถมให้นิดนึงครับ ตัวอย่างนี้ใช้ package ด้วย

ทำให้ใช้ class ใด้หลาย ๆ class แต่อยู่คนละ file กัน

มีการกำหนด internal class เพื่อใช้ class นี้ใน package เท่านั้น

-----------

ข้อ 2 คือ method ที่เวลาสร้าง Instance ขึ้นมาแล้วเรียกใช้

method ผ่าน Instance นั้น ๆ ครับ ดูตัวอย่างที่เคยอธิบายไปแล้วครับ

ข้อ 4 ก็คือ constructor ที่เคยอธิบายไปแล้วครับ

ข้อ 1 เดี๋ยวเอาไว้  ตอน 3 ครับ


One thought on “OOP ใน Flash cs 5.5 ตอนที่ 2

  • New Hands

    อยากทราบว่า code ต่อไปนี้เป็น bound method หรือไม่อย่างไร
    class ThisTest
    {
    private var num:Number = 3;
    function foo():void // bound method defined
    {
    trace("foo's this: " + this);
    trace("num: " + num);
    }
    function bar():Function
    {
    return foo; // bound method returned
    }
    }

    var myTest:ThisTest = new ThisTest();
    var myFunc:Function = myTest.bar();
    trace(this); // output: [object global]
    myFunc();
    /* output:
    foo's this: [object ThisTest]
    output: num: 3 */

    ขอรบกวนอธิบายอย่างละเอียดด้วยครับ

    Reply

Leave a Reply to New Hands Cancel reply

Your email address will not be published. Required fields are marked *

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>