summaryrefslogtreecommitdiff
path: root/mit/inherit.py
diff options
context:
space:
mode:
Diffstat (limited to 'mit/inherit.py')
-rw-r--r--mit/inherit.py125
1 files changed, 125 insertions, 0 deletions
diff --git a/mit/inherit.py b/mit/inherit.py
new file mode 100644
index 0000000..c391c8b
--- /dev/null
+++ b/mit/inherit.py
@@ -0,0 +1,125 @@
+#!/usr/bin/python3
+
+import random
+
+class Animal(object):
+ def __init__(self, age):
+ self.age = age # These are instance variables, they are not created
+ self.name = None # until __init__ method of Animal class is called
+
+ def get_age(self):
+ return self.age
+ def get_name(self):
+ return self.name
+
+ def set_age(self, newage):
+ self.age = newage
+ def set_name(self, newname=""): #default value for name
+ self.name = newname
+
+ def __str__(self):
+ return "animal: " + str(self.name) + ": " + str(self.age)
+
+# Inheritance
+class Cat(Animal):
+ def speak(self):
+ print("meow")
+
+ def __str__(self):
+ return "cat: " + str(self.name) + ": " + str(self.age)
+
+class Rabbit(Animal):
+
+ tag = 1 # This is a class variable, it exists whether or not
+ # the __init__ method is called, and it is a STATIC
+ # variable
+
+ def __init__(self, age, parent1 = None, parent2 = None):
+ Animal.__init__(self, age)
+ self.parent1 = parent1
+ self.parent2 = parent2
+ self.rid = Rabbit.tag
+ Rabbit.tag += 1
+
+
+ def get_rid(self):
+ return str(self.rid).zfill(3)
+ def get_parent1(self):
+ return self.parent1
+ def get_parent2(self):
+ return self.parent2
+
+ def speak(self):
+ print("meep")
+
+ def __str__(self):
+ return "Rabbit: " + str(self.name) + ": " + str(self.age)
+
+ def __add__(self, other):
+ return Rabbit(0, self, other)
+
+ def __eq__(self, other):
+ parents_same = self.parent1.rid == other.parent1.rid and \
+ self.parent2.rid == other.parent2.rid
+
+ parents_opposite = self.parent1.rid == other.parent2.rid and \
+ self.parent2.rid == other.parent1.rid
+
+ return parents_same or parents_opposite
+
+class Person(Animal):
+
+ def __init__(self, name, age):
+ Animal.__init__(self, age) # Reuse superclass init method
+ Animal.set_name(self, name) # Reuse superclass Setter.
+ self.friends = []
+
+ def get_friends(self):
+ return self.friends
+ def add_friend(self, fname):
+ if fname not in self.friends:
+ self.friends.append(fname)
+
+ def speak(self):
+ print("hello")
+ def age_diff(self, other):
+ diff = self.get_age() - other.get_age()
+ if diff > 0:
+ print(self.name, "is", abs(diff), "years olther than", other.name)
+ elif diff < 0:
+ print(self.name, "is", abs(diff), "years younger than", other.name)
+ else:
+ print(self.name, "is the same age as", other.name)
+
+ def __str__(self):
+ return "Person: " + str(self.name) + ": " + str(self.age)
+
+
+class Student(Person):
+
+ def __init__(self, name, age, major=None):
+ Person.__init__(self, name, age)
+ self.major = major
+
+ def change_maker(self, major):
+ self.major = major
+
+ def speak(self):
+ r = random.random()
+
+ if r < 0.25:
+ print("I have homework")
+ elif r < 0.5:
+ print("I need sleep")
+ elif r < 0.75:
+ print("I should eat")
+ else:
+ print ("I am watching tv")
+
+ def __str__(self):
+ return "Student: " + str(self.name) + ": " + str(self.age) + ": " + \
+ str(self.major)
+
+myanimal = Animal(7)
+martin = Cat(7)
+