我正在用 php 编写一个足球经理模拟器,[HARD AlGORITHMS!]
我有 3 节课:
播放器
class Player {
protected $name;
public function addAttr($name) {
$this->name = $name;
}
}
团队
class Team {
protected $name;
protected $players = array();
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function addPlayer($player) {
$this->players[] = $player;
}
public function getPlayers() {
print_r($this->players);
}
public function getOpponetsPosition() {
GAME::getOpponetPlayersPosition();
}
和游戏
class Game {
protected $t1;
protected $t2;
function setTeams($team1,$team2) {
$this->t1 = $team1;
$this->t2 = $team2;
}
function getOpponetPlayersPosition() {
$this->t1->getPlayers();
}
}
和主脚本
require_once 'classes/CPlayer.php';
require_once 'classes/CTeam.php';
require_once 'classes/CGame.php';
$game = new Game;
$team1 = new Team;
$team1->setName("PO-1");
$team2 = new Team;
$team2->setName("PO-2");
$p1 = new Player;
$p2 = new Player;
$p1->addAttr("payam babaiy");
$p2->addAttr("parsa babaiy");
$team1->addPlayer($p1);
$team2->addplayer($p2);
$game->setTeams($team1,$team2);
$team1->getOpponetsPosition();
我需要在Team类中使用getOpponetsPosition()函数获取游戏中的所有玩家位置
但它不返回我在主脚本中输入的值。我这样做对吗?这是应用即时通讯写作的好方法吗?