-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudents.py
More file actions
70 lines (53 loc) · 2.05 KB
/
Copy pathstudents.py
File metadata and controls
70 lines (53 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Student:
university="Queen Mary" # class variable - see below
def __init__(self, name, surname): # constructor
self.name=name # self.name is an attribute, name is the parameter
self.surname=surname
self._printaccount=0.0
def __str__(self):
s="Name: " + self.name+ " "+ self.surname+"\n"
s+="Print credit: " + str(self._printaccount)
return s
def buyPrintCredit(self, amount): # method with a parameter
self._printaccount+=amount
def canPrint(self): # method that returns a value.
return self._printaccount>0
class BScStudent(Student): # inherit from Student
""" A type of student who may like beer """
def __init__(self, name, surname, likesbeer=True): # constructor
Student.__init__(self, name, surname) # call constructor of the parent class
self.likesbeer=likesbeer
def __str__(self):
# override parent method
s="Name: " + self.name+ " "+ self.surname+"\n"
s+="Print credit: " + str(self._printaccount) +"\n"
if self.likesbeer:
s+="Likes beer"
else:
s+="Does not like beer"
return s
def havePint(self):
if self.likesbeer:
print("Cheers mate!")
else:
print("No thanks")
class PhDStudent(Student): # inherit from Student
""" A type of student who must publish or perish """
def __init__(self, name, surname, haspublished=False):
Student.__init__(self, name, surname) # call constructor of the parent class
self.haspublished=haspublished
def __str__(self):
# override parent method
s=Student.__str__(self) # this is quicker
if self.haspublished:
s+="\nHas published - is safe"
else:
s+="\nWill perish"
return s
def publish(self):
self.haspublished=True
def checkStatus(self):
if self.haspublished:
print("Has published")
else:
print("Has perished")