Skip to content

Commit 4235b6f

Browse files
committed
Adding basic Python and Java code to support article
1 parent 563d3c4 commit 4235b6f

File tree

4 files changed

+141
-10
lines changed

4 files changed

+141
-10
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Car class
3+
*
4+
* Used for the Real Python OOP in Java vs Python article
5+
*
6+
* Requires both Vehicle.java and Device.java
7+
*/
8+
9+
public class Car extends Vehicle implements Device {
10+
11+
private int year; // Year of our ccar
12+
private int voltage; // Battery voltage
13+
private static int wheels; // How many wheels do we have
14+
15+
/**
16+
* Car constructor
17+
*/
18+
public Car(String color, String model, int year) {
19+
super(color, model);
20+
this.year = year;
21+
this.voltage = 12;
22+
}
23+
24+
/**
25+
* Override the interface method.
26+
*/
27+
@Override
28+
public int getVoltage() {
29+
return voltage;
30+
}
31+
32+
/**
33+
* getYear returns the Car's year
34+
*/
35+
public int getYear() {
36+
return year;
37+
}
38+
39+
/**
40+
* getWheels returns the number of wheels
41+
*/
42+
public static int getWheels() {
43+
return wheels;
44+
}
45+
46+
/**
47+
* setWheelsw sets the number wheels
48+
*/
49+
public static void setWheels(int count) {
50+
wheels = count;
51+
}
52+
53+
/**
54+
* Return a human readable string reoresenting the Car
55+
*/
56+
public String toString() {
57+
return "Car: " + getColor() + " : " + getModel() + " : " + getYear();
58+
}
59+
}

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,74 @@
1+
"""Car and zVehicke classes
2+
3+
Used to demonstrate object oriented techniques in Java vs Python
4+
"""
5+
6+
17
class Vehicle:
8+
"""The Vehicle class is the parent for all vehicles."""
9+
10+
def __init__(self, color, model):
11+
"""Define the color and model of our vehicle"""
12+
self.color = color
13+
self.model = model
214

3-
def __init__(self, color, model):
4-
self.color = color
5-
self.model = model
615

716
class Device:
17+
"""The Device class defines objects which have a battery."""
818

919
def __init__(self):
10-
self.voltage = 12
20+
"""Define the base voltage for our device."""
21+
self._voltage = 12
22+
1123

1224
class Car(Vehicle, Device):
25+
"""The Car class is both a Vehicle and a Device."""
1326

1427
wheels = 0
1528

1629
def __init__(self, color, model, year):
17-
Vehicle.__init__(self, color, model)
18-
Device.__init__(self)
19-
self.year = year
30+
"""Call our oarent classes, then define the year."""
31+
Vehicle.__init__(self, color, model)
32+
Device.__init__(self)
33+
self.year = year
2034

2135
def add_wheels(self, wheels):
36+
"""Change the number of wheels we have."""
2237
self.wheels = wheels
2338

2439
@property
2540
def voltage(self):
41+
"""Allow us to access the Device._voltage property as voltage"""
2642
return self._voltage
2743

2844
@voltage.setter
2945
def voltage(self, volts):
46+
"""Warn the user before resetting the voltage."""
3047
print("Warning: this can cause problems!")
3148
self._voltage = volts
3249

3350
@voltage.deleter
3451
def voltage(self):
52+
"""Warn the user beore deleting the voltage."""
3553
print("Warning: the radio will stop working!")
3654
del self._voltage
3755

3856
def __str__(self):
39-
return f'Car {self.color} : {self.model} : {self.year}'
57+
"""Imorived human readable version of the object."""
58+
return f"Car {self.color} : {self.model} : {self.year}"
4059

4160
def __eq__(self, other):
61+
"""Do these objects have the same year?"""
4262
return self.year == other.year
4363

4464
def __lt__(self, other):
65+
"""Which object was made earlier than the other?"""
4566
return self.year < other.year
4667

4768
def __add__(self, other):
48-
return Car(self.color + other.color,
49-
self.model + other.model, int(self.year) + int(other.year))
69+
"""Add the objects together in our oredefined way."""
70+
return Car(
71+
self.color + other.color,
72+
self.model + other.model,
73+
int(self.year) + int(other.year),
74+
)

oop-in-java-vs-python/Device.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Device Interface
3+
*
4+
* Used for the Real Python OOP in Java vs Python article
5+
*/
6+
public interface Device {
7+
8+
/**
9+
* getVoltage returns the current battery voltage
10+
*/
11+
int getVoltage();
12+
}

oop-in-java-vs-python/Vehicle.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Vehicle class
3+
*
4+
* Used for the Real Python OOP in Java vs Python article
5+
*
6+
* Implements basic functionality for our Vehicle
7+
*/
8+
public class Vehicle {
9+
10+
private String color; // What color are we?
11+
private String model; // What model vehicle are we?
12+
13+
/**
14+
* Vehicle constructor
15+
*/
16+
public Vehicle(String color, String model) {
17+
this.color = color;
18+
this.model = model;
19+
}
20+
21+
/**
22+
* getColor returns the Vehicles color
23+
*/
24+
public String getColor() {
25+
return color;
26+
}
27+
28+
/**
29+
* getModel returns the Vehicles model
30+
*/
31+
public String getModel() {
32+
return model;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)