class CCitizen //公民
{
var $id; //身份证号是公民的唯一标志
var $name;
var $sex;
...
};
class CCompany //上市公司
{
var $exchange; //上市的交易所
var $manager; // 一个经理
var $employees; // N 个雇员
var $friends; //战略伙伴企业
function CCompany($exc) //构造,假设公司对应的交易所是固定不变的
{
$this->exchange = $exc;
$this->manager = "";
$this->employees = Array();
$this->friends = Array();
}
function EmpolyManager($manager) //聘用或更换经理
{
$this->manager = $manager;
}
function Employ($person) //雇佣普通雇员
{
$this->employees[] = $person;
}
function Dismiss($person) //解雇雇员
{
for ( $i = 0; $i < count($this->employees); $i ++ )
{
if ( $this->employees[$i]->id == $person->id )
{
array_splice($this->employees, $i, 1);
}
}
}
function AddFriend($friend)
{
$this->friends[] = $friend;
}
..
};
$Nasdaq = new CExchange;
$BinLaden = new CCitizen;
$JodgeBush = new CCitizen;
$Sadam = new CCitizen; //“萨达姆”是这么拼的吗?呵呵
$TheBase = new CCompany($Nasdaq);
$TheBase->EmpolyManager($BinLaden);
$TheBase->Employ($JodgeBush);
$TheBase->Employ($Sadam);
$TheBase->AddFriend($Taliban);
...
class CCompany 与 class CExchange 是 N 对 1 关联(假设一个公司只能在一个地方上市)
class CCompany 与 class CCitizen 有 1 对 1 关联($manager),也有 1 对 N 关联($employees)(假设一个人只能同时受雇于一个公司)
class CCompany 与 class CCompany 是 N 对 N 关联(同时还是自身关联),因为公司 A 可以与公司 B,C,D 有合作关系,B 同时又可以与其它许多公司有关联。