文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>物件导向程式设计

物件导向程式设计

时间:2007-02-17  来源:PHP爱好者

物件导向程式设计 (OOP), 相信对许许多多的程式设计员都不感陌生吧 !
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
记得刚接触OOP时 , 常有同样的一个问题:
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
OOP的写法与平时写程式的方法有什么差别 ? OOP所能达到的功能, 平时用函数的写法不也能达到吗 ? 何必费时费力学呢 ?
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
后来才慢慢发觉, 学OOP不是费时, 而是为了节省往后写程式的时间 :) 以下就是一个简单的例子, 可以看
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
到, 对于"使用者"而言, 只要懂得使用
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc = new math();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->add();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_x (1);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_y(2);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
echo $calc->calculate();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
unset ( $calc );
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
等, 就行了, 不需要了解里面是怎样执行的, 就好象我们看电视, 并不需要了解电视的构造一样.
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
<html>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
<head>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
<title>物件导向程式设计</title>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
</head>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
<body>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
<?
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
/* class 取名为 math
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
当然, 我们也可把这整个class"移"至另一个file, 如math.php,
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
必要时, 用 include("math.php") 即可 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
class math {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
var $int_x; /* x 值 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
var $int_y; /* y 值 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
var $str_access_status; /* 处于什么状态: 加减乘除四种 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function math() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->_initialize(); /* 设置x值及y值的初始值 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
} //-- end math()
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function _initialize() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->int_x = 0; /* 初始值设为0 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->int_y = 0; /* 初始值设为0 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->str_access_status = ""; /* 状态设为null */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
} //-- end _initialize()
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function add() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->str_access_status = "A"; /* A 代表加法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
}
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function subtract() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->str_access_status = "S"; /* S代表减法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
}
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function multiply() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->str_access_status = "M"; /* M代表乘法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
}
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function divide() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$this->str_access_status = "D"; /* D代表除法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
}
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
/* 运算函数, 所有的数字运算全在这里 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function calculate() {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
if ( $this->str_access_status == "A" ) {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
return ($this->int_x + $this->int_y);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
} elseif ( $this->str_access_status == "S" ) {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
return ($this->int_x - $this->int_y);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
} elseif ( $this->str_access_status == "M" ) {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
return ($this->int_x * $this->int_y);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
} elseif ( $this->str_access_status == "D") {
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
return ($this->int_x / $this->int_y);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
}
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
}
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function set_x ( $arg_x ) { $this->int_x = $arg_x; }
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
function set_y ( $arg_y ) { $this->int_y = $arg_y; }
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
} //-- end class math
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
/* 加法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc = new math();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->add();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_x (1);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_y(2);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
echo $calc->calculate();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
unset ( $calc );
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
/* 减法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc = new math();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->subtract();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_x (1);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_y(2);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
echo $calc->calculate();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
unset ( $calc );
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
/* 乘法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc = new math();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->multiply();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_x (1);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_y(2);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
echo $calc->calculate();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
unset ( $calc );
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
/* 除法 */
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc = new math();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->divide();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_x (1);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
$calc->set_y(2);
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
echo $calc->calculate();
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
unset ( $calc );
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
?>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
</body>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
</html>
chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd

chinaitpower.com81FZgJx8BLKkJHnmHWyqXlXVd
当然, 这只是一个非常简单的例子, 但别小看OOP喔. 大家都知道PHP对于每一种DB都有不同的接口吧 ! 使用OOP的方式, 我们可将之统一呢
php爱好者站 http://www.phpfans.net c/vc/c++/java.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载