Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] OOP, Object-Oriented Programming
kosmos
post 30.05.2008, 20:29:23
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 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 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

Posty w temacie


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 Wersja Lo-Fi Aktualny czas: 14.08.2025 - 06:26