TEL:400-8793-956
当前位置:程序、服务器

OOP PHP基础

提问者: 近期获赞: 浏览人数: 发布时间:2021-02-17 10:57:55

 问:OOP PHP基础

<?php
公共函数__construct ($ name ,$ flavor ,$ record ,$ species ){父:: __construct ($ name ,$ flavor ,$ record ); $ this- >种= $种; }公共函数getInfo (){ $ this- > species 。“” $ this- > common_name 。“品尝” $ this- >风味。
 
      “。记录”  
$ this- >种。“” $ this- > common_name 。“权衡” $ this- > record_weight ; } } $ brook_trout =新鳟鱼(“鳟鱼” ,“美味” ,“ 14磅8盎司” ,“布鲁克” );?>  
 
需要通过公共功能getinfo返回“布鲁克鳟鱼味道鲜美。创纪录的布鲁克鳟鱼重14磅8盎司。”
 
我究竟做错了什么...?    
    
 
答:首先,您没有定义attribute public $species;。
 
其次,您没有在任何地方定义类。如果我没记错的话,您需要在Fish课后加上class Trout extends Fish;。
 
第三,你public function getInfo()有各种各样的问题。您实际上不需要对方法(类中的函数)执行任何操作,而必须返回它。我已经成功完成了以下工作:
 
<?php 
//在此处填写适当的代码
public function getInfo (){ return “ $ this-> common_name $ this-> species是一种$ this->风味品尝鱼。记录重量为$ this-> record 。” ; }
   
使用双引号允许变量在双引号内进行解析。
 
您可以执行类似建议的操作,但是必须将其分配给变量,然后返回该变量。
上一篇: 我不知道如何将getter方法合并到此类中
下一篇: 刷新时看不到边界