no message
This commit is contained in:
45
Question13-Inheritance/common/Animal.php
Normal file
45
Question13-Inheritance/common/Animal.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Common;
|
||||
|
||||
class Animal
|
||||
{
|
||||
/** 名前 */
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* 名前をセットするセッター
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 名前を取得するゲッター
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 「{name}は走る」と出力する
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
echo $this->name . 'は走る';
|
||||
}
|
||||
|
||||
/**
|
||||
* 「{name}は呼吸する」と出力する
|
||||
*/
|
||||
public function breathe()
|
||||
{
|
||||
echo $this->name . 'は呼吸する';
|
||||
}
|
||||
}
|
||||
97
Question13-Inheritance/common/Character.php
Normal file
97
Question13-Inheritance/common/Character.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace inheritance\Common;
|
||||
|
||||
class Character
|
||||
{
|
||||
|
||||
/** 名前 */
|
||||
private $name = 'キャラクター';
|
||||
|
||||
/** 体力 */
|
||||
private $hp;
|
||||
|
||||
/** 力 */
|
||||
private $power;
|
||||
|
||||
/** 防御力 */
|
||||
private $endurance;
|
||||
|
||||
/**
|
||||
* 体力、攻撃力、防御力の初期値を設定しつつ、Characterインスタンスを生成します
|
||||
*
|
||||
* @param int $hp
|
||||
* @param int $power
|
||||
* @param int $endurance
|
||||
*/
|
||||
public function __construct($hp, $power, $endurance)
|
||||
{
|
||||
$this->hp = $hp;
|
||||
$this->power = $power;
|
||||
$this->endurance = $endurance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 名前を設定します
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 名前を取得します
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 攻撃を行います
|
||||
*
|
||||
* @return int 攻撃力を返す
|
||||
*/
|
||||
public function attack()
|
||||
{
|
||||
return $this->power;
|
||||
}
|
||||
|
||||
/**
|
||||
* ダメージ判定を行います
|
||||
*
|
||||
* @param int $attack
|
||||
* @return bool 体力が0になったら true、まだ残っていれば false
|
||||
*/
|
||||
public function damage($attack)
|
||||
{
|
||||
$calc = $attack - $this->endurance;
|
||||
if ($calc <= 0) {
|
||||
$calc = 1;
|
||||
}
|
||||
$this->hp -= $calc;
|
||||
if ($this->hp <= 0) {
|
||||
$this->hp = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* キャラクター情報を表示します
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function showParameter()
|
||||
{
|
||||
$str = nl2br('名 前:' . $this->name
|
||||
. "\r\n" . '体 力:' . $this->hp
|
||||
. "\r\n" . ' 力 :' . $this->power
|
||||
. "\r\n" . '耐 久:' . $this->endurance);
|
||||
return $str;
|
||||
}
|
||||
}
|
||||
18
Question13-Inheritance/common/Hero.php
Normal file
18
Question13-Inheritance/common/Hero.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace inheritance\Common;
|
||||
|
||||
require_once __DIR__ . '/Character.php';
|
||||
|
||||
class Hero extends Character
|
||||
{
|
||||
|
||||
/**
|
||||
* Hero クラスのコンストラクタ
|
||||
* Character のコンストラクタを利用して初期値(HP:25, POWER:10, ENDURANCE:7)を設定
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(25, 10, 7);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user