>  기사  >  백엔드 개발  >  PHP类(初学)

PHP类(初学)

WBOY
WBOY원래의
2016-06-23 13:40:12895검색

类示例:Cat.php

<?phpclass Cat  {	private  $name ;	private $age;	private $color;			function __construct() 	{		$numargs  =  func_num_args ();		if($numargs==3)		{			$this->name=func_get_arg(0);			$this->age=func_get_arg(1);			$this->color=func_get_arg(2);		}			}/* 不支持	 * function __construct($name,$age,$color)	{		$this->name=$name;		$this->age=$age;		$this->color=$color;	} */			public function  getName()	{		return $this->name;	}		public function setName($name)	{		return $this->name=$name;	}			public function getAge()	{		return $this->age;	}	public function setAge($age)	{		$this->age=$age;	}		public function getColor()	{		return $this->color;	}	public function setColor($color)	{		$this->color=$color;	}	}?>

对象:

<?phpheader ('Content-Type:text/html;charset=UTF-8');require 'Cat.php';$cat1 = new Cat('蔡倩倩',24,'白的');/*  * $cat1 = new Cat();$cat1->setName('蔡倩倩');$cat1->setAge(24);$cat1->setColor('白的'); */echo 'Cat1的姓名为:'.$cat1->getName().'<br>年龄为:'.$cat1->getAge();?>


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.