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

原创
2016-07-25 08:56:10 561浏览
  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. ?>

复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:php.ini安全配置详解 下一条:php导出excel格式文件的例子

相关文章

查看更多