Repository navigation
Expand file tree
/
Copy pathAdapter.php
More file actions
112 lines (86 loc) · 1.69 KB
/
Copy pathAdapter.php
File metadata and controls
112 lines (86 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
//篮球翻译适配器
abstract class Player
{
protected $name;
function __construct($name)
{
$this->name = $name;
}
abstract public function Attack();
abstract public function Defense();
}
//前锋
class Forwards extends Player
{
public function Attack()
{
echo "前锋:".$this->name." 进攻\n";
}
public function Defense()
{
echo "前锋:".$this->name." 防守\n";
}
}
//中锋
class Center extends Player
{
function __construct()
{
parent::__construct();
}
public function Attack()
{
echo "中锋:".$this->name." 进攻\n";
}
public function Defense()
{
echo "中锋:".$this->name." 防守\n";
}
}
//外籍中锋
class ForeignCenter
{
private $name;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function 进攻()
{
echo "外籍中锋:".$this->name." 进攻\n";
}
public function 防守()
{
echo "外籍中锋:".$this->name." 防守\n";
}
}
//翻译者
class Translator extends Player
{
private $foreignCenter;
function __construct($name)
{
$this->foreignCenter = new ForeignCenter();
$this->foreignCenter->setName($name);
}
public function Attack()
{
$this->foreignCenter->进攻();
}
public function Defense()
{
$this->foreignCenter->防守();
}
}
// 客户端代码
$forwards = new Forwards("巴蒂尔");
$forwards->Attack();
$forwards->Defense();
$translator = new Translator("姚明");
$translator->Attack();
$translator->Defense();