-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClasses_e_Objetos4.py
100 lines (75 loc) · 2.26 KB
/
Classes_e_Objetos4.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Este exercicio e o mesmo que o anterior
a penas obedecendo as convencoes comumente
exigidas a programacao orientada a objetos,
como encapsulamento, restricoes a acessos
diretos de atributos e etc...
Agora, o unico meio de acesso aos atributos
seria pelos metodos de acesso, a tentativa
de acessar algum atributo diretamente de fora
da classe seria reconhecida como violacao, resultando
em um erro de execucao.
"""
# a class header
class Time:
"""Class Time with accessor methods"""
def __init__(self, hour=0, minute=0, second=0):
"""Time constructor initializes each data member to zero"""
self.setTime(hour, minute, second)
def setTime(self, hour, minute, second):
"""Set values of hour, minute and second"""
#Metodos de acessos aos atributos
self.setHour(hour)
self.setMinute(minute)
self.setSecond(second)
def setHour(self, hour):
"""Set hour value"""
if 0 <= hour < 24:
self.__hour = hour
else:
raise (ValueError, "Invalid hour value: %d" % hour)
def setMinute(self, minute):
"""Set minute value"""
if 0 <= minute < 60:
self.__minute = minute
else:
raise (ValueError, "Invalid minute value: %d" % minute)
def setSecond(self, second):
"""set second value"""
if 0 <= second < 60:
self.__second = second
else:
raise (ValueError, "Invalid second value: %d" % second)
def getHour(self):
"""Get hour value"""
return self.__hour
def getMinute(self):
"""Get minute value"""
return self.__minute
def getSecond(self):
"""Get second value"""
return self.__second
def printMilitary(self):
"""Prints time object in military format"""
return "%.2d:%.2d:%.2d" % (self.__hour, self.__minute, self.__second)
def printStandard(self):
"""Prints time object in standard format"""
standardTime = ""
if self.__hour == 0 or self.__hour == 12:
standardTime += "12:"
else:
standardTime += "%d:" % (self.__hour % 12)
standardTime += "%.2d:%.2d" % (self.__minute, self.__second)
if self.__hour < 12:
standardTime += " AM"
else:
standardTime += " PM"
return standardTime
"""
Isso e a penas uma classe,
faremos os teste da classe
em outro arquivo python
chamado 'Teste_Classes_e_Objetos4.py'
mas por via das duvidas, execute o comando
python nesta classe para averiguar erros.
"""