interface InterfaceName {
public function someMethod1();
public function someMethod2();
function someMethod3(); #ไม่ต้องประกาศว่าเป็น public ก็ได้
}
interface iTelevision {
public function turnOn();
public function turnOff();
public function changeChannel($newChannel);
}
<?php
// ประกาศอินเทอร์เฟซ iTelevision
interface iTelevision {
public function turnOn(); // เมธอดเปิดทีวี
public function turnOff(); // เมธอดปิดทีวี
public function changeChannel($newChannel); // เมธอดเปลี่ยนช่องทีวี
}
// เมธอดภายในคลาส Television จะต้องมีการทำงานทั้งหมด
class Television implements iTelevision {
// ตัวแปรของคลาส Televison
public $currentChannel = 1; // ประกาศให้ช่องทีวีปัจจุบันมีค่าเป็น 1
public function turnOn() { // เมธอดที่แจ้งเมื่อมีการเปิดทีวี
echo "Television has turned on\n";
}
public function turnOff() { // เมธอดที่แจ้งเมื่อมีการปิดทีวี
echo "Television has turned off\n";
}
public function changeChannel($newChannel) { // เมธอดในการเปลี่ยนช่องทีวี
echo "Changed channel from ";
echo "$this->currentChannel to $newChannel\n";
$this->currentChannel = $newChannel;
}
}
// เรียกใช้งานคลาส
$myTV = new Television(); // สร้างออบเจ็กต์ของคลาส Television ชื่อว่า $myTV
$myTV->turnOn(); // เรียกเมธอดเปิดทีวี
$myTV->changeChannel(5); // เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 5
$myTV->turnOff(); // เรียกเมธอดปิดทีวี
?>
#include <stdio.h>
int currentChannel; //ตัวแปรช่องทีวี
};
//ในภาษา C ไม่มีคำว่า interface แต่ใช้ struct และ function pointer แทนคลาส Television
//ฟังก์ชันสำหรับการเปิดทีวี
void turnOn(struct Television *tv) {
printf("Television has turned on\n");
}
//ฟังก์ชันสำหรับการปิดทีวี
void turnOff(struct Television *tv) {
printf("Television has turned off\n");
}
//ฟังก์ชันสำหรับการเปลี่ยนช่องทีวี
void changeChannel(struct Television *tv, int newChannel) {
printf("Changed channel from %d to %d\n", tv->currentChannel, newChannel);
tv->currentChannel = newChannel;
}
int main() {
//สร้าง object ของ struct Television
struct Television myTV;
myTV.currentChannel = 1; //กำหนดค่าเริ่มต้นของ currentChannel
//เรียกใช้ฟังก์ชันต่างๆ แทนการเรียกใช้เมธอดของคลาส
turnOn(&myTV); //เรียกฟังก์ชันเปิดทีวี
changeChannel(&myTV, 5); //เรียกฟังก์ชันเปลี่ยนช่องไปที่ช่อง 5
turnOff(&myTV); //เรียกฟังก์ชันปิดทีวี
return 0;
}
//ในภาษา Java การสร้างคลาสและเมธอดจะคล้ายกับ PHP แต่มีความต่างในเรื่องของไวยากรณ์และการจัดการเมธอด
interface iTelevision { //สร้าง interface iTelevision
void turnOn(); //เมธอดสำหรับเปิดทีวี
void turnOff(); //เมธอดสำหรับปิดทีวี
void changeChannel(int newChannel); //เมธอดสำหรับเปลี่ยนช่องทีวี
}
//คลาส Television ที่ implement อินเทอร์เฟซ iTelevision
class Television implements iTelevision {
//ประกาศตัวแปรของคลาส Television
private int currentChannel = 1;
@Override
public void turnOn() { //เมธอดที่แจ้งเมื่อมีการเปิดทีวี
System.out.println("Television has turned on");
}
@Override
public void turnOff() { //เมธอดที่แจ้งเมื่อมีการปิดทีวี
System.out.println("Television has turned off");
}
@Override
public void changeChannel(int newChannel) { //เมธอดในการเปลี่ยนช่องทีวี
System.out.println("Changed channel from " + currentChannel + " to " + newChannel);
currentChannel = newChannel;
}
public static void main(String[] args) { //main function เพื่อรันโค้ด
Television myTV = new Television();//สร้าง object ของคลาส Television
myTV.turnOn(); //เรียกเมธอดเปิดทีวี
myTV.changeChannel(5); //เรียกเมธอดเปลี่ยนช่องไปที่ช่อง 5
myTV.turnOff();//เรียกเมธอดปิดทีวี
}
}
#สร้างคลาส iTelevision ซึ่งเปรียบเสมือน interface ใน PHP
class iTelevision:
def turnOn(self):
raise NotImplementedError #ใช้บอกว่าต้อง implement เมธอดนี้ในคลาสที่สืบทอด
def turnOff(self):
raise NotImplementedError
def changeChannel(self, newChannel):
raise NotImplementedError
#สร้างคลาส Television ที่ implement เมธอดจาก iTelevision
class Television(iTelevision):
#ตัวแปรของคลาส Television
currentChannel = 1
#เมธอดที่แจ้งเมื่อมีการเปิดทีวี
def turnOn(self):
print("Television has turned on")
#เมธอดที่แจ้งเมื่อมีการปิดทีวี
def turnOff(self):
print("Television has turned off")
#เมธอดที่แจ้งเมื่อมีการเปลี่ยนช่องทีวี
def changeChannel(self, newChannel):
print(f"Changed channel from {self.currentChannel} to {newChannel}")
self.currentChannel = newChannel
#เรียกใช้งานคลาส
myTV = Television() #สร้างออบเจ็กต์ของคลาส Television ชื่อว่า myTV
myTV.turnOn() #เรียกเมธอดเปิดทีวี
myTV.changeChannel(5) #เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 5
myTV.turnOff() #เรียกเมธอดปิดทีวี
Output:
Television has turned on
Changed channel from 1 to 5
Television has turned off
<?php
interface iTelevision { //อินเทอร์เฟซแรก
public function turnOn();
public function turnOff();
public function changeChannel($newChannel);
}
interface iVolume { //อินเทอร์เฟซที่สอง
public function volumeUp();
public function volumeDown();
}
//คลาส Television มีการ implement อินเทอร์เฟซแรก และอินเทอร์เฟซที่สอง โดยใช้เครื่องหมายคอมมา ","
class Television implements iTelevision, iVolume {
//ตัวแปรของคลาส Televison
public $currentChannel = 1; //ให้ช่องทีวีช่องปัจจุบันมีค่าเป็น 1
public $currentVolume = 50; //ให้ระดับเสียงทีวีมีค่าเป็น 50
//implement อินเทอร์เฟซ iTelevision
public function turnOn() { //เมธอดที่แจ้งเมื่อมีการเปิดทีวี
echo "Television has turned on\n";
}
public function turnOff() { //เมธอดที่แจ้งเมื่อมีการปิดทีวี
echo "Television has turned off\n";
}
public function changeChannel($newChannel) { //เมธอดในการเปลี่ยนช่องทีวี
echo "Changed channel from ";
echo "$this->currentChannel to $newChannel\n";
$this->currentChannel = $newChannel;
}
//implement อินเทอร์เฟซ iVolume
public function volumeUp() { //เมธอดเพิ่มระดับเสียงทีวีครั้งละ 5
$this->currentVolume += 5;
echo "Volume up to $this->currentVolume%\n";
}
public function volumeDown() { //เมธอดลดระดับเสียงทีวีครั้งละ 5
$this->currentVolume -= 5;
echo "Volume down to $this->currentVolume%\n";
}
//เมธอดของคลาส Television
public function addToFavoriteChannel() { //เมธอดบันทึกช่องทีวีโปรด
echo "Channel $this->currentChannel added to favorite channel \n";
}
}
//เรียกใช้งานคลาส
$tv = new Television(); //สร้างออบเจ็กต์ของคลาส Television ชื่อว่า $tv
$tv->turnOn(); //เรียกเมธอดเปิดทีวี
$tv->volumeUp(); //เรียกเมธอดเพิ่มระดับเสียงทีวี
$tv->volumeUp();
$tv->changeChannel(34); //เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 34
$tv->changeChannel(10); //เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 10
$tv->addToFavoriteChannel(); //เรียกเมธอดบันทึกช่องทีวีโปรด
$tv->turnOff(); //เรียกเมธอดปิดทีวี
?>
#include <stdio.h>
//ในภาษา C ไม่มีคำว่า interface แต่ใช้ struct และ function pointer แทน
typedef struct iTelevision { //สร้าง struct แรก
void (*turnOn)(); //ฟังก์ชัน pointer สำหรับเปิดทีวี
void (*turnOff)();
void (*changeChannel)(int newChannel);
} iTelevision;
typedef struct iVolume { //สร้าง struct ที่สอง
void (*volumeUp)();
void (*volumeDown)();
} iVolume;
//คลาส Television มีการ implement struct แรก struct ที่สอง
typedef struct Television {
iTelevision tv; //การใช้ struct ของ iTelevision
iVolume vol; //การใช้ struct ของ iVolume
int currentChannel; //ช่องทีวีปัจจุบัน
int currentVolume; //ระดับเสียงทีวี
} Television;
void turnOn() { //ฟังก์ชันการเปิดทีวี
printf("Television has turned on\n");
}
void turnOff() { //ฟังก์ชันการปิดทีวี
printf("Television has turned off\n");
}
void changeChannel(int newChannel) { //ฟังก์ชันเปลี่ยนช่องทีวี
printf("Changed channel from %d to %d\n", newChannel);
}
void volumeUp(Television *tv) { //ฟังก์ชันเพิ่มระดับเสียงครั้งละ 5
tv->currentVolume += 5;
printf("Volume up to %d%%\n", tv->currentVolume);
}
void volumeDown(Television *tv) { //ฟังก์ชันลดระดับเสียงครั้งละ 5
tv->currentVolume -= 5;
printf("Volume down to %d%%\n", tv->currentVolume);
}
void addToFavoriteChannel(Television *tv) { //ฟังก์ชันบันทึกช่องโปรด
printf("Channel %d added to favorite channel\n", tv->currentChannel);
}
Television createTelevision() { //ฟังก์ชันสร้างออบเจ็กต์ Television
Television tv;
tv.tv.turnOn = turnOn; //กำหนดฟังก์ชันเปิดทีวี
tv.tv.turnOff = turnOff; //กำหนดฟังก์ชันปิดทีวี
tv.tv.changeChannel = changeChannel; //กำหนดฟังก์ชันเปลี่ยนช่อง
tv.vol.volumeUp = volumeUp; //กำหนดฟังก์ชันเพิ่มเสียง
tv.vol.volumeDown = volumeDown; //กำหนดฟังก์ชันลดเสียง
tv.currentChannel = 1; //ช่องทีวีเริ่มต้น
tv.currentVolume = 50; //ระดับเสียงเริ่มต้น
return tv;
}
//เรียกใช้งานคลาสภายในเมน
int main() {
Television tv = createTelevision(); //สร้างออบเจ็กต์จากคลาส Television
tv.tv.turnOn(); //เรียกเมธอดเปิดทีวี
tv.vol.volumeUp(); //เรียกเมธอดเพิ่มระดับเสียงทีวี
tv.vol.volumeUp();
tv.tv.changeChannel(34); //เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 34
tv.tv.changeChannel(10); //เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 10
addToFavoriteChannel(&tv); //เรียกเมธอดบันทึกช่องทีวีโปรด
tv.tv.turnOff(); //เรียกเมธอดปิดทีวี
return 0;
}
//อินเทอร์เฟซแรก
interface iTelevision {
void turnOn(); //เมธอดเปิดทีวี
void turnOff(); //เมธอดปิดทีวี
void changeChannel(int newChannel); //เมธอดเปลี่ยนช่องทีวี
}
//อินเทอร์เฟซที่สอง
interface iVolume {
void volumeUp(); //เมธอดเพิ่มระดับเสียง
void volumeDown(); //เมธอดลดระดับเสียง
}
//คลาส Television มีการ implement อินเทอร์เฟซแรก และอินเทอร์เฟซที่สอง
class Television implements iTelevision, iVolume {
//ตัวแปรของคลาส Television
private int currentChannel = 1; //ช่องทีวีปัจจุบันเริ่มต้นที่ 1
private int currentVolume = 50; //ระดับเสียงทีวีเริ่มต้นที่ 50
//implement อินเทอร์เฟซ iTelevision
public void turnOn() { //เมธอดที่แจ้งเมื่อมีการเปิดทีวี
System.out.println("Television has turned on");
}
public void turnOff() { //เมธอดที่แจ้งเมื่อมีการปิดทีวี
System.out.println("Television has turned off");
}
public void changeChannel(int newChannel) { //เมธอดในการเปลี่ยนช่องทีวี
System.out.println("Changed channel from " + currentChannel + " to " + newChannel);
this.currentChannel = newChannel; //เปลี่ยนช่องปัจจุบัน
}
//implement อินเทอร์เฟซ iVolume
public void volumeUp() { //เมธอดเพิ่มระดับเสียงทีวีครั้งละ 5
currentVolume += 5;
System.out.println("Volume up to " + currentVolume + "%");
}
public void volumeDown() { //เมธอดลดระดับเสียงทีวีครั้งละ 5
currentVolume -= 5;
System.out.println("Volume down to " + currentVolume + "%");
}
//เมธอดของคลาส Television
public void addToFavoriteChannel() { //เมธอดบันทึกช่องทีวีโปรด
System.out.println("Channel " + currentChannel + " added to favorite channel");
}
//เมธอดหลักสำหรับเรียกใช้งานคลาส
public static void main(String[] args) {
Television tv = new Television(); //สร้างออบเจ็กต์ของคลาส Television
tv.turnOn(); //เรียกเมธอดเปิดทีวี
tv.volumeUp(); //เรียกเมธอดเพิ่มระดับเสียงทีวี
tv.volumeUp();
tv.changeChannel(34); //เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 34
tv.changeChannel(10); //เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 10
tv.addToFavoriteChannel(); //เรียกเมธอดบันทึกช่องทีวีโปรด
tv.turnOff(); //เรียกเมธอดปิดทีวี
}
}
#ใน Python จะใช้การสร้างคลาสที่มีแต่เมธอด abstract แทนการใช้คำว่า interface
from abc import ABC, abstractmethod
class iTelevision(ABC):
@abstractmethod
def turnOn(self): #เมธอด abstract เปิดทีวี
pass
@abstractmethod
def turnOff(self): #เมธอด abstract ปิดทีวี
pass
@abstractmethod
def changeChannel(self, newChannel): #เมธอด abstract เปลี่ยนช่องทีวี
pass
class iVolume(ABC):
@abstractmethod
def volumeUp(self): #เมธอด abstract เพิ่มระดับเสียง
pass
@abstractmethod
def volumeDown(self): #เมธอด abstract ลดระดับเสียง
pass
#คลาส Television มีการ implement คลาส iTelevision, iVolume
class Television(iTelevision, iVolume):
def __init__(self):
#ตัวแปรของคลาส Television
self.currentChannel = 1 #ช่องทีวีปัจจุบันเริ่มต้นที่ 1
self.currentVolume = 50 #ระดับเสียงทีวีเริ่มต้นที่ 50
#implement คลาส iTelevision
def turnOn(self): #เมธอดที่แจ้งเมื่อมีการเปิดทีวี
print("Television has turned on")
def turnOff(self): #เมธอดที่แจ้งเมื่อมีการปิดทีวี
print("Television has turned off")
def changeChannel(self, newChannel): #เมธอดในการเปลี่ยนช่องทีวี
print(f"Changed channel from {self.currentChannel} to {newChannel}")
self.currentChannel = newChannel #เปลี่ยนช่องปัจจุบัน
#implement คลาสiVolume
def volumeUp(self): #เมธอดเพิ่มระดับเสียงทีวีครั้งละ 5
self.currentVolume += 5
print(f"Volume up to {self.currentVolume}%")
def volumeDown(self): #เมธอดลดระดับเสียงทีวีครั้งละ 5
self.currentVolume -= 5
print(f"Volume down to {self.currentVolume}%")
def addToFavoriteChannel(self): #เมธอดบันทึกช่องทีวีโปรด
print(f"Channel {self.currentChannel} added to favorite channel")
#เรียกใช้งานคลาส
tv = Television() #สร้างออบเจ็กต์ของคลาส Television
tv.turnOn() #เรียกเมธอดเปิดทีวี
tv.volumeUp() #เรียกเมธอดเพิ่มระดับเสียงทีวี
tv.volumeUp()
tv.changeChannel(34) #เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 34
tv.changeChannel(10) #เรียกเมธอดเปลี่ยนช่องทีวีไปช่อง 10
tv.addToFavoriteChannel() #เรียกเมธอดบันทึกช่องทีวีโปรด
tv.turnOff() #เรียกเมธอดปิดทีวี
Output:
Television has turned on
Volume up to 55%
Volume up to 60%
Changed channel from 1 to 34
Changed channel from 34 to 10
Chanel 10 added to favorite channel
Television has turned off
<?php
//อินเทอร์เฟซแรก
interface iTelevision {
public function turnOn();
public function turnOff();
}
//อินเทอร์เฟซที่สองขยายจาก iTelevision
interface iSmartTelevision extends iTelevision {
public function browseInternet();
public function streamVideo();
}
//คลาสที่นำอินเทอร์เฟซ iSmartTelevision ที่ขยายอินเทอร์เฟซแรกไปใช้
class SmartTelevision implements iSmartTelevision {
public function turnOn() { //เมธอดเปิดทีวีที่ขยายจากอินเทอร์เฟซ iTelevison
echo "Smart Television has turned on\n";
}
public function turnOff() { //เมธอดปิดทีวีขยายจากอินเทอร์เฟซ iTelevison
echo "Smart Television has turned off\n";
}
public function browseInternet() { //เมธอดในการค้นหาอินเทอร์เน็ต
echo "Browsing the internet\n";
}
public function streamVideo() { //เมธอดสตรีมมิ่งวิดีโอ
echo "Streaming video\n";
}
}
//เรียกใช้งานคลาส
$tv = new SmartTelevision(); //สร้างออบเจ็กต์จากคลาส Television ชื่อว่า $tv
$tv->turnOn(); //เรียกเมธอดเปิดทีวี
$tv->browseInternet(); //เรียกเมธอดในการค้นหาอินเทอร์เน็ต
$tv->streamVideo(); //เรียกเมธอดสตรีมมิ่งวิดีโอ
$tv->turnOff(); //เรียกเมธอดปิดทีวี
?>