zoukankan      html  css  js  c++  java
  • 使用self关键字调用类体中的静态成员

    <!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>
    <h3>对象和属性</h3>
    <?php
    class car
    {
    public $color;
    public $size;
    
    public function set1($color,$size)
    {
    $this->color=$color;
    $this->size=$size;
    	}
    	public function print1()
    	{
    echo "颜色:".$this->color;
    echo "大小:".$this->size;
    		}
    	}
    	$bus=new car();
    	$bus->set1("blue",30);
    	$bus->print1();
    ?>
    <h3>使用构造函数</h3>
    <?php
    class car1
    {
    public $color;
    public $size;
    
    public function car1($color,$size)
    {
    	$this->color=$color;
    	$this->size=$size;
    	}
    	
    	public function printArrbute()
    	{
           echo "颜色:".$this->color;
    	   echo "大小:".$this->size;
    		}
    	}
    	$bus=new car1("black",20);
    	$bus->printArrbute();
    ?>
    <h3>类的继承和重载.extends关键字和final关键字</h3>
    <?php
          class father
             {
         public function printNUM()
             {
    	  printf("%d",10);	
    	     }
        }
    	class child extends father
    	    {  
        public function printNUM()
            {
         printf("%d",10*10);
         }
     }
     $father=new father();
     echo $father->printNUM();
     echo "	";
     $child=new child();
     echo $child->printNUM();
    ?>
    <h3>接口。接口的主要思想是指定一个实现了该接口的类必须实现的一系列方法。interface声明接口</h3>
    <?php
    interface test
    {
    function fun();
    	}
    	class work implements test
    	{
    function fun()
    {
    echo "快跑!";
    	}
    		}
    		
    		$work=new work();
    		$work->fun();
    ?>
    <h3>常量的使用</h3>
    <?php
    
       const str="你好";
    	echo str;
    ?>
    <h3>对象的克隆</h3>
    <?php
    echo "用Clone关键字所克隆的对象只与原对象具有相同的特征,但二者并不代表同一对象,而是相互独立的","<br>";
    class person
    {
    public function fun()
    {
    echo "快跑!";
    	}
    	}
    	
    	$a=new person();
    	$b=clone $a;
    	$a->fun();
    	
    ?>
    <h3>_autoload()方法</h>
    <?php
    /*include_once 语句在脚本执行期间包含并运行指定文件。此行为和 include 语句类似,唯一区别是如果该文件中已经被包含过,则不会再次包含。如同此语句名字暗示的那样,只会包含一次。
    function _autoload($name)
    {
    include_once $name;
    	}
    	_autoload("mysql.php");
    	*/
    ?>
    <h3>使用instanceof关键字判断实例类型<h3/>
    <?php
    class ball
    {}
    $basketball=new ball();
    if($basketball instanceof ball )
    {
    echo "yes,可以断定basketball是类的实例";
    	}
    	else
    	{
    echo "NO";
    		}
    ?>
    <h3>使用this关键字调用类成员</h3>
    <?php
    class china
    {
     private $x;
     private $y;
     
     public function china($x,$y)
         {
         $this->x=$x;
         $this->y=$y;
    	 }
    	 
     public function fangfa()
     {
       echo  "数字:".$this->x;
       echo  "数字:".$this->y;
    	 }
     }
    	$person1=new china(10,99);
    	echo "<br>";
    	$person1->fangfa()
    ?>
    <h3>self调用静态成员</h3>
    <?php
    echo "elf是指向类本身,也就是PHP self关键字是不指向任何已经实例化的对象,一般self使用来指向类中的静态变量。","<br>"; 
    class Counter  
    {  
    //定义属性,包括一个静态变量  
    private static $firstCount = 0;  
    private $lastCount;  
    //构造函数  
    function __construct()  
    {  
    $this->lastCount = ++self::$firstCount;
     //使用PHP self关键字来调用静态变量,使用self调用必须使用::(域运算符号) 
    }  
    //打印最次数值  
    function printLastCount()  
    {  
    print( $this->lastCount );  
    }   
    }  
    //实例化对象  
    $countObject = new Counter();  
    $countObject->printLastCount();
     //输出 1  
    ?> 
    
    
    </body>
    </html>
    

      

  • 相关阅读:
    C#3.0实现变异赋值(Mutantic Assignment)
    博客园积分算法探讨
    C#动静结合编程之二: 两种哲学
    REST构架风格介绍之二:CRUD
    C# vs C++之一:委托 vs 函数指针
    REST构架风格介绍之一:状态表述转移
    C#动静结合编程之三:Duck Typing
    C#动静结合编程之四:泛型委托
    C# vs C++之二:GC vs RAII
    Ecshop文章分类列表页如何自定义Title以提高SEO效果
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4800509.html
Copyright © 2011-2022 走看看