Skip to content

Commit 563d3c4

Browse files
authored
Create Car.py
Files to support the OOP in Java vs Python article
1 parent 21f6077 commit 563d3c4

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

oop-in-java-vs-python/Car.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Vehicle:
2+
3+
def __init__(self, color, model):
4+
self.color = color
5+
self.model = model
6+
7+
class Device:
8+
9+
def __init__(self):
10+
self.voltage = 12
11+
12+
class Car(Vehicle, Device):
13+
14+
wheels = 0
15+
16+
def __init__(self, color, model, year):
17+
Vehicle.__init__(self, color, model)
18+
Device.__init__(self)
19+
self.year = year
20+
21+
def add_wheels(self, wheels):
22+
self.wheels = wheels
23+
24+
@property
25+
def voltage(self):
26+
return self._voltage
27+
28+
@voltage.setter
29+
def voltage(self, volts):
30+
print("Warning: this can cause problems!")
31+
self._voltage = volts
32+
33+
@voltage.deleter
34+
def voltage(self):
35+
print("Warning: the radio will stop working!")
36+
del self._voltage
37+
38+
def __str__(self):
39+
return f'Car {self.color} : {self.model} : {self.year}'
40+
41+
def __eq__(self, other):
42+
return self.year == other.year
43+
44+
def __lt__(self, other):
45+
return self.year < other.year
46+
47+
def __add__(self, other):
48+
return Car(self.color + other.color,
49+
self.model + other.model, int(self.year) + int(other.year))

0 commit comments

Comments
 (0)