Zadanie 1: Klasa Book
z metodą getSummary()
Stwórz klasę Book
, która ma pola title
, author
, year
. Dodaj metodę getSummary()
, która zwraca tekst:
„
Tytuł
(Autor, Rok)”
Kroki rozwiązania:
- Utwórz klasę
Book
- Dodaj właściwości:
title
,author
,year
- Zbuduj konstruktor
- Zaimplementuj metodę
getSummary()
- Przetestuj tworząc obiekt i wyświetlając podsumowanie
Przykład Rozwiązania:
class Book {
public $title;
public $author;
public $year;
public function __construct($title, $author, $year) {
$this->title = $title;
$this->author = $author;
$this->year = $year;
}
public function getSummary() {
return "{$this->title} ({$this->author}, {$this->year})";
}
}
// Test
$book = new Book("1984", "George Orwell", 1949);
echo $book->getSummary(); // 1984 (George Orwell, 1949)
Zadanie 2: Klasa Rectangle
z metodami getArea()
i getPerimeter()
Treść:
Zbuduj klasę Rectangle
, która przechowuje długość i szerokość prostokąta oraz liczy pole i obwód.
Kroki:
- Utwórz klasę
Rectangle
- Dodaj pola
width
,height
- Napisz metody
getArea()
igetPerimeter()
- Przetestuj obiekt
Rozwiązanie:
class Rectangle {
public $width;
public $height;
public function __construct($width, $height) {
$this->width = $width;
$this->height = $height;
}
public function getArea() {
return $this->width * $this->height;
}
public function getPerimeter() {
return 2 * ($this->width + $this->height);
}
}
// Test
$r = new Rectangle(5, 10);
echo $r->getArea(); // 50
echo $r->getPerimeter(); // 30
Zadanie 3: Dziedziczenie – Vehicle
i Car
Treść:
Utwórz klasę bazową Vehicle
z metodą getInfo()
, a następnie klasę Car
, która rozszerza Vehicle
i dodaje pole brand
.
Kroki:
- Utwórz klasę
Vehicle
z polemtype
i metodągetInfo()
- Stwórz klasę
Car
dziedziczącą poVehicle
- Dodaj pole
brand
i przesłońgetInfo()
- Utwórz obiekt i przetestuj
Rozwiązanie:
class Vehicle {
public $type;
public function __construct($type) {
$this->type = $type;
}
public function getInfo() {
return "Typ pojazdu: {$this->type}";
}
}
class Car extends Vehicle {
public $brand;
public function __construct($type, $brand) {
parent::__construct($type);
$this->brand = $brand;
}
public function getInfo() {
return "Samochód: {$this->brand} ({$this->type})";
}
}
// Test
$car = new Car("osobowy", "Toyota");
echo $car->getInfo(); // Samochód: Toyota (osobowy)
Zadanie 4: Licznik z inkrementacją
Treść:
Zbuduj klasę Counter
, która ma metodę increment()
i getValue()
. Inicjalna wartość to 0.
Kroki:
- Utwórz klasę
Counter
- Dodaj prywatne pole
$value
- Stwórz metodę
increment()
zwiększającą wartość - Dodaj
getValue()
zwracającą aktualną wartość
Rozwiązanie:
class Counter {
private $value = 0;
public function increment() {
$this->value++;
}
public function getValue() {
return $this->value;
}
}
// Test
$c = new Counter();
$c->increment();
$c->increment();
echo $c->getValue(); // 2
Zadanie 5: Magazyn produktów – tablica obiektów
Treść:
Utwórz klasę Product
z nazwą i ceną. Następnie klasę Warehouse
, która przechowuje tablicę produktów i metodę getTotalValue()
– zwracającą sumę cen.
Kroki:
- Stwórz klasę
Product
z polaminame
,price
- Zbuduj klasę
Warehouse
z polemproducts
(tablica) - Dodaj metodę
addProduct()
do magazynu - Dodaj metodę
getTotalValue()
- Utwórz kilka produktów, dodaj do magazynu i oblicz sumę
Rozwiązanie:
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
class Warehouse {
private $products = [];
public function addProduct(Product $product) {
$this->products[] = $product;
}
public function getTotalValue() {
$total = 0;
foreach ($this->products as $product) {
$total += $product->price;
}
return $total;
}
}
// Test
$p1 = new Product("Mysz", 50);
$p2 = new Product("Klawiatura", 120);
$w = new Warehouse();
$w->addProduct($p1);
$w->addProduct($p2);
echo $w->getTotalValue(); // 170