Last active
October 24, 2021 23:25
-
-
Save msyvr/aa6575f8cb8a4bc31fad411339fa96b4 to your computer and use it in GitHub Desktop.
mit 6.0001 implement and use classes, subclasses, and class variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# walk through principles of implementing and calling classes and sub/superclasses | |
# implement and use a class variable | |
class Animal(object): | |
def __init__(self, age): | |
assert type(age) == int | |
self.age = age | |
self.name = [] | |
def __str__(self): | |
return "Hey! An Animal of age: " + str(self.age) + " years." | |
def get_age(self): | |
return self.age | |
def set_age(self, new_age): | |
self.age = new_age | |
def get_name(self): | |
return self.name | |
def set_name(self, new_name): | |
self.name = new_name | |
class Cat(Animal): | |
def __str__(self): | |
return 'meow meow meow' | |
class Person(Animal): | |
def __init__(self, age, name): | |
Animal.__init__(self, age) | |
self.name = name | |
self.friends = [] | |
def get_name(self): | |
return self.name | |
def set_name(self, new_name): | |
self.name = new_name | |
def get_friends(self): | |
return self.friends | |
def add_friend(self, new_friend): | |
if new_friend not in self.friends: | |
self.friends.append(new_friend) | |
def speak(self): | |
return "Hi, I'm " + str(self.name) | |
def age_diff(self, other): | |
diff = abs(self.age - other.age) | |
return str(self.name) + " and " + str(other.name) + " have a " + str(diff) + " year gap in their ages" | |
def __str__(self): | |
return "Person: " + str(self.name) + ": Age: " + str(self.age) | |
class Student(Person): | |
def __init__(self, name, age, major = "none"): | |
Person.__init__(self, age, name) | |
self.major = major | |
self.friends = [] | |
def get_major(self): | |
return "Major: " + str(self.major) | |
def change_major(self, new_major): | |
self.major = new_major | |
def __str__(self): | |
return "Gotta run to class." | |
# use the Animal class | |
print("Use the Animal class:") | |
my_animal = Animal(3) | |
print(my_animal) | |
# use the Cat subclass with no new data attributes | |
print("Use the Cat class:") | |
my_cat = Cat(1) | |
print(my_cat) | |
my_cat.set_name('fluffy') | |
print('The cat has a name! ' + my_cat.get_name()) | |
# use the Person subclass with new data attributes | |
print('Use the Person class:') | |
name1 = input('A name for a Person class instance: ') | |
age1 = int(input('Their age (years): ')) | |
p1 = Person(age1, name1) | |
print(p1) | |
print(p1.speak()) | |
name2 = input('A name for another Person: ') | |
age2 = int(input('Their age (years): ')) | |
p2 = Person(age2, name2) | |
print(p2) | |
print(p2.speak()) | |
p1.add_friend('Sasha') | |
print(str(p1.get_name()) + "\'s friends are: "+ str(p1.get_friends())) | |
p2_friends = list(input(f'list some friends for {p2.get_name()} (spaces between names): \n').split(" ")) | |
for friend in p2_friends: | |
p2.add_friend(friend) | |
print(str(p2.get_name()) + "\'s friends are: "+ str(p2.get_friends())) | |
print(p1.age_diff(p2)) | |
# use the student subsubclass with new data attributes | |
print("Use the Student subclass of Person:") | |
genius = Student('Leah', 20, 'comp sci') | |
print('New student: ' + str(genius.get_name() + ': Age: ' + str(genius.age) + ': Major: ' + str(genius.major))) | |
new_major = input('Let\'s change major to: ') | |
genius.change_major(new_major) | |
print('Changed major to: ' + str(genius.major)) | |
print(genius) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment