码迷,mamicode.com
首页 > Web开发 > 详细

php面向对象

时间:2016-01-25 22:56:50      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:

<!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=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
        class Dog                    //定义类
        {
            private $name;                 //定义变量,private封装
            private $age;
            private $pinzhong;
            
            function Jiao()
            {
                echo "{$this->name}在叫,它{$this->age}岁"."<br>";               //this代表该对象,如果要在成员方法中,
            }                                                           //调用成员变量,一要使用$this找出该成员变量
                                                         
            function __construct($n,$a,$p)       //构造函数
            {
                $this->name = $n;
                $this->age =$a;
                $this->pinzhong = $p;
            }
            
            /*function __destruct()               //析构函数,无参数,
            {
                echo "结束";                       //{}方法随意写
            }*/
            
            function __set($name,$value)
            {
                $this->$name = $value;
                
            }
            
            function __get($name)
            {
                return $this->$name;
            }
        }
            
        class HaShiQi extends Dog                     //extends 继承
        {
            const A = 9 ;                            //const 定义一个常量
                    
            public static $height;
            
            function Jiao()
            {
                parent::Jiao();              //类调用方法   类名加双冒号方法或变量::
                
                echo "hello";
            }
            
            static function Gao()
            {
                echo "这么高";
            }
            
        }
             
            $dog = new Dog("旺财",10,"金毛");    
            $dog->name = "小强";
            $dog->age = "6";
            echo $dog->pinzhong."<br>";
            $dog->Jiao();
            
            //final  class  person{}             //final 终类不能被继承
            
            HaShiQi::$height=10;
            echo HaShiQi::$height;
            
            
?>    

           
            
            
</body>
</html>

php面向对象

标签:

原文地址:http://www.cnblogs.com/sihuiming/p/5158869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!