<?php
class Man
{
public function man()
{
return '我是男人';
}
}
class Woman
{
public function woman()
{
return '我是女人';
}
}
class Tangtang
{
public function tangtang()
{
$tt=new Man();
return '我是一名'.$tt->man();
}
public function yangyang()
{
$ss=new Woman();
return '我是一名'.$ss->woman();
}
}
$tangtang=new Tangtang();
echo $tangtang->tangtang();
echo '<br>';
echo $tangtang->yangyang();
echo '<br>';
class Factory
{
public static function create($className)
{
switch (strtolower($className))
{
case 'man':
return new man();
break;
case 'woman':
return new woman();
break;
}
}
}
class Tangtang1
{
public function tangtang()
{
$tt=Factory::create('Man');
return '我是一名'.$tt->man();
}
public function yangyang()
{
$ss=Factory::create('woman');
return '我是一名'.$ss->woman();
}
}
$tangtang1=new Tangtang1();
echo $tangtang1->tangtang();
echo '<br>';
echo $tangtang1->yangyang();
echo '<br>';