Commit 5180c9a5 authored by 君塚 悠's avatar 君塚 悠

プログラム作成(call.php,Figure.class.php,Square.class.php,Triangle.class.php:継承の学習)

parent fcca232c
...@@ -21,7 +21,7 @@ $param = array( ...@@ -21,7 +21,7 @@ $param = array(
$auth = new Auth("DB", $passwd, "MyLogin", TRUE); $auth = new Auth("DB", $passwd, "MyLogin", TRUE);
$Auth -> start(); $Auth -> start();
if ($auth -> getUsername() == "") { if ($auth -> getUsername() == "") {
//未承認→強制終了 //未承認→強制終了する
exit(1); exit(1);
} }
?> ?>
\ No newline at end of file
...@@ -12,7 +12,7 @@ function __autoload($name) ...@@ -12,7 +12,7 @@ function __autoload($name)
require_once($name.".class.php"); require_once($name.".class.php");
} }
// Triangleクラスのインスタンス化 // Triangleクラス(Triangle.class.phpに有る)のインスタンス化
$objCls = new Triangle(); $objCls = new Triangle();
$objCls -> set_width(10); $objCls -> set_width(10);
$objCls -> set_height(20); $objCls -> set_height(20);
......
<?php
abstract class Figure
{
private $width;
private $height;
public function __construct(){
$this -> width = 1;
$this -> height = 1;
}
public function __destruct()
{
/*デストラクタ*/
}
public final function get_width()
{
return $this -> width;
}
public final function get_height()
{
return $this -> height;
}
public final function set_width()
{
if (is_numeric($width)) {
$this -> height = $height;
}
}
public final function set_height()
{
if (is_numeric($height)) {
$this -> height = $height;
}
}
//抽象メソッド
public abstract function get_area();
}
?>
<?php
class Square extends Figure
{
public function __construct()
{
parent::__construct();
}
public function get_area()
{
return $this -> get_width() * $this -> get_height();
}
}
\ No newline at end of file
<?php
class Triangle extends Figure
{
public function __construct()
{
parent::__construct();
}
public function get_area()
{
return $this -> get_width() * $this -> get_height / 2;
}
}
<html>
<head>
<title>オブジェクト指向入門(ポリモーフィズム)</title>
</head>
<body>
<h1 style = "background:#cccccc">オブジェクト入門(ポリモーフィズム</h1>
<?php
// autoload関数の定義
function __autoload($name)
{
require_once($name.".class.php");
}
// Triangleクラス(Triangle.class.phpに有る)のインスタンス化
$objTri = new Triangle();
$objTri -> set_width(10);
$objTri -> set_height(20);
print("三角形の面積:".$objTri -> get_area()."<br>");
$objSqr = new Square();
$objSqr -> set_width(10);
$objSqr -> set_height(20);
print("四角形の面積:".$objSqr -> get_area()."<br>");
?>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment