Examples of php abstract classes Learn how to implement php abstract classes

WBOY
Release: 2016-07-25 08:56:10
Original
841 people have browsed it
  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. ?>

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template