-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car2.php
162 lines (145 loc) · 2.83 KB
/
Car2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Class Wheel класс колес для автомобиля
*/
class Wheel
{
/**
* @var float
*/
/**
* @var float|int
*/
/**
* @var float|int|null
*/
private $pressure,
$d,
$owner;
/**
* Wheel constructor.
* @param null $owner
* @param float $pressure
* @param int $d
*/
function __construct($owner = null, $pressure = 2.0, $d = 13)
{
$this->pressure = $pressure;
$this->d = $d;
$this->owner = $owner;
}
/**
* @return float|int|null
*/
function getPressure()
{
return $this->pressure;
}
/**
* @param $pressure
*/
function setPressure($pressure)
{
if ($pressure < 0) {
$pressure = 0;
}
if ($pressure > 5) {
echo "BABAH!!!<br>";
$pressure = 0;
}
if ($this->owner != null) {
if ($pressure == 0) {
$this->owner->speed = 0;
} else {
if ($pressure < 0.5) {
$this->owner->speed /= 2;
}
}
}
$this->pressure = $pressure;
}
}
/**
* Class Car состоит из кузова и колес класса Wheel
*/
class Car
{
/**
* @var
*/
/**
* @var string
*/
/**
* @var int|string
*/
/**
* количество колесв автомобиле может быть 4, 6, ...
* @var int|string
*/
public $year;
public $color;
public $speed;
public $wheels = array();
/**
* Car constructor.
* @param string $color
* @param int $speed
*/
function __construct($color = 'Grey', $speed = 0)
{
$this->color = $color;
$this->speed = $speed;
for ($i = 0; $i < 4; ++$i) {
$this->wheels[$i] = new Wheel($this);
}
}
/**
* @param $speed
*/
function aacelerate($speed)
{
$this->speed += $speed;
}
/**
* @return string
*/
function __toString()
{
return "This is {$this->color} car with speed = {$this->speed}!";
}
}
$a = new Car();
$b = new Car('Green', 120);
// накачаем во второе колесо 7 бар
$b->wheels[2]->setPressure(7);
echo $a."<br>";
$a->color = 'Red';
echo $a."<br>";
echo $b."<br>";
for ($i = 0; $i < 4; ++$i) {
echo $b->wheels[$i]->getPressure()."<br>";
}
// постфиксная и префиксная записи: сравнение
function post(&$x)
{
$temp = $x;
$x += 1;
return $temp;
}
function pref(&$x)
{
return $x += 1;;
}
$x = 10;
echo post($x)."<br>";
echo $x."<br>";
$x = 10;
echo $x++."<br>";
echo $x."<br>";
$x = 10;
echo pref($x)."<br>";
echo $x."<br>";
$x = 10;
echo ++$x."<br>";
echo $x."<br>";