gpt4 book ai didi

PHP 类设计问题

转载 作者:行者123 更新时间:2023-12-04 06:49:25 25 4
gpt4 key购买 nike

我正在尝试编写类以在我的技能测试中展示一些人。问题之一如下:

展示 child 如何调用 parent 的方法。展示重载的工作原理。指示类变量的语法。

我想我已经完成了大部分问题,但不确定是什么 指示类变量的语法 mean ……谁能帮我解释一下?非常感谢!

Class Phone{

function __construct(){
echo "This is the constructor";
}

protected function feature(){
echo "Communication <br>";
}
}

Class CellPhone extends CordlessPhone {
private $brand;
function __construct(){
echo "<p style='color:red;'>Cell phone feature:</p>";
}

function __set($b, $value){
if($value=="Apple"){
$this->brand=$value." is awesome!";
}elseif($value=="LG"){
$this->brand=$value." is nice";
}else{
$this->brand="We only accept Apple and LG";
}
}

function __get($brand){
return "The brand: ".$this->brand."------";
}

public function feature(){
parent::feature();
echo "Play music<br>";
echo "Surf internet<br>";
}
}

Class CordlessPhone extends Phone {
function __construt(){
echo "<p style='color:red;'>Cordless phone feature:</p>";
}
public function feature(){
parent::feature();
echo "Cordless<br>";
echo "Use Battery<br>";
}
}

$phone=new CellPhone();
$phone->feature();
$phone->brand="LG";
echo $phone->brand;

最佳答案

我认为它们的意思是类变量作为该类的属性。

class A {
private $b = TRUE; // This is the syntax for setting visibility, name and default value

public function __construct() {
echo $this->b; // this is how to access from within a method
}

}

关于PHP 类设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3339942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com