【静的メンバ】STEP: 2 コンストラクタ (paizaランク C 相当) 解答例 – PHP編【クラス・構造体メニュー】
【クラス・構造体メニュー】 > 【静的メンバ】STEP: 2 コンストラクタ (paizaランク C 相当)
※リンク先へ移動する為には「paiza」へのログインが必要です。
解答例
|
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 |
<?php class employee{ public $number; public $name; public function __construct($number , $name){ $this->number = $number; $this->name = $name; } public function getnum(){ return $this->number; } public function getname(){ return $this->name; } } $n = trim(fgets(STDIN)); for($i = 0;$i < $n;$i++){ $test = explode(" ",trim(fgets(STDIN))); $a = $test[0]; $b = $test[1]; if(isset($test[2])){ $c = $test[2]; } if($a == "make"){ $ans[] = new employee($b , $c); } elseif($a == "getnum"){ if(isset($ans[$b - 1])){ echo $ans[$b - 1]-> getnum(); echo "\n"; } } elseif($a == "getname"){ if(isset($ans[$b - 1])){ echo $ans[$b - 1]-> getname(); echo "\n"; } } } ?> |