Part1 -> Section2 -> Classes -> 002. Properties

     perperties (หรือจะเรียกว่าคุณสมบัติของคลาส ก็ได้ แต่อย่าเลย เรียก property น่ะดีแล้ว)คือตัวที่ช่วยให้เราควบคุมความสามารถในการเข้าใช้ตัวแปร จาก class อื่น, ซึ่งสำหรับ programming language ที่เป็นแบบ object oriented programming (เช่นพวก C#, Java) นั้นแนะนำว่าควรใช้คุณสมบัติตัวนี้ มันดียังไง มาดูกัน
     properties นั้นจะมีหน้าตาที่คล้ายกับการรวมกันระหว่างตัวแปรที่บรรจุ method ไว้ภายในซึ่งมันจะไม่สามารถรับ parameter (method รับ parameter ได้) ดูตัวอย่างด้านล่าง




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Pen pen = new Pen(); // Create object of class Pen

            pen.Colour = "Blue"; // Call setter of field colour
            Console.WriteLine("Pen colour is: " + pen.Colour); // Call getter of field colour

            Console.ReadLine();
        }
    }

    class Pen
    {
        private string colour; // This is field

        public string Colour // This is property
        {
            get { return colour; }
            set { colour = value; }
        }

    }

}

     โปรแกรมนี้ด้านบนนี้จะเป็นโปรแกรมสำหรับเซ็ตค่าสีลงไปและพรินท์ค่าสีนั้นออกมา



     เรามาดูที่คลาส Pen กันก่อน เริ่มต้นประกาศตัวแปร (ปกติตัวแปรทั่วไปที่ประกาศใน method เราจะเรียก variable แต่ถ้าตัวแปรที่ประกาศนอก method (ตัวแปรของคลาส) เราจะเรียกตัวแปรลักษณะนี้ว่า field (ฟิลด์)) ซึ่งรูปแบบก็จะเป็นตัวแปรปกติ เพิ่มเติมคือมี access modifier ด้านหน้าสุดเพิ่มเข้ามา (private ซึ่งเป็นการระบุว่าสำหรับ field นี้ คนที่จะสามารถเรียกใช้ได้จะต้องอยู่ใน class เดียวกันเท่านั้น พูดง่ายๆคือ class อื่นจะมองไม่เห็น filed ตัวนี้เลย ถ้าจะอยากใช้งานต้องใช้ผ่าน property ที่เราสร้างให้เท่านั้น ทำให้โปรแกรมเรามี security ขึ้นมาเลยนะ ดูโปรมากๆ นี้คือข้อดีของ OOP เลยนะ จะมีอธิบาย access modifier แบบละเอียดในบทความถัดๆไปนะจร๊ะ) จากนั้นจะเป็นการสร้าง property ของ field colour ซึ่งรูปแบบทั่วๆไปคือ

<accessibility> <data type> <Name>
{
     get { return name; }
     set { name = value; }
}

ชื่อของ property ปกติเราจะใช้ชื่อเดียวกับ field แต่ให้ตัวแรกเป็นตัวพิมพ์ใหญ่ (ชื่อเดียวกันเวลาดูโค้ดจะได้ดูง่ายๆว่า perperty ตัวนี้ของ field ไหนนะคัช),  และภายใน get คือ return field ส่วน set นั้นหลายคนอาจจะงงว่า value นั้นมาจากไหน ผมบอกเลยว่าผมก็งง 55 แต่ให้เราเข้าใจว่า value คือค่าที่เราส่งมาจาก class อื่นเพื่อ set ค่าให้กับ field และต้องชื่อ value เท่านั้นะคัช ต้องพิมพ์เป๊ะๆเลย ไม่งั้น error

     คราวนี้เราลองมาดูที่ main method กันบ้าง ในมุมมองของ class อื่นๆที่ไม่ใช่ class Pen นั้น การที่จะเรียกใช้งาน field colour จะต้องเรียกผ่าน object เราก็สร้าง object จากนั้นก็ set สี Blue ให้กับ field colour ผ่าน property Colour เพราะเราจะมองไม่เห็น field colour เลย (สังเกตุ Colour ตัว C พิมพ์ใหญ่ ซึ่งก็คือ property นะคัช) สุดท้ายก็ปรินท์ออกมา

   เราสามารถ ปรับแต่งการเก็บข้อมูลและการ return ข้อมูลได้ด้วย ดูตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Pen pen = new Pen(); // Create object of class Pen

            pen.Colour = "Red"; // Call setter of field colour
            Console.WriteLine("Pen colour is: " + pen.Colour); // Call getter of field colour

            Console.ReadLine();
        }
    }

    class Pen
    {
        private string colour; // This is field

        public string Colour // This is property
        {
            get { return colour.ToUpper(); }
            set
            {
                if (value == "Blue")
                {
                    colour = value;
                }
                else
                {
                    colour = "Other";
                }
            }
        }

    }

}

     ผลการรันโปรแกรมจะเป็นดังรูป


     สุดท้ายนี้ผมมีเทคนิคเล็กน้อยสำหรับโปรแกรมเมอร์จอมขี้เกียจ คือ visual studio มีการสร้างคำสั่งสำหรับ generate โค้ดที่ใช้บ่อยๆ ในที่นี้เราจะลองใช้ตัวช่วยที่เรียกว่า refactor ขั้นตอนง่ายเลย คือให้คลิกขวาที่ field เลือก Refactor -> Encapsulate Field... เห็นไหมครัช ง่ายสุดๆสำหรับคนขี้เกียจ ผมก็ชอบใช้


     จบแล้วสำหรับ properties ในบทความต่อไปเราจะมาทำความรู้จักกับ Contructor and Destructor หนุกแน่นอน

สอบถามหรือติดตามได้ที่ https://www.facebook.com/learnaspnetmvcjquery

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Part2 -> Section1 The Basic -> 003. MVC Folder Structure

Part2 -> Section1 The Basic -> 001. Introduction

Part2 -> Section1 The Basic -> 005. Controller