You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB

3 years ago
  1. <?php
  2. use a\cls_a;
  3. class add{
  4. public function getVal($n,$m){
  5. return $n + $m;
  6. }
  7. }
  8. class sub
  9. {
  10. public function getVal($n,$m)
  11. {
  12. return $n-$m;
  13. }
  14. }
  15. class mul
  16. {
  17. public function getVal($n,$m)
  18. {
  19. return $n * $m;
  20. }
  21. }
  22. class div
  23. {
  24. public function getVal($n,$m)
  25. {
  26. return $n / $m;
  27. }
  28. }
  29. //工程类:用来创建对象
  30. class mathOpt{
  31. public static function createObj($opt){
  32. switch ($opt)
  33. {
  34. case '+':
  35. return new add();
  36. break;
  37. case '-':
  38. return new sub();
  39. break;
  40. case '*':
  41. return new mul();
  42. break;
  43. case '/':
  44. return new div();
  45. break;
  46. }
  47. }
  48. }
  49. $obj = mathOpt::createObj('*');
  50. $ret=$obj -> getVal(5,7);
  51. echo $ret;
  52. ?>