求帮忙?php oop例子

WBOY
Release: 2016-06-23 14:23:48
Original
1188 people have browsed it

PHP

class animal{
public $name="";
public $color="";
public $age="";
function getInfo(){return $this->name;}
function setInfo($name){return $this->name;}//编译器提示警告
}
$pig=new animal();
$pig->setInfo('猪');
$name=$pig->getInfo();//编译器提示错误
echo $name;


?>

回复讨论(解决方案)

求帮忙!!帮我看看那里出错了!

$pig=new animal();$pig->setInfo('猪');//是这行错了,你的分号为全角的分号。$name=$pig->getInfo();//编译器提示错误echo $name;/**另外:以后发代码,格式化了吧*/
Copy after login

前一行是全角的分号?

谢谢你!已经改过了!但是没有结果的?

不是应该输出一个"猪"字吗?

<?php class animal{	public $name="";	public $color="";	public $age="";	function getInfo(){return $this->name;}	function setInfo($name){return $this->name;}//编译器提示警告}$pig=new animal();$pig->setInfo('猪');$name=$pig->getInfo();//编译器提示错误echo $name;?>
Copy after login

<?php class person{    private $name;        function setname($name){        return $this->name = $name;    }        function getname(){        return $this->name;    }}$p = new person();$p->setname('lizhi');echo $p->getname();?>
Copy after login

坐等高手!!

你的代码我调式过了是可以的,但是我的为什么不行,能帮忙找出原因吗?

为什么会报错,
是因为你第10行有个大写分号,

为什么没输出“猪”
试试
function setInfo($name){return $this->name;}//编译器提示警告
改成
function setInfo($name){$this->name = $name;}//编译器提示警


你的setInfo函数并没有给$name属性赋值,而是直接返回了$this->name
function setInfo($name){$this->name = $name;}

你的代码我调式过了是可以的,但是我的为什么不行,能帮忙找出原因吗?
function setInfo($name){return $this->name;}
这里没有任何赋值操作,所以仍然为原值(空)

class animal{  public $name="";  public $color="";  public $age="";  function getInfo(){    return $this->name;  }  function setInfo($name){    $this->name = $name; //这里应该是赋值,想必你复制错了  }}$pig=new animal();$pig->setInfo('猪'); //这里原来是全角的;$name=$pig->getInfo();echo $name;
Copy after login

你的代码我调式过了是可以的,但是我的为什么不行,能帮忙找出原因吗?

class animal{private $name;function setInfo($name){    $this->name = $name;}function getInfo(){    return $this->name;}}$pig=new animal();$pig->setInfo('lizhi');$name=$pig->getInfo();//编译器提示错误echo $name;
Copy after login

应该是setInfo()这里没有赋值!谢谢你们!!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template