OOP (Object-Oriented Programming) 的概念
2010 年8月15日
1. 变量
变量 (Variable) 是程序语言中的第一个重要概念。可以把变量想象为一个可以装东西
的盒子,里面能够装各种东西。 变量分为不同的类别 (Type),如整数,浮点小数,字符,
等等。 变量进一步扩展成为 Array,如 a(1), a(2), a(3),......, 以及 a(1,1),
a(1,2), a(1,3), ..., a(2,1), a(2,2), a(2,3),...... 等等。
2. 子程序(Subroutine) 及 函数 (Function)
有时,一段程序会经常反复地使用,于是引入子程序,使得整个程序大为简便。
函数的概念与子程序相似,只不过要返回 (Return)一个结果。到了 C 语言,
把子程序与函数合并成为广义的函数,就更为简洁。
3. Structure
在简单的程序语言中,如 BASIC,每个变量之间是独立的,即使在意义上是有关联的,
但是在结构上,变量之间并没有什么有机的联系。这样,当程序变得十分庞大复杂时,
程序的维护和纠错就变得很困难。到了 C 语言,引入了一个新的用户自定义的变量类型,
Structure。 Structure 把相互之间具有有机联系的一些变量整合成为一个 Structure。
如,
struct addr { // struc is the keyword, it defines a structure type.
char name [30]; // addr is the structure name;
char street [60];
char city [20]; // city is one of the structure elements.
char state [2];
unsigned long int zip;
} addr_info; // addr_info is the structure variable.
在程序中要 reference Structure 中的某一具体 element 的时候,
使用Structure 变量名,后加一个 “.” 在加 Structure Element 的名字即可,
如
addr_info.zip = 12345;
4. Object 的概念
随着实用程序变得越来越复杂繁琐,为便于纠错和维护,在 C++ 中进一步引入了 Object
的概念。这是 Structure 的进一步发展。 Object 不仅包括了相互之间有关联的变量
(在OOP中称为 Property),而且进一步包括了有联系的函数(在OOP中成为 Method)。
C++ 中的 Object 的概念就是从实际生活中引入的。我们的大千世界是由一个一个 Object
构成的。每个 Object 都可以看作包括两方面的内容。第一方面的内容是它的性质 (Property)
比如 汽车这个 Object,它的性质有,品牌,厂商,颜色,重量,马力,价格。。。等等,
这些内容相当于变量。 而第二方面的内容与它的行为和动作有关, 如转弯,刹车,加速,后退,
鸣笛。。。等等。 这在 OOP 中被称为 Method,相当于前述的函数。这样,Object就把与之
有联系的变量 (Variable - Property)和函数 (Function - Method)都整合到一起了。
5. Class
在 C++, Java,以及 PHP (5.0)这些 OOP 程序语言中, 一个Object 所包括的 Property
和 Method 是首先在 Class 中定义的。Class 可以被看作是 Create Object 的蓝图或者
Template。如,
<?php
class employee {
private $name;
private $rate;
var $hours; // it cannot be without a scope, but "var" is OK.
public function set_rate($rate) { // The $rate is of "private"
scope, it can be set from
// outside the class only using the public
$this->rate = $rate; // function "set_rate($rate)"
}
public function get_rate() {
$rate = $this->rate;
echo "The rate of $this->name is $rate. <br />";
}
public function payment( ) { // the scope cannot be "private".
$wage = $this->rate * $this->hours;
echo "Payment for $this->name is $wage. <br />";
}
}
上述的 Class employee 定义了三个 property, $name, $rate, 和 $hours。
同时,还定义了三个 method, 即 function set_rate($rate), function get_rate(),
和 function payment( )。注意,如果某个property 的 scope 是 private 的话,
那么只能通过包括它的 public function 才能 access 它。
注意,$this 是一个特殊的变量,也可以视为一个 keyword。它代表由该 class
所产生的任何一个 object。对object 的某一个property 的 access 是通过 object
的 name 之后加一个 "->",再加property 的 name 实现的。例如, $this->rate。在这里,
"->" 类似于 C++ 和 JavaScript 中的 "."。
同样的,对object 的某一个method 的 access 也是通过 object 的name 之后加一个
"->",再加method 的 name 实现的。显然,$this 只能出现在class 的定义之内,而不能
出现在 class 的定义之外。
6. Object 的生成 (instantiation)
当object 的 property 和 method 经由 class 定义之后,某个具体的object 就可以使用
$object_name = new class_name( );
生成。一旦一个object经由上述语句生成,这个object就具备了定义它的class的全部属性,
包括它的全部property和method。
Object 生成之后,就可以对其进行种种操作。例如,
$mary = new employee();
$mary->name = "Mary";
$mary->rate = 56;
$mary->hours = 80;
$mary->payment();
7. Constructor 和 Destructor
使用constructor可以很方便地对object 的property进行赋值。例如,
<?php
class employee {
protected $name;
protected $rate;
protected $hours;
public function __construct($name, $rate, $hours) {
$this->set_name($name);
$this->set_rate($rate);
$this->set_hours($hours);
$this->get_rate();
$this->payment($rate, $hours);
}
public function payment($rate, $hours) {
$wage = $this->rate * $this->hours;
echo "Payment for $this->name is $wage. ";
echo "<p></p>";
}
function __destruct() {
echo "<p>employee class instance destroyed.</p>";
}
}
$mary = new employee("Mary", 56, 80);
?>