• 面向对象的加载类和设计模式


      1 //加载类:
      2 //include("./Ren.class.php");
      3 //include "./Ren.class.php";
      4 
      5 //require("./Ren.class.php");
      6 //require "./Ren.class.php";
      7 
      8 //require_once("./Ren.class.php");
      9 //require_once "./Ren.class.php";
     10 
     11 //自动加载类的方法
     12 //所有的类文件要放在同一个目录下
     13 //所有类文件的命名规则一致
     14 /*
     15 function __autoload($classname)
     16 {
     17     require $classname.".class.php";
     18 }
     19 
     20 
     21 $r = new Ren();*/
     22 
     23 
     24 //设计模式
     25 //1.单例模式
     26 //类的计划生育
     27 //1.让该类在外界无法造对象
     28 //2.让外界可以造一个对象,做一个静态方法返回对象
     29 //3.在类里面通过静态变量控制返回对象只能是一个
     30 
     31 /*class Dog
     32 {
     33     static $dx;
     34     public $test;
     35     
     36     private function __construct()
     37     {
     38         
     39     }
     40     
     41     static function DuiXiang()
     42     {
     43         //return new Dog();
     44         
     45         if(empty(self::$dx))
     46         {
     47             self::$dx = new Dog();
     48         }
     49         
     50         return self::$dx;
     51     }
     52 }
     53 
     54 $a = Dog::DuiXiang();
     55 
     56 
     57 $b = Dog::DuiXiang();
     58 $b->test="hello";
     59 
     60 
     61 var_dump($a);*/
     62 
     63 //工厂模式
     64 /*class YunSuan
     65 {
     66     public $a;
     67     public $b;
     68     
     69     function Jia()
     70     {
     71         return $a+$b;
     72     }
     73     function Jian()
     74     {
     75         return $a-$b;
     76     }
     77 }*/
     78 
     79 abstract class YuanSuan
     80 {
     81     public $a;
     82     public $b;
     83     
     84     function Suan()
     85     {
     86     }
     87 }
     88 
     89 class Jia extends YuanSuan
     90 {
     91     function Suan()
     92     {
     93         return $this->a+$this->b;
     94     }
     95 }
     96 
     97 class Jian extends YuanSuan
     98 {
     99     function Suan()
    100     {
    101         return $this->a-$this->b;
    102     }
    103 }
    104 
    105 class Cheng extends YuanSuan
    106 {
    107     function Suan()
    108     {
    109         return $this->a*$this->b;
    110     }
    111 }
    112 
    113 /*$j = new Cheng();
    114 $j->Suan();
    115 */
    116 
    117 class GongChang
    118 {
    119     static function ShengChan($f)
    120     {
    121         switch($f)
    122         {
    123             case "+":
    124                 return new Jia();
    125                 break;
    126             case "-":
    127                 return new Jian();
    128                 break;
    129             case "*":
    130                 return new Cheng();
    131                 break;
    132         }
    133     }
    134 }
    135 
    136 
    137 $r = GongChang::ShengChan("*");
    138 $r->a=10;
    139 $r->b=5;
    140 echo $r->Suan();
  • 相关阅读:
    面试容易问到的Linux问题
    Java面试题复习笔记(框架)
    Java面试题复习笔记(前端)
    Java面试题复习笔记(数据库)
    Java面试题复习笔记(Web方向)
    【刷题-LeetCode】191 Number of 1 Bits
    【数学】随机方法计算逆矩阵
    【刷题-LeetCode】190 Reverse Bits
    【刷题-LeetCode】188 Best Time to Buy and Sell Stock IV
    python 30 面向对象之 多态
  • 原文地址:https://www.cnblogs.com/The-second/p/6004356.html
Copyright © 2020-2023  润新知