Commit fcca232c authored by 君塚 悠's avatar 君塚 悠

プログラム作成(Triangle.class.php,cal.php:オブジェクト指向の学習)

parent d6cb5a4d
<?php
class Triangle
{
private $width;
private $height;
//コンストラクタ…クラスがインスタンス化されるときに呼び出される
public function __construct() {
$this -> width = 1;
$this -> heigh = 1;
}
//デストラクタ…破棄されるときに呼び出される
public function __destruct(){ /*デストラクタ*/ }
public function get_width() {return $this -> width;}
public function get_height(){return $this -> height;}
public function set_width($width) {
if (i_numeric($width)) {
$this->width=$width;
}
}
public function set_height($height)
{
if (is_numeric($height)) {
$this->height=$height;
}
}
public function get_area()
{
return $this->get_width() * $this->get_height() / 2;
}
// static…静的メソッド。インスタンス化が必要ない。
// 固有の値をつかわない場合は、インスタンスの意味が無いため、静的にする。
public static function calculate_area($width, $height) {
return $width * $height /2;
}
}
?>
\ No newline at end of file
<html>
<head>
<title>オブジェクト指向入門</title>
</head>
<body>
<h1 style = "background:#cccccc">オブジェクト入門</h1>
<?php
// autoload関数の定義
function __autoload($name)
{
require_once($name.".class.php");
}
// Triangleクラスのインスタンス化
$objCls = new Triangle();
$objCls -> set_width(10);
$objCls -> set_height(20);
print("三角形の面積:".$objCls -> get_area()."<br>");
print("三角形の面積:".Triangle::calculate_area(5,15));
?>
</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