Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] OOP, Object-Oriented Programming
kosmos
post
Post #1





Grupa: Zarejestrowani
Postów: 367
Pomógł: 17
Dołączył: 4.03.2008

Ostrzeżenie: (0%)
-----


Witam

Już kiedyś pisałem że chciałem się wdrożyć w programowanie obiektowe w PHP (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Napisałem cosbie taki oto kod składający się z 3 klas:

  1. <?php
  2.  
  3. class Car{
  4.  protected $mark,$color,$mileage;
  5.  
  6.  protected function __construct($mark,$color,$milleage){
  7.  $this->mark=$mark;
  8. $this->color=$color;
  9. $this->mileage=$milleage;
  10.  }
  11.  
  12.  protected function getMark(){
  13.  return $this->mark;
  14. }
  15.  
  16.  protected function getColor(){
  17.  return $this->color;
  18. }
  19.  
  20.  protected function getMileage(){
  21.  return $this->mileage;
  22.  }
  23.  
  24.  private function getAllPropertiesOfCar(){
  25.  return $this->getMark().' '.$this->getColor().' '.$this->getMileage();
  26.  }
  27.  
  28.  }
  29.  
  30.  
  31. class Person extends Car{
  32.  protected $name,$surname,$age,$profession,$auto;
  33.  
  34.  protected function setName($name){
  35.  $this->name=$name;
  36.  }
  37.  
  38.  protected function setSurname($surname){
  39.  $this->surname=$surname;
  40. }
  41.  
  42.  protected function setAgeAndProfession($age,$profession){
  43.  $this->age=$age; $this->profession=$profession;
  44.  }
  45.  
  46.  protected function giveAllPropertiesOfPerson(){
  47.  print $this->name.' '.$this->surname.' '.$this->age.' '.$this->profession.'<br><br>';
  48.  
  49.  }
  50.  
  51.  
  52.  public  function chooseYourCar($mark,$color,$milleage){
  53.  $this->mark=$mark;
  54.  $this->color=$color;
  55.  $this->mileage=$milleage;
  56.  }
  57.  
  58.  
  59.  
  60.  
  61. public  function showNewCar(){
  62.  print $this->name. ' bought a new car. It`s: '.$this->getMark().' the color is: '.$this->getColor().' and the milleage is not to bad: '.$this->getMileage();
  63.  }
  64.  
  65.  }
  66.  
  67.  
  68.  
  69.  class Family extends Person{
  70.  private $address,$fortune,$volume;
  71.  
  72.  function __construct($address,$fortune,$volume){
  73.  
  74. $this->address=$address;
  75.  $this->fortune=$fortune;
  76.  $this->volume=$volume;
  77.  }
  78.  
  79.  
  80.  public function addPerson($name,$surname,$age,$profession){
  81.  $this->setName($name);
  82. $this->setSurname($surname);
  83. $this->setAgeAndProfession($age,$profession);
  84. }
  85.  
  86.  
  87.  public function printFamily(){
  88. $this->giveAllPropertiesOfPerson();
  89. }
  90.  
  91.  public function printPropertiesOfFamilyCar(){
  92. $this->showNewCar();
  93. }
  94.  
  95.  
  96.  public function hugePersonOfFamily(Person $p){
  97.  print'<br>';
  98.  return $this->name.' is hugging '. $p->name;
  99.  }
  100.  
  101.  };
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  // CIAŁO
  112.  
  113.  $family= new Family('London',240000,4);
  114.  $family->addPerson('John','Kowalski',46,'Programmer');
  115.  
  116.  $family_a=new Family('Krakow',120000,2);
  117.  $family_a->addPerson('Izabel','Kowlaska',44,'Designer');
  118.  
  119.  $family->printFamily();
  120.  $family_a->printFamily();
  121.  
  122.  $family->chooseYourCar('volvo','red' ,230000 );
  123.  $family->showNewCar();
  124.  print $family->hugePersonOfFamily($family_a);
  125.  
  126. ?>

Proszę znawców o zweryfikowanie czy dobrze rozumiem pojecie obiektowości. Zastosowałem oddzielne klasy dla obiektów, dodałem dla nich unikalne funkcje gdzie niekiedy inne obiekty korzystają z funkcji innych obiektów.

Proszę o opinie czy moje wypocinki można nazwać choź troszkę OOP (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

EDIT:

oto wynik zwracany przez powyższy kod

Kod
John Kowalski 46 Programmer

Izabel Kowlaska 44 Designer

John bought a new car. It`s: volvo the color is: red and the milleage is not to bad: 230000
John is hugging Izabel


Ten post edytował kosmos 30.05.2008, 20:30:47
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
uznam to za złośliwość z Twojej strony ( wczoraj było dosyć późno ... nie każdy jest robokopem a każdy potrzenuje wypoczynku stąd być może moje błędy )
Pokazujesz kod OOP i prosisz o ocene i wskazanie bledow. Dostales to odemnie. Dostales tez sugestie bys jednak zaczal od nauki podstaw bo z tym tez u Ciebie krucho. Jak chcesz to traktuj to sobie jako zlosliwosc. Wybacz, ale ja nie wiem czy ty gadasz ze mna na forum po nieprzespanej nocy, czy po trzech nieprzespanych nocach. Oddajesz cos pod ocene, znaczy ze chcesz oceny. Koniec kropka. Jak jestes zmeczony to sie przespij i napisz kod dnia nastepnego. Ludzie jak cos analizują Twojego to poswiecają na to swoj czas, wiec oddawaj cos co piszesz będąc w pelni swiadomym. Szanuj czas innych, ktorych prosisz o pomoc.

Widze ze dales pomogl dla dr_bonzo. Ok. Ale ja Ci nie pomogłem? Pokazałem ci na samym początku jakie robisz podstawowe błedy. dr_bonzo niejako to podsumował nie dodając nic więcej. Odnosze wrażenie, ze skoro napisalem bys sie za podstawy wziął to mnie w ten sposob karzesz :/ Dziwne postepowanie.
Nie zeby mi zalezalo na pomogl od Ciebie. Chce ci jedynie zwrocic uwage bys na przyszlosc troche inaczej postepowal. (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

edit: dziękuję za Pomogł (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 2.10.2025 - 19:56