ในวงการเขียนโปรแกรมที่พัฒนาอย่างไม่หยุดยั้งในปัจจุบันนี้ หนึ่งในแนวคิดที่ถูกพูดถึงอย่างกว้างขวางคือแนวคิด Object-Oriented Programming หรือ OOP ซึ่ง TypeScript เป็นหนึ่งในภาษาโปรแกรมมิ่งที่รองรับการเขียนโค้ดภายใต้แนวคิด OOP อย่างเต็มรูปแบบ หนึ่งในคุณสมบัติของ OOP ที่สำคัญคือ Accessibility หรือการควบคุมการเข้าถึงข้อมูลและวิธีการทำงานว่าจะเป็น public, private หรือ protected ตามที่เหมาะสมกับสถานการณ์นั้นๆ
Accessibility หมายถึงการกำหนดระดับการเข้าถึงตัวแปร (properties) และฟังก์ชัน (methods) ภายใน class ซึ่งมีอยู่ 3 ระดับคือ:
1. `public`: สามารถเข้าถึงได้จากทุกที่ทั้งภายในและภายนอกของ class
2. `private`: สามารถเข้าถึงได้เฉพาะภายใน class ที่ประกาศตัวแปรหรือฟังก์ชันนั้นๆ
3. `protected`: คล้ายกับ private แต่สามารถเข้าถึงได้ภายใน class เดียวกันและ subclass ที่สืบทอดมาจาก class นั้น
ประโยชน์ของการกำหนดระดับการเข้าถึงนั้น คือช่วยให้โค้ดของเรามีการจัดการที่ดี ง่ายต่อการบำรุงรักษา และป้องกันการใช้งานที่ไม่ถูกต้องของคลาสหรืออ็อบเจกต์
ต่อไปนี้เป็นตัวอย่างโค้ดที่ใช้การกำหนดการเข้าถึงใน TypeScript:
// ตัวอย่างที่ 1: การประกาศ Class พร้อมกับการกำหนดระดับการเข้าถึง
class Person {
public name: string;
private age: number;
protected gender: string;
constructor(name: string, age: number, gender: string) {
this.name = name;
this.age = age;
this.gender = gender;
}
public greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
private showAge() {
return this.age;
}
protected showGender() {
return this.gender;
}
}
let person1 = new Person("Somchai", 30, "male");
person1.greet(); // Accessible
console.log(person1.name); // Accessible
// console.log(person1.age); // Error: 'age' is private and only accessible within class 'Person'
// person1.showAge(); // Error: 'showAge' is a private method and is not accessible from outside
// ตัวอย่างที่ 2: การสืบทอด Class พร้อมกับการใช้เมธอดที่ถูกป้องกันด้วย protected
class Employee extends Person {
private salary: number;
constructor(name: string, age: number, gender: string, salary: number) {
super(name, age, gender);
this.salary = salary;
}
public showEmployeeDetails() {
console.log(`${this.name} is a ${this.showGender()} and earns ${this.salary}`);
}
}
let employee1 = new Employee("Suda", 40, "female", 50000);
employee1.showEmployeeDetails(); // Accessible
// console.log(employee1.gender); // Error: 'gender' is protected and only accessible within class 'Person' and its subclasses
// ตัวอย่างที่ 3: Encapsulation หรือการซ่อนรายละเอียดพร้อมกับการให้บริการผ่าน method
class BankAccount {
private static accountNumberSeed = 1000;
private accountNumber: number;
private balance: number = 0;
constructor() {
this.accountNumber = BankAccount.accountNumberSeed++;
}
public deposit(amount: number) {
if (amount <= 0) {
throw new Error('Invalid amount');
}
this.balance += amount;
}
public withdraw(amount: number) {
if (amount <= 0 || amount > this.balance) {
throw new Error('Invalid amount');
}
this.balance -= amount;
}
public getAccountDetails() {
console.log(`Account Number: ${this.accountNumber}, Balance: ${this.balance}`);
}
}
let account = new BankAccount();
account.deposit(500);
account.getAccountDetails();
account.withdraw(200);
account.getAccountDetails();
การใช้ Accessibility Controls ใน OOP พบได้ในหลากหลายระบบเช่นการจัดการพนักงาน, ระบบธนาคาร หรือแม้แต่ในการพัฒนาเกมส์ ที่ต้องการกำหนดสิทธิ์การเข้าถึงและป้องกันการใช้ข้อมูลไม่ถูกต้อง เช่น ซ่อนข้อมูลรายได้ของพนักงานจากการเข้าถึงโดยตรง, ป้องกันไม่ให้ทำการถอนเงินมากกว่ายอดเงินคงเหลือในระบบธนาคาร, หรือกำหนดพฤติกรรมของ NPC ในเกมที่ผู้เล่นไม่สามารถเปลี่ยนแปลงได้
การเรียนรู้และทำความเข้าใจเกี่ยวกับการควบคุมการเข้าถึงใน OOP และการประยุกต์ใช้ใน TypeScript นั้น จะเป็นประโยชน์อย่างมากต่อการพัฒนาซอฟต์แวร์ที่มีคุณภาพ หากคุณสนใจที่จะศึกษาและเป็นมืออาชีพด้านการเขียนโปกรมด้วยภาษา TypeScript หรือภาษาโปรแกรมมิ่งอื่นๆ ขอให้คุณพิจารณาสถาบัน EPT ที่พร้อมให้คำปรึกษาและแนะนำคุณในทุกขั้นตอนของการเรียนรู้ อย่างลึกซึ้งและรอบคอบ และสร้างโอกาสในอาชีพของคุณให้กว้างไกลยิ่งขึ้น!
หมายเหตุ: ข้อมูลในบทความนี้อาจจะผิด โปรดตรวจสอบความถูกต้องของบทความอีกครั้งหนึ่ง บทความนี้ไม่สามารถนำไปใช้อ้างอิงใด ๆ ได้ ทาง EPT ไม่ขอยืนยันความถูกต้อง และไม่ขอรับผิดชอบต่อความเสียหายใดที่เกิดจากบทความชุดนี้ทั้งทางทรัพย์สิน ร่างกาย หรือจิตใจของผู้อ่านและผู้เกี่ยวข้อง
หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor
085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM