<?php
// factory pattern
class Shape {
static public function getShape($type, $dimension) {
if ($type && $dimension)
{
switch($type)
{
case 'circle':
return new Circle($dimension);
break;
case 'square':
return new Square($dimension);
break;
default:
throw new Exception("Unrecognized shape");
break;
}
}
}
}
class Circle {
private $radius = 0;
public function __construct($radius) {
$this->radius = $radius;
}
public function getArea() {
return $this->radius * $this->radius * pi();
}
}
class Square {
private $side = 0;
public function __construct($side) {
$this->side = $side;
}
public function getArea() {
return $this->side * $this->side;
}
}
$shape = Shape::getShape('circle', 10);
echo $shape->getArea();
echo "\n";
$shape = Shape::getShape('square', 2);
echo $shape->getArea();
echo "\n";
一个基于ASP.NET+MSSQL实现的网站源码,包含一个网站的后台管理、前面展示、留言等常用功能,简单而功能完整,具有相当的学习意义。 采用面向对象模式开发,暂时没有超级管理员管理后台
0
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号