เรียนรู้การใช้งาน Generic ในภาษา Java

KongRuksiam Studio
2 min readAug 16, 2024

--

Generic คืออะไร

เป็นกระบวนการจัดการประเภทข้อมูลที่ระบุอยู่ภายในคลาสและเมธอดให้มีความยืดหยุ่นตามการเรียกใช้งาน โดยรูปแบบการระบุประเภทข้อมูลนั้นจะเขียนในพื้นที่ <>
หรือเรียกอีกชื่อคือ Parameterized Class หมายถึงคลาสที่สามารถมีพารามิเตอร์ได้โดยพารามิเตอร์ของ Generic Class จะถูกเรียกว่า “Type Parameter” สำหรับจัดการเกี่ยวกับประเภทข้อมูลภายในคลาส

การสร้าง Generic Classes

class ClassName<T>{ //ให้ T เป็นตัวแทนของประเภทข้อมูลหรือคลาสที่สนใจ


}

ตัวอย่าง

class Item<T>{
T data;
public Item(T value){
data=value;
}
}

<T> คืออะไร , ทำไมต้องใช้ T

เราสามารถใช้ตัวอักษรอื่นแทน T ได้ เช่น X , Y , Z เนื่องจากตัวอักษรดังกล่าวเปรียบเสมือนกับการนิยามตัวแปรขึ้นมาใช้งานเพื่อเป็นตัวแทนของประเภทข้อมูลหรือคลาส (Wrapper Classes) ที่เราสนใจเท่านั้น ตัวอย่างตัวอักษรที่นิยมใช้งาน

  • T — Type (ชนิดข้อมูล)
  • E — Element (สมาชิก)
  • K — Key (คีย์)
  • V — Value (ข้อมูล)

การใช้งาน Generic Classes

//การเรียกใช้งาน
ClassName<type1> objName = new ClassName<>(param);
ClassName<type2> objName = new ClassName<>(param);
ClassName<type3> objName = new ClassName<>(param);
ClassName<type4> objName = new ClassName<>(param);

ตัวอย่าง

class Item<T>{
T data;
public Item(T value){
data=value;
}
}

public class Program {
public static void main(String[] args) {
Item<Integer> obj1=new Item<>(99);
System.out.println(obj1.data);
Item<Double> obj2=new Item<>(10.5);
System.out.println(obj2.data);
Item<String> obj3 = new Item<>("Kong");
System.out.println(obj3.data);
Item<Boolean> obj4=new Item<>(true);
System.out.println(obj4.data);
Item<Character> obj5=new Item<>('C');
System.out.println(obj5.data);
}

}

Bounded Type Parameter

คือ การกำหนดข้อบังคับหรือขอบเขตในการจัดการประเภทข้อมูลที่อยู่ใน Type Parameter

โครงสร้างคำสั่ง

class ClassName<T extends type>{

}

ตัวอย่าง

class Item<T extends Number>{ //กำหนดให้รองรับเฉพาะข้อมูลที่เป็นตัวเลขเท่านั้น
T data;
public Item(T value){
data=value;
}
}

public class Program {
public static void main(String[] args) {
Item<Integer> obj1=new Item<>(99);
System.out.println(obj1.data);
Item<Double> obj2=new Item<>(10.5);
System.out.println(obj2.data);
}

}

Multiple Type Parameter

//โครงสร้างคำสั่ง
class ClassName<T , U>{

}
  • T เป็นตัวแทนของประเภทข้อมูลตัวที่ 1
  • U เป็นตัวแทนของประเภทข้อมูลตัวที่ 2

ตัวอย่าง

class Person<T extends String, U extends Integer> {
T name;
U age;
public Person(T name, U age) {
this.name = name;
this.age = age;
}
}

class Product<T extends String, U extends Number> {
T name;
U price;
public Product(T name, U price) {
this.name = name;
this.price = price;
}
}

public class Program {
public static void main(String[] args) {
Person<String,Integer> obj1=new Person<>("kong",30);
System.out.println(obj1.name);
System.out.println(obj1.age);
Product<String, Number> product1 = new Product<>("Mouse", 100);
Product<String, Number> product2 = new Product<>("Keyboard", 199.99);
}
}

Generic Method

คือ การสร้างเมธอดที่สามารถจัดการข้อมูลต่างกันได้

โครงสร้างคำสั่ง (void method)

<T> void methodName(T parameter){
//คำสั่งต่างๆ
}

โครงสร้างคำสั่ง (return method)

<T> T methodName(T parameter){
//คำสั่งต่างๆ
}

ตัวอย่าง

class Data{
static <T> void showArray(T[] arr){
for(T element : arr){
System.out.println(element);
}
System.out.println("----------------");
}
}
public class Program {
public static void main(String[] args) {
Data.showArray(new String[]{"Python","Java","PHP"});
Data.showArray(new Integer[]{10,20,30});
Data.showArray(new Double[]{10.5,20.3,30.4});
Data.showArray(new Boolean[]{true,false,true,true});
}
}

ช่องทางการสนับสนุน
🎓คอร์สเรียน Udemy | 🛒ซื้อของผ่าน Shopee

🌎 ติดตามข่าวสารเพิ่มเติมได้ที่
Facebook | YouTube | TikTok

--

--

KongRuksiam Studio

🚀 เรียนรู้การเขียนโปรแกรมนอกห้องเรียน