求帮忙?php oop例子

WBOY
Libérer: 2016-06-23 14:23:48
original
1144 Les gens l'ont consulté

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;/**另外:以后发代码,格式化了吧*/
Copier après la connexion

前一行是全角的分号?

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

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

<?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;?>
Copier après la connexion

<?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();?>
Copier après la connexion

坐等高手!!

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

为什么会报错,
是因为你第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;
Copier après la connexion

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

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;
Copier après la connexion

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

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!