0% found this document useful (0 votes)
2 views4 pages

Python Class

The document defines an abstract data type for animals and demonstrates inheritance through classes like Cat, Person, Student, and Rabbit. Each class has methods for setting and getting attributes, with specific behaviors for speaking and managing relationships. The Rabbit class includes class variables and methods for addition and equality comparison based on parentage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python Class

The document defines an abstract data type for animals and demonstrates inheritance through classes like Cat, Person, Student, and Rabbit. Each class has methods for setting and getting attributes, with specific behaviors for speaking and managing relationships. The Rabbit class includes class variables and methods for addition and equality comparison based on parentage.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import random

#################################
## Animal abstract data type
#################################
class Animal(object):
def __init__(self, age):
[Link] = age
[Link] = None
def get_age(self):
return [Link]
def get_name(self):
return [Link]
def set_age(self, newage):
[Link] = newage
def set_name(self, newname=""):
[Link] = newname
def __str__(self):
return "animal:"+str([Link])+":"+str([Link])

print("\n---- animal tests ----")


a = Animal(4)
print(a)
print(a.get_age())
a.set_name("fluffy")
print(a)
a.set_name()
print(a)

#################################
## Inheritance example
#################################
class Cat(Animal):
def speak(self):
print("meow")
def __str__(self):
return "cat:"+str([Link])+":"+str([Link])

print("\n---- cat tests ----")


c = Cat(5)
c.set_name("fluffy")
print(c)
[Link]()
print(c.get_age())
#[Link]() # error because there is no speak method for Animal class

#################################
## Inheritance example
#################################
class Person(Animal):
def __init__(self, name, age):
Animal.__init__(self, age)
self.set_name(name)
[Link] = []
def get_friends(self):
return [Link]
def speak(self):
print("hello")
def add_friend(self, fname):
if fname not in [Link]:
[Link](fname)
def age_diff(self, other):
diff = [Link] - [Link]
print(abs(diff), "year difference")
def __str__(self):
return "person:"+str([Link])+":"+str([Link])

print("\n---- person tests ----")


p1 = Person("jack", 30)
p2 = Person("jill", 25)
print(p1.get_name())
print(p1.get_age())
print(p2.get_name())
print(p2.get_age())
print(p1)
[Link]()
p1.age_diff(p2)

#################################
## Inheritance example
#################################
class Student(Person):
def __init__(self, name, age, major=None):
Person.__init__(self, name, age)
[Link] = major
def __str__(self):
return "student:"+str([Link])+":"+str([Link])+":"+str([Link])
def change_major(self, major):
[Link] = major
def speak(self):
r = [Link]()
if r < 0.25:
print("i have homework")
elif 0.25 <= r < 0.5:
print("i need sleep")
elif 0.5 <= r < 0.75:
print("i should eat")
else:
print("i am watching tv")
print("\n---- student tests ----")
s1 = Student('alice', 20, "CS")
s2 = Student('beth', 18)
print(s1)
print(s2)
print(s1.get_name(),"says:", end=" ")
[Link]()
print(s2.get_name(),"says:", end=" ")
[Link]()

#################################
## Use of class variables
#################################
class Rabbit(Animal):
# a class variable, tag, shared across all instances
tag = 1
def __init__(self, age, parent1=None, parent2=None):
Animal.__init__(self, age)
self.parent1 = parent1
self.parent2 = parent2
[Link] = [Link]
[Link] += 1
def get_rid(self):
# zfill used to add leading zeroes 001 instead of 1
return str([Link]).zfill(3)
def get_parent1(self):
return self.parent1
def get_parent2(self):
return self.parent2
def __add__(self, other):
# returning object of same type as this class
return Rabbit(0, self, other)
def __eq__(self, other):
# compare the ids of self and other's parents
# don't care about the order of the parents
# the backslash tells python I want to break up my line
parents_same = [Link] == [Link] \
and [Link] == [Link]
parents_opposite = [Link] == [Link] \
and [Link] == [Link]
return parents_same or parents_opposite
def __str__(self):
return "rabbit:"+ self.get_rid()

print("\n---- rabbit tests ----")


print("---- testing creating rabbits ----")
r1 = Rabbit(3)
r2 = Rabbit(4)
r3 = Rabbit(5)
print("r1:", r1)
print("r2:", r2)
print("r3:", r3)
print("r1 parent1:", r1.get_parent1())
print("r1 parent2:", r1.get_parent2())

print("---- testing rabbit addition ----")


r4 = r1+r2 # r1.__add__(r2)
print("r1:", r1)
print("r2:", r2)
print("r4:", r4)
print("r4 parent1:", r4.get_parent1())
print("r4 parent2:", r4.get_parent2())

print("---- testing rabbit equality ----")


r5 = r3+r4
r6 = r4+r3
print("r3:", r3)
print("r4:", r4)
print("r5:", r5)
print("r6:", r6)
print("r5 parent1:", r5.get_parent1())
print("r5 parent2:", r5.get_parent2())
print("r6 parent1:", r6.get_parent1())
print("r6 parent2:", r6.get_parent2())
print("r5 and r6 have same parents?", r5 == r6)
print("r4 and r6 have same parents?", r4 == r6)

You might also like