Ex.01,Ex.02 完了

This commit is contained in:
2026-10-21 12:49:39 +09:00
parent 261ff5f68b
commit 9aebbab0ab
2 changed files with 38 additions and 1 deletions

View File

@@ -1,5 +1,4 @@
<?php <?php
/** /**
* [Ex01 継承とnamespaceを使った問題] * [Ex01 継承とnamespaceを使った問題]
* *
@@ -22,3 +21,26 @@
* d. 継承したメソッド run() と breathe() を実行し、結果を出力する。 * d. 継承したメソッド run() と breathe() を実行し、結果を出力する。
* *
*/ */
// require_once を使い "common/Animal.php" を読み込む。
require_once __DIR__ . '/common/Animal.php';
// b
use Common\Animal;
// b Animal~
class Ex01 extends Animal {
}
// "Ex01" のインスタンスを生成し、
$ex01 = new Ex01();
// setName() メソッドで名前に '人' をセットする
$ex01->setName('人');
// 継承したメソッド run() と breathe() を実行し、結果を出力する。
$ex01->run();
$ex01->breathe();

View File

@@ -18,3 +18,18 @@
* 5. その後、showParameter() メソッドを呼び出して、キャラクター情報を出力してください。 * 5. その後、showParameter() メソッドを呼び出して、キャラクター情報を出力してください。
* *
*/ */
// 本ファイル内で、require_once を使い "/common/Hero.php" を読み込んでください。
require_once __DIR__ . '/common/Hero.php';
// namespace を利用して、inheritance\common\Common\Hero クラスを使用
// namespaceが書いてあれば呼び出し側はuseを使う必要がある
use inheritance\Common\Hero;
// Common\Hero クラスのインスタンスを生成し、
$hero = new Hero();
// setName() メソッドを使って名前に "勇者" を設定してください。
$hero->setName('勇者');
// showParameter() メソッドを呼び出して、キャラクター情報を出力
echo $hero->showParameter();