面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。
类,属性和方法,类,对象,面向对象编程的三大特点。特点,封装性,继承性,多态性。
封装性,也可以称为信息隐藏。
继承性就是派生类(子类)自动继承一个或多个基类(父类)中的属性与方法,并可以重写或添加新的属性或方法。
多态性是指同一个类的不同对象,使用同一个方法可以获得不同的结果,这种技术称为多态性。
类的定义,成员方法,类的实例化,成员变量,类常量,构造方法和析构方法,继承和多态的实现,数据的隐藏,静态变量。
类的定义:
<?php
class SpoObject{ //定义类
//…
}
?>
成员方法:
<?php
class SportObject{
function beatBasketball()($name,$height,$avoirdupois,$age,$sex){ //声明成员方法
echo "姓名:".$name; //方法实现的功能
}
}
?>
类的实例化:
对象名 -> 成员方法
成员变量
类中的变量,也称为成员变量
关键字 成员变量名
对象名 -> 成员变量
类常量
常量就是不会改变的量,是一个恒值。
定义常量使用关键字const
输出格式:
类名::常量名
构造方法和析构方法
void __construct([mixed args [,…]])
void __destruct ( void )
示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>成员变量</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
class SportObject{
public $name; //定义成员变量
public $height; //定义成员变量
public $avoirdupois; //定义成员变量
public function bootFootBall($name,$height,$avoirdupois){ //声明成员方法
$this->name=$name;
$this->height=$height;
$this->avoirdupois=$avoirdupois;
if($this->height<185 and $this->avoirdupois<85){
return $this->name.",符合踢足球的要求!"; //方法实现的功能
}else{
return $this->name.",不符合踢足球的要求!"; //方法实现的功能
}
}
}
$sport=new SportObject(); //实例化类,并传递参数
echo $sport->bootFootBall('1','185','80'); //执行类中的方法
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>成员方法</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
class SportObject{
function beatBasketball($name,$height,$avoirdupois,$age,$sex){ //声明成员方法
if($height>180 and $avoirdupois<=100){
return $name.",符合打篮球的要求!"; //方法实现的功能
}else{
return $name.",不符合打篮球的要求!"; //方法实现的功能
}
}
}
$sport=new SportObject();
echo $sport->beatBasketball('1','185','80','20周岁','男');
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>常量</title>
<style type=&quo