php抽象类的例子 学习php抽象类的实现方法

WBOY
Lepaskan: 2016-07-25 08:56:10
asal
841 orang telah melayarinya
  1. //定义一个抽象类

  2. abstract class Staff
  3. {
  4. abstract function hire();
  5. abstract function fire();
  6. abstract function promote();
  7. abstract function demote();
  8. }
  9. ?>

复制代码

例2,php抽象类的例子

  1. class Employee {

  2. private $title;
  3. private $lastName;
  4. private $firstName;
  5. protected $salary;
  6. private $ratio = 0;
  7. public function __construct($title, $firstName, $mainName, $salary ) {
  8. $this->title = $title;
  9. $this->firstName = $firstName;
  10. $this->lastName = $mainName;
  11. $this->salary = $salary;
  12. }
  13. public function firstName() {

  14. return $this->firstName;
  15. }
  16. public function getlastName() {

  17. return $this->lastName;
  18. }
  19. public function setRatio( $num ) {

  20. $this->ratio=$num;
  21. }
  22. public function getRatio() {

  23. return $this->ratio;
  24. }
  25. public function getTitle() {
  26. return $this->title;
  27. }
  28. public function getSalary() {

  29. return ($this->salary - $this->ratio);
  30. }
  31. public function getFullName() {

  32. return "{$this->firstName}" . " {$this->lastName}";
  33. }
  34. function getSummaryLine() {

  35. $base = "$this->title ( $this->lastName, ";
  36. $base .= "$this->firstName )";
  37. return $base;
  38. }
  39. }
  40. //定义抽象类

  41. abstract class EmployeeWriter {
  42. abstract static function write( Employee $shopProduct );
  43. }
  44. class TextEmployeeWriter extends EmployeeWriter {

  45. static function write( Employee $shopEmployee ) {
  46. $str = "{$shopEmployee->getTitle()}: ";
  47. $str .= $shopEmployee->getFullName();
  48. $str .= " ({$shopEmployee->getSalary()})\n";
  49. print $str;
  50. }
  51. }
  52. $developer1 = new Employee("A", "A1", "A2", 5.99 );

  53. TextEmployeeWriter::write( $developer1 );
  54. ?>
复制代码


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan