sábado, 5 de abril de 2025

Class interface

 // Defining the interface

interface BicycleInterface {

    // Method declarations (no implementations)

    public function changeCadence($newValue);

    public function changeGear($newValue);

    public function speedUp($increment);

    public function applyBrakes($decrement);

    public function printStates();

}






// Implementing the BicycleInterface in a class

class Bicycle implements BicycleInterface {

    private $cadence = 0;

    private $speed = 0;

    private $gear = 1;


    // Implementing methods from BicycleInterface

    public function changeCadence($newValue) {

        $this->cadence = $newValue;

    }


    public function changeGear($newValue) {

        $this->gear = $newValue;

    }


    public function speedUp($increment) {

        $this->speed += $increment;

    }


    public function applyBrakes($decrement) {

        $this->speed -= $decrement;

    }


    public function printStates() {

        echo "Cadence: " . $this->cadence . ", Speed: " . $this->speed . ", Gear: " . $this->gear . "\n";

    }


}


// Example usage

$bicycle = new Bicycle();

$bicycle->changeCadence(50);

$bicycle->changeGear(3);

$bicycle->speedUp(20);

$bicycle->applyBrakes(5);

$bicycle->printStates();  // Outputs: Cadence: 50, Speed: 15, Gear: 3

No hay comentarios:

Publicar un comentario