Hello there,
i am pretty new to object-oriented programming and i have a question:
let's say i have a simple class such as:
When is nesting classes "simple".
class father:
age=...
name=....
def abcd.....
class son(father):
age=....
name=....
def efgh:
or any other heirarchic structure of class and subclasses.
What is the difference between a son and a father? Other than an
implied relationship...
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
self.father = None #not defined at present
self.children = [] #none defined at present
p1 = Person("jack", 24)
p2 = Person("sam", 58)
p1.father = p2
p2.children.append(p1)
i would like to list or print the data content of a given instance of the
subclass, all the way up (e.g. if sam is jack's son, so i would like to
"son" and "father" are both persons -- not a hierarchy of classes.
Your inheritance hierarchy expands to: a Son IS A Father! (Rather than:
a Son HAS A Father) A hierarchy of classes would be, maybe:
class Person(object):
...
class Male(Person):
...
class Female(Person):
...
(a Male IS A Person; a Female IS A Person) which would allow for adding
attributes specific to the subclass. Or...
class Vehicle(object):
...
class AirVehicle(Vehicle):
...
class LandVehicle(Vehicle):
...
class SeaVehicle(Vehicle):
...
class Airship(AirVehicle):
...
class Airplane(AirVehicle):
...
class TurboJet(Airplane):
...
class TurboProp(Airplane):
...
class Glider(Airplane):
...
etc.
A TurboProp IS An Airplane IS An AirVehicle IS A Vehicle. A Vehicle HAS
A Position, HAS A Velocity, HAS A Capacity, etc. All these "has"s are
common to the subclasses.
get their names and ages and use the class as a data structure for that
matter).
sp = p1
while sp:
print sp.name, sp.age
sp = sp.father
(assuming you are doing a trace from youngest to oldest in direct line;
going the other way gets trickier and recursive)
def printPerson(sp, level=0):
print " " * level, sp.name, sp.age
for c in sp.children:
printPerson(c, level+1)
printPerson(p2)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/