Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

allClass

<?php
//Clase Instanciable
class Telefono {
  public $marca;
  public $modelo;
  protected $medio = 'alámbrico';
  protected $transmision = 'análoga';

  function __construct($marca, $modelo) {
    $this->marca = $marca;
    $this->modelo = $modelo;
  }

  public function llamar() {
    echo '<p>Riiing Riiing!!!</p>';
  }

  public function ver_info() {
    echo "
      <ul>
        <li>$this->marca</li>
        <li>$this->modelo</li>
        <li>$this->medio</li>
        <li>$this->transmision</li>
      </ul>
    ";
  }
}

//Clase Heredada
class Celular extends Telefono {
  protected $medio = 'inalámbrico';
  protected $transmision = 'digital';

  function __construct($marca, $modelo) {
    parent::__construct($marca, $modelo);
  }

  public function vibrar() {
    echo '<p>BRRRR BRRRR!!!</p>';
  }
}

//Clase Final
final class SmartPhone extends Celular {
  public $datos = 'Tengo WIFI';

  function __construct($marca, $modelo) {
    parent::__construct($marca, $modelo);
  }

  public function conectar() {
    echo 'Acceso a Internet!!!';
  }

  public function ver_info() {
    echo "
      <ul>
        <li>$this->marca</li>
        <li>$this->modelo</li>
        <li>$this->medio</li>
        <li>$this->transmision</li>
        <li>$this->datos</li>
      </ul>
    ";
  }
}

/* ********************************************* */

echo '<h1>Herencia y Polimorfismo en PHP</h1>';

echo '<h2>Evolución del Teléfono</h2>';

echo '<h3>Teléfono:</h3>';
$tel_casa = new Telefono('Panasonic', 'KX-TS550');
$tel_casa->llamar();
$tel_casa->ver_info();

echo '<h3>Celular:</h3>';
$mi_cel = new Celular('Nokia', '5120');
$mi_cel->llamar();
$mi_cel->vibrar();
$mi_cel->ver_info();

echo '<h3>SmartPhone:</h3>';
$mi_sp = new SmartPhone('Motorola', 'G4');
$mi_sp->llamar();
$mi_sp->vibrar();
$mi_sp->conectar();
$mi_sp->ver_info();

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.