Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] interface
plej
post
Post #1





Grupa: Zarejestrowani
Postów: 264
Pomógł: 11
Dołączył: 9.05.2011
Skąd: Gdańsk

Ostrzeżenie: (10%)
X----


  1. interface Band {
  2. public function getName();
  3. public function GetGenre();
  4. public function addMusician();
  5. public function getMusicians();
  6. }
  7.  
  8. interface Musician {
  9. public function addInstrument();
  10. public function getInstruments();
  11.  
  12. public function assignToBand();
  13. public function getMusicianType();
  14. }
  15.  
  16. interface Instrument {
  17. public function getName();
  18. public function getCategory();
  19. }
  20.  
  21. class Guitarist implements Musician {
  22. private $last;
  23. private $first;
  24. private $musicianType;
  25.  
  26. private $instruments;
  27. private $bandReference;
  28.  
  29. function __construct($first, $last) {
  30. $this->last = $last;
  31. $this->first = $first;
  32. $this->instruments = array();
  33. $this->musicianType = "gitarzysta";
  34. }
  35.  
  36. public function getName() {
  37. return $this->first . " ". $this->last;
  38. }
  39.  
  40. public function addInstrument(Instrument $instrument) {
  41. array_push($this->instruments, $instrument);
  42. }
  43.  
  44. public function getInstruments() {
  45. return $this->instruments;
  46. }
  47.  
  48. public function getBand() {
  49. return $this->$bandReference;
  50. }
  51.  
  52. public function assignBand(Band $band) {
  53. $this->$bandReference = $band;
  54. }
  55.  
  56. public function getMusicianType() {
  57. return $this->musicianType;
  58. }
  59.  
  60. public function setMusicianType($musicianType) {
  61. $this->musicianType = $musicianType;
  62. }
  63. }
  64.  
  65. class LeadGuitarist extends Guitarist {
  66. function __construct($last, $first) {
  67. parent::__construct($last, $first);
  68. $this->setMusicianType("główny gitarzysta");
  69. }
  70. }
  71.  
  72. class RockBand implements Band {
  73. private $bandName;
  74. private $bandGenre;
  75. private $musicians;
  76.  
  77. function __construct($bandName) {
  78. $this->bandName = $bandName;
  79. $this->musicians = array();
  80. $this->bandGenre = "rock";
  81. }
  82.  
  83. public function getName() {
  84. return $this->bandName;
  85. }
  86.  
  87. public function getGenre() {
  88. return $this->bandGenre;
  89. }
  90.  
  91. public function addMusician(Musician $musician) {
  92. array_push($this->musicians, $musician);
  93. $musicians->assignToBand($this);
  94. }
  95.  
  96. public function getMusicians() {
  97. return $this->musicians;
  98. }
  99. }
  100.  
  101. class Guitar implements Instrument {
  102.  
  103. private $name;
  104. private $category;
  105.  
  106. function __construct($name) {
  107. $this->name = $name;
  108. $this->category = "gitary";
  109. }
  110.  
  111. public function getName() {
  112. return $this->name;
  113. }
  114.  
  115. public function getCategory() {
  116. return $this->category;
  117. }
  118. }
  119.  
  120. // test Obiektów.
  121. $band = new RockBand("Czerwone Zmienne");
  122. $bandMemberA = new Guitarist("Michał", "Zmiennoprzecinkowy");
  123. $bandMemberB = new LeadGuitarist("Grzegorz", "Całkowity");
  124.  
  125. $bandMemberA->addInstrument(new Guitar("Gibson Les Paul"));
  126. $bandMemberB->addInstrument(new Guitar("Fender Stratocaster"));
  127. $bandMemberB->addInstrument(new Guitar("Hondo H-77"));
  128.  
  129. $band->addMusician($bandMemberA);
  130. $band->addMusician($bandMemberB);
  131.  
  132. foreach($band->getMusicians() as $musician) {
  133. echo "Muzyk" .$musician->getName() ."<br>";
  134. echo "to " .$musician->getMusicianType() ."<br>";
  135. echo "w zespole grającym " .$musician->getBand()->getGenre() ."<br>";
  136. echo "o nazwie " .$musician->getBand()->getName() ."<br>";
  137.  
  138. foreach($musician->getInstruments() as $instrument) {
  139. echo "Jego instrument to " .$instrument->getName() ." ";
  140. echo "(" .$instrument->getCategory() .")<br>";
  141. }
  142. echo "<p>";
  143. }



Cytat
Fatal error: Declaration of Guitarist::addInstrument() must be compatible with that of Musician::addInstrument() in /opt/lampp/htdocs/nauka/index.php on line 26



Cześć biggrin.gif
U góry kod php i błąd wie ktoś w czym problem tu jest? bo ja nie umiem sb z tym poradzić:/
obiektowego uczę się pare dni dopiero jak coś...
Z góry thx.smile.gif


EDIT:

linia 26 nie,
  1. class Guitarist implements Musician {

czyli linia 21 to błąd.
ten fragment porostu nie ma znaczników php i zapominałem siębiggrin.gif

Ten post edytował plej 7.06.2011, 09:33:38
Go to the top of the page
+Quote Post

Posty w temacie
- plej   [PHP] interface   7.06.2011, 09:30:50
- - nospor   Cytatobiektowego uczę się pare dni dopiero jak coś...   7.06.2011, 09:32:47
- - plej   ale tak jest w książce dokładnie przepisane.. czy...   7.06.2011, 09:35:07
- - nospor   zrób zdjęcie i nam wklej. Bo nie wierzę, że tak je...   7.06.2011, 09:37:36
- - phpion   W interfejsie zmień: [PHP] pobierz, plaintext publ...   7.06.2011, 09:37:47
- - Daiquiri   Porównać public function addInstrument(); z interf...   7.06.2011, 09:38:05
- - plej   No to patrz łap screnna: http://zapodaj.net/upload...   7.06.2011, 09:40:27
- - nospor   A co ty mi dajesz? Link do uploadu? Ty mi daj link...   7.06.2011, 09:42:21
- - plej   Zmieniłem jak powiedzieliście ii mam next problem ...   7.06.2011, 09:42:40
- - Daiquiri   Plej, zacznij czytać komunikaty. Masz zadeklarowan...   7.06.2011, 09:48:47
- - plej   ehh no czytam komunikaty i tłumacze ale poprostu n...   7.06.2011, 09:51:30
- - Daiquiri   To zacznij najpierw od teorii. Doczytaj do czego m...   7.06.2011, 09:54:16
- - plej   oka ale wiesz gdzie mogę sobie o tym poczytać? bo ...   7.06.2011, 09:56:17
- - Daiquiri   W Internecie . Jak zerkniesz nawet tutaj, to przec...   7.06.2011, 10:18:24
- - plej   spoko tak czytam z zrozumieniem ale i tak mi coś n...   7.06.2011, 10:34:35
- - olechafm   generalnie powiem Ci szczerze, że wywal tę książkę...   7.06.2011, 10:37:57
- - plej   Spoko wywalę te książki sobie kupię : http://hel...   7.06.2011, 10:42:38
- - olechafm   ta pierwsza Hasina Haydera też nie grzeszy zajebis...   7.06.2011, 10:53:30
- - plej   wiem wiem:) ale chodzi o zrozumienie tego:) reszta...   7.06.2011, 11:08:55
- - Daiquiri   Prawda, że pierwsza pozycja nie jest może taka jak...   7.06.2011, 11:12:30
|- - olechafm   Cytat(Daiquiri @ 7.06.2011, 12:12:30 ...   8.06.2011, 00:38:24
- - dboss   Książka z której jest podany przykład (PHP5. Zaawa...   7.06.2011, 11:26:33
- - plej   To ja już nie wiem którą kupić polecacie jakąś dob...   8.06.2011, 06:06:40
- - olechafm   niestety ale wyboru nie masz wielkiego, szczególni...   8.06.2011, 08:53:10
- - plej   spoko:) no ja chce tylko zrozumieć obiektowe i wte...   8.06.2011, 09:37:39
- - LSM   Myślę że ta kniga "PHP5 zaawansowane programo...   8.06.2011, 18:44:01


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 00:25