返回垃圾输出。我是PHP的新手。请帮我解决这个问题。我不想永远陷入困境。我是面向对象编程的新手,所以我无法弄清楚出了什么问题以及在哪里!
<?php
Abstract class Employee {
protected $Fname;
protected $Lname;
protected $dept;
static protected $total= '0';
public abstract function getSalary();
function __construct($f,$l,$d) {
$this->Fname = $f;
$this->Lname = $l;
$this->dept = $d;
self::$total++;
}
public function getFullname () {
return $this->Fname." ".$this->Lname;
}
public function getDept() {
return $this->dept;
}
public static function getTotal(){
return self::$total;
}
}
class FullTime extends Employee {
protected $Annualsalary;
function __construct($s) {
$this->Annualsalary = $s;
}
public function getSalary() {
return $this->Annualsalary / 12;
}
}
class Contract extends Employee {
protected $Monthlypay;
function __construct ($s)
{
$this->Monthlypay = $s;
}
public function getSalary() {
return $this->Monthlypay;
}
}
$emp1 = new FullTime("John",'Doe','IT',150000);
$emp2 = new FullTime('J','Doe','sales',130000);
$emp3 = new FullTime('John','D','sales',140000);
$emp4= new Contract('JOHAN','Doe','sales',14000);
echo "Total Employees = ".Employee::getTotal()."<br>";
echo $emp1->getFullname()." | ".$emp1->getDept()." | ".$emp1->getSalary()."<br>";
echo $emp2->getFullname()." | ".$emp2->getDept()." | ".$emp2->getSalary()."<br>";
echo $emp3->getFullname()." | ".$emp3->getDept()." | ".$emp3->getSalary()."<br>";
echo $emp4->getFullname()." | ".$emp4->getDept()." | ".$emp4->getSalary();
?>
输出
员工总数= 0
| | 0
| | 0
| | 0
| | JOHAN
答案 0 :(得分:2)
你很亲密。您需要初始化每个类属性,PHP不会自动执行此操作。只需调用父类中的构造函数。此外,您需要将所有参数传递给构造函数。
看看这个:
<?php
Abstract class Employee {
protected $Fname;
protected $Lname;
protected $dept;
static protected $total = 0;
public abstract function getSalary();
function __construct($f, $l, $d) {
$this->Fname = $f;
$this->Lname = $l;
$this->dept = $d;
self::$total++;
}
public function getFullname() {
return $this->Fname . " " . $this->Lname;
}
public function getDept() {
return $this->dept;
}
public static function getTotal() {
return self::$total;
}
}
class FullTime extends Employee {
protected $Annualsalary;
function __construct($f, $l, $d, $s) {
parent::__construct($f, $l, $d);
$this->Annualsalary = $s;
}
public function getSalary() {
return $this->Annualsalary / 12;
}
}
class Contract extends Employee {
protected $Monthlypay;
function __construct($f, $l, $d, $s) {
parent::__construct($f, $l, $d);
$this->Monthlypay = $s;
}
public function getSalary() {
return $this->Monthlypay;
}
}
$emp1 = new FullTime("John", 'Doe', 'IT', 150000);
$emp2 = new FullTime('J', 'Doe', 'sales', 130000);
$emp3 = new FullTime('John', 'D', 'sales', 140000);
$emp4 = new Contract('JOHAN', 'Doe', 'sales', 14000);
echo "Total Employees = " . Employee::getTotal() . "<br>";
echo $emp1->getFullname() . " | " . $emp1->getDept() . " | " . $emp1->getSalary() . "<br>";
echo $emp2->getFullname() . " | " . $emp2->getDept() . " | " . $emp2->getSalary() . "<br>";
echo $emp3->getFullname() . " | " . $emp3->getDept() . " | " . $emp3->getSalary() . "<br>";
echo $emp4->getFullname() . " | " . $emp4->getDept() . " | " . $emp4->getSalary();
?>
答案 1 :(得分:0)
__构造函数在FullTime类中被覆盖,但不调用父构造函数
function __construct($f,$l,$d, $s) {
parent::__construct($f,$l,$d);
$this->Annualsalary = $s;
}