<?php// Parent classabstractclassCar{public$name;publicfunction__construct($name){$this->name =$name;}abstractpublicfunctionintro():string;}// Child classesclassAudiextendsCar{publicfunctionintro():string{return"Choose German quality! I'm an $this->name!";}}classVolvoextendsCar{publicfunctionintro():string{return"Proud to be Swedish! I'm a $this->name!";}}classCitroenextendsCar{publicfunctionintro():string{return"French extravagance! I'm a $this->name!";}}// Create objects from the child classes$audi=newaudi("Audi");echo$audi->intro();echo"<br>";$volvo=newvolvo("Volvo");echo$volvo->intro();echo"<br>";$citroen=newcitroen("Citroen");echo$citroen->intro();?>
จากตัวอย่าง คลาส Audi, Volvo และ Citroen ถูกสืบทอดมาจากคลาส Car ดังนั้นคลาส Audi, Volvo, และ Citroen จะสามารถใช้สมบัติ $name ที่เป็นแบบ public และเมธอด __construct() ที่เป็นแบบ public จากคลาส Car ได้ โดยที่เมธอด intro() เป็น Abstract Methods จะต้องประกาศซ้ำในคลาสลูกทุกคลาส และต้องคืนค่าเป็นสตริง
Javatpoint. (n.d.). Abstraction in Python: [Abstraction classes in Python], [Abstract Base Classes], [Working of the Abstract Classes], [Example -]. Javatpoint. https://www.javatpoint.com/abstraction-in-python
Choose German quality! I'm an Audi!
Proud to be Swedish! I'm a Volvo!
French extravagance! I'm a Citroen!
<?php
abstract class AbstractClass {
// Our abstract method only needs to define the required arguments
abstract protected function prefixName($name);
}
class ConcreteClass extends AbstractClass {
// Our child class may define optional arguments not in the parent's signature
public function prefixName($name, $separator = ".") {
if ($name == "Pacman") {
$prefix = "Mr";
} elseif ($name == "Pacwoman") {
$prefix = "Mrs";
} else {
$prefix = "";
}
return "{$prefix}{$separator} {$name}";
}
}
$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";
?>
Mr. Pacman
Mrs. Pacwoman
<?php
abstract class Animal {
public $name;
public $age;
public function Describe() {
return $this->name . ", " . $this->age . " years old";
}
abstract public function Greet();
}
class Dog extends Animal {
public function Greet() {
return "Woof!";
}
public function Describe() {
return parent::Describe() . ", and I'm a dog!";
}
}
$animal = new Dog();
$animal->name = "Bob";
$animal->age = 7;
echo $animal->Describe();
echo $animal->Greet();
?>
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
// Abstract class
abstract class Animal
{
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}
}
// Derived class (inherit from Animal)
class Pig : Animal
{
public override void animalSound()
{
// The body of animalSound() is provided here
Console.WriteLine("The pig says: wee wee");
}
}
class Program
{
static void Main(string[] args)
{
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound(); // Call the abstract method
myPig.sleep(); // Call the regular method
}
}
# Python program demonstrate
# abstract base class work
from abc import ABC, abstractmethod
class Car(ABC):
def mileage(self):
pass
class Tesla(Car):
def mileage(self):
print("The mileage is 30kmph")
class Suzuki(Car):
def mileage(self):
print("The mileage is 25kmph ")
class Duster(Car):
def mileage(self):
print("The mileage is 24kmph ")
class Renault(Car):
def mileage(self):
print("The mileage is 27kmph ")
# Driver code
t= Tesla ()
t.mileage()
r = Renault()
r.mileage()
s = Suzuki()
s.mileage()
d = Duster()
d.mileage()
Bob, 7 years old, and I'm a dog!Woof!
The pig says: wee wee
Zzz
The pig says: wee wee
Zzz
The mileage is 30kmph
The mileage is 27kmph
The mileage is 25kmph
The mileage is 24kmph