如何为对象创建默认值? PHP

时间:2015-06-27 22:00:21

标签: php class object inheritance default

让我们说我有一个Human类,它具有$ gender的变量,并没有赋值给它。人类的构造函数具有年龄,性别,身高和体重等参数。

我有另一个名为Female的类,它继承自Human,但现在,Female类使用Female字符串覆盖$ gender变量。

当我创建对象时,请说$f = new Female(12, 'female', 123, 40); 如何在创建对象时跳过键入女性?

我认为我们需要在Female类中创建另一个新的构造函数,并且在Female类构造函数的参数中我有age, gender = 'female', height and weight但这似乎不起作用。

我尝试在创建对象时将性别部分留空,或尝试输入""之类的空字符串。

有人可以帮我一把吗?非常感谢。

我的人类代码

class Human {
    protected $age = 0;   
    protected $gender;
    protected $height_in_cm;
    protected $weight_in_kg;

    function __construct($age, $gender, $heightCM, $weightKG)
    {
        $this->age = $age;
        $this->gender = $gender;
        $this->height_in_cm = $heightCM;
        $this->weight_in_kg = $weightKG;
    }

    /**
     * @return int
     */
    public function getAge()
    {
        return $this->age;
    }

    /**
     * @return string
     */
    public function getGender()
    {
        return $this->gender;
    }
}

女性课程代码

require_once('Human.php');
class Female extends Human{
    protected $gender = 'female';

    function __construct($age, $gender = 'female', $heightCM, $weightKG)
    {
        $this->age = $age;
        $this->gender = $gender;
        $this->height_in_cm = $heightCM;
        $this->weight_in_kg = $weightKG;
    }
}

$f = new Female(12,'female',123,40);
echo "Your gender is ". $f->getGender()."<br>";

4 个答案:

答案 0 :(得分:3)

您只需在扩展类中设置值:

// If it should be possible to instantiate the Human
// class then remove the "abstract" thing at set the
// "gender" property in the constructer
abstract class Human {
    protected $gender;
    protected $age;
    public function __construct($age) {
        $this->age = $age;
    }
}
class Female extends Human {
    protected $gender = "Female";
}
class Male extends Human {
    protected $gender = "Male";
}

虽然这有效,但实际上并没有多大意义。课程本身会告诉您人类的性别,因此您只需拨打$human instanceof Female

$person = new Female(18);
if ($person instanceof Female) {
    echo "Person is female";
}

答案 1 :(得分:3)

只需覆盖构造函数:

class Human {
     public function __construct($age, $gender, $height, $weight) {
     }
}

class Female extends Human {
    public function __construct($age, $height, $weight) {
         parent::__construct($age, 'Female', $height, $weight);
    }
}

答案 2 :(得分:2)

您可以简单地覆盖构造函数:

abstract class Human
{
    protected $age;

    protected $gender;

    protected $height;

    protected $weight;

    public function __construct($age, $gender, $height, $weight)
    {
        $this->age = $age;
        $this->gender = $gender;
        $this->height = $height;
        $this->weight = $weight;
    }

    public function getGender()
    {
        return $this->gender;
    }
}

class Female extends Human
{
    public function __construct($age, $height, $weight)
    {
        parent::__construct($age, 'female', $height, $weight);
    }
}

$female = new Female(12, 170, 60);
echo $female->getGender();

答案 3 :(得分:1)

我认为,在你的构造函数

__construct($var, $gender="female", $var, $var){
//rest assignment
}

应该这样做

或者,使用已设置female的3个param构造函数