Advertisement
yoshoo

Untitled

Aug 25th, 2019
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. <?php
  2.  
  3. class Produk {
  4.     public $judul,
  5.            $penulis,
  6.            $penerbit,
  7.            $harga;
  8.  
  9.     public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0) {
  10.         $this->judul = $judul;
  11.         $this->penulis = $penulis;
  12.         $this->penerbit = $penerbit;
  13.         $this->harga = $harga;
  14.     }
  15.  
  16.     public function getLabel() {
  17.         return "$this->penulis, $this->penerbit";
  18.     }
  19.  
  20.     public function getInfoProduk() {
  21.         $str = "{$this->judul} | {$this->getLabel()} (Rp. {$this->harga})";
  22.  
  23.         return $str;
  24.     }
  25.  
  26. }
  27.  
  28. class Komik extends Produk {
  29.     public $jmlHalaman;
  30.  
  31.     public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0, $jmlHalaman = 0) {
  32.         parent::__construct($judul, $penulis, $penerbit, $harga);
  33.  
  34.         $this->jmlHalaman = $jmlHalaman;
  35.     }
  36.  
  37.     public function getInfoProduk() {
  38.         $str = "Komik : " . parent::getInfoProduk() . " - {$this->jmlHalaman} Halaman.";
  39.         return $str;
  40.     }
  41. }
  42.  
  43. class Game extends Produk {
  44.     public $waktuMain;
  45.  
  46.     public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0, $waktuMain = 0) {
  47.         parent::__construct($judul, $penulis, $penerbit, $harga);
  48.  
  49.         $this->waktuMain = $waktuMain;
  50.     }
  51.  
  52.     public function getInfoProduk() {
  53.         $str = "Game : " . parent::getInfoProduk() . " ~ {$this->waktuMain} Jam.";
  54.         return $str;
  55.     }
  56. }
  57.  
  58. class CetakInfoProduk {
  59.     public function cetak( Produk $produk ) {
  60.         $str = "{$produk->judul} | {$produk->getLabel()} (Rp. {$produk->harga})";
  61.         return $str;
  62.     }
  63. }
  64.  
  65.  
  66. $produk1 = new Komik("Naruto", "Masashi Kishimoto", "Shonen Jump", 30000, 100);
  67. $produk2 = new Game("Uncharted", "Neil Druckmann", "Sony Computer", 250000, 50);
  68.  
  69. echo $produk1->getInfoProduk();
  70. echo "<br>";
  71. echo $produk2->getInfoProduk();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement