Continuing from last time, we are designing a program to simulate the planets and the movement of a space probe. To do so, we need to compute the gravitational field from each body which gives us the acceleration and from the acceleration use the equations of motion to compute the change in position and velocity. We decided to separate the design into objects: one object for each planet, one object for the probe, one object for the sun, and one object for the solar system to control everything. Each of these objects will be built from a template/class. Last time we wrote a method to compute the gravitational field and stuck it inside the Planet class. But that same method needs to be on the Sun object but we don't want to copy and paste the method into the Sun class, we want to reuse it.
Python inheritance is a way of doing so. First a simple example:
class Foo:
def say_hello(self, name):
print("Hello", name)
class Bar(Foo):
def say_goodbye(self, name):
print("Goodbye", name)
x = Foo()
x.say_hello("John")
y = Bar()
y.say_hello("Bob")
y.say_goodbye("Ann")
What happens is we say that Bar
inherits from Foo
. This means that all the methods and properties in the Foo
class appear in the Bar
class. Remember that classes are templates for creating objects. An object like y
created from the Bar
class will have as its initial methods and properties all the methods and properties from Foo
and all the methods and properties from Bar
. In this example there are just two methods. Note that calling x.say_goodbye("Dan")
will produce an error, since x
has no method named say_goodbye
.
We use quite a bit of terminology to refer to this. All of the following mean the same thing and are used interchangeably: for some reason we can't decide on a single terminology.
Bar
inherits from Foo
Bar
is a derived class of Foo
Bar
is a child class of Foo
Foo
is the parent of Bar
.Foo
is the base class of Bar
.If two methods have the same name, the method in the child class wins out and will be the method that is used in the newly created object. (The other method is still around and there is a way to get at it, just not by calling it like a method.)
The primary use of inheritance is to share methods. In our example, we will create a Body
class that will have methods for computing the gravitational field and moving the body. The Planet
, Sun
, and Probe
classes will all derive from Body
. This way all objects created from these classes will have methods.
The code is day26-solarsystem.py. The locations and velocities are from NASA and the probe I use is Juno. The code contains the locations and velocities of these objects on October 26, 2015. The design is as follows.
mass
for the mass in kilogramsx
, y
, z
, for position in kilometersvx
, vy
, vz
for velocity in kilometers/secondname
and color
, for the color to drawname
bodies
which contains a list of all the objects in the solar system: the sun, planets, and probedraw
which draws the solar system by calling draw
on each bodyOne thing to point out is that once the objects are created they live by themselves with their properties and methods. For example, consider the list of bodies in the bodies
property in the SolarSystem class. The objects in this list come from different classes, but they all have a draw
method and all have the methods from the Body
class, so when SolarSystem makes these calls, they will call whatever method is attached to the object. For example, the draw
calls will go to different methods depending on which class the object came from.
Sentence
with the following methods:
__init__
method which takes a single parameter for a sentence as a string and stores this string in a property. Assume the sentence has no punctuation.get_sentence
method which just returns the sentence as a stringget_num_words
method which returns the number of words in the sentence. Hint: split.capitalize
method which modifies the property to make sure the first letter is capitalized. Nothing is returned. Hint: upper but only to the first character.