Ошибка Uncaught: вызов неопределенного метода Vehicles :: setPassengerSeats () - PullRequest
0 голосов
/ 16 ноября 2018

Я получаю эту ошибку:

необработанный вызов ошибки для неопределенного метода Vehicles :: setPassengerSeats () в C: \ xampp \ htdocs \ practice \ vehicle.php: 91.

Я также добавил скриншот ошибки.Пожалуйста, проверьте и скажите мне, как я могу решить это?Я думаю, что у меня проблемы с моим подклассом, но я не знаю где?

Вот мой исходный код:

<?php  

class Vehicles{
    private $noOfVehicles;
    private $color;
    private $fuel;
    private $speed;

    public function getNoOfVehicles(){
        return $this->noOfMobiles;
    }

    public function setNoOfVehicles($Vehicles){
        $this->noOfMobiles = $Vehicles;
        echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
    }

    public function getColor(){
        return $this->color;
    }

    public function setColor($look){
        $this->color = $look;
        echo "</br>The Color of Vehicle is: ".$this->color."</br>";
    }

    public function getFuel(){
        return $this->fuel;
    }

    public function setFuel($petrol){
        $this->fuel = $petrol;
        echo "</br>The fuel is: ".$this->color."</br>";
    }

    public function getSpeed(){
        return $this->speed;
    }

    public function setSpeed($vehicleSpeed){
        $this->speed = $vehicleSpeed;
        echo "</br>The speed of vehicle is: ".$this->speed."</br>";
    }

}

class PassengerVehicles extends Vehicles{
    private $passengerSeats;

    public function getPassengerSeats(){
        return $this->passengerSeats;
    }

    public function setPassengerSeats($seats){
        return $this->passengerSeats = $seats;
        echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
    }

}

class TransportationVehicles extends Vehicles{
    private $noOfDoors;
    private $loadCapacity;

    public function getNoOfDoors(){
        return $this->noOfDoors;
    }

    public function setNoOfDoors($doors){
        return $this->noOfDoors = $doors;
        echo "</br>The No of Doors are: ".$this->noOfDoors."</br>";
    }

    public function getLoadCapacity(){
        return $this->loadCapacity;
    }

    public function setLoadCapacity($capacity){
        return $this->loadCapacity = $capacity;
        echo "The Load Capacity is: ".$this->loadCapacity."</br>";
    }
}

$VehiclesObj = new Vehicles;
$VehiclesObj->setNoOfVehicles("15");
$VehiclesObj->setColor("Black");
$VehiclesObj->setFuel("5 Litre");
$VehiclesObj->setSpeed("120 km/h");

$VehiclesObj->setPassengerSeats("4");
$VehiclesObj->setNoOfDoors("4");
$VehiclesObj->setLoadCapacity("500 KG");

?>

enter image description here

Ответы [ 2 ]

0 голосов
/ 16 ноября 2018

Вы вызываете метод setPassengerSeats, который находится в другом классе, а не в Vehicles Сначала вы должны создать экземпляр, а затем вызвать этот метод:

$passangerVehicle = new PassengerVehicles;
$passangerVehicle->setPassengerSeats("4");
0 голосов
/ 16 ноября 2018

Вы не можете вызывать дочерние методы от родителя.Вам нужно создать экземпляр дочернего объекта, чтобы иметь возможность вызывать родительские методы

   class Vehicles{
    private $noOfVehicles;
    private $color;
    private $fuel;
    private $speed;

    public function getNoOfVehicles(){
        return $this->noOfMobiles;
    }

    public function setNoOfVehicles($Vehicles){
        $this->noOfMobiles = $Vehicles;
        echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
    }

    public function getColor(){
        return $this->color;
    }

    public function setColor($look){
        $this->color = $look;
        echo "</br>The Color of Vehicle is: ".$this->color."</br>";
    }

    public function getFuel(){
        return $this->fuel;
    }

    public function setFuel($petrol){
        $this->fuel = $petrol;
        echo "</br>The fuel is: ".$this->color."</br>";
    }

    public function getSpeed(){
        return $this->speed;
    }

    public function setSpeed($vehicleSpeed){
        $this->speed = $vehicleSpeed;
        echo "</br>The speed of vehicle is: ".$this->speed."</br>";
    }

}

class PassengerVehicles extends Vehicles{
    private $passengerSeats;

    public function getPassengerSeats(){
        return $this->passengerSeats;
    }

    public function setPassengerSeats($seats){
        return $this->passengerSeats = $seats;
        echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
    }

}

class TransportationVehicles extends Vehicles{
    private $noOfDoors;
    private $loadCapacity;

    public function getNoOfDoors(){
        return $this->noOfDoors;
    }

    public function setNoOfDoors($doors){
        $this->noOfDoors = $doors;
        echo "</br>The No of Doors are: {$this->noOfDoors}</br>";

        return $this->noOfDoors;
    }

    public function getLoadCapacity(){
        return $this->loadCapacity;
    }

    public function setLoadCapacity($capacity){
        return $this->loadCapacity = $capacity;
        echo "The Load Capacity is: ".$this->loadCapacity."</br>";
    }
}

$truck = new TransportationVehicles();
$truck->setNoOfVehicles("15");
$truck->setColor("Black");
$truck->setFuel("5 Litre");
$truck->setSpeed("120 km/h");
$truck->setNoOfDoors("4");
$truck->setLoadCapacity("500 KG");

$taxi = (new PassengerVehicles())->setPassengerSeats('4');

. В этом случае у вас будет два экземпляра класса Vehicles + собственный дочерний элемент.

ПервыйЭкземпляр связан с самим транспортным средством + транспортные свойства, такие как $noOfDoors и $loadCapacity - например, грузовик.Вторым примером является пассажирское транспортное средство - например, такси.

И вы пытались получить пассажирский вариант такси из автобуса.

...