-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
85 lines (62 loc) · 1.77 KB
/
classes.py
File metadata and controls
85 lines (62 loc) · 1.77 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import math
class Person:
def __init__(self, first, last):
self.first = first
self.last = last
def full_name(self):
return self.first + " " + self.last
person = Person("Willian", "Cruz")
print(person.full_name())
print("---------------------------------------------------------------")
class Circle:
def __init__(self, radius, color):
self.radius = radius
self.color = color
def area(self):
return math.pi * pow(self.radius, 2)
circle = Circle(3, "blue")
print(f"Área do círculo {circle.color}: {circle.area()}")
print("---------------------------------------------------------------")
class ObjectCounter:
num_instances = 0
def __init__(self):
type(self).num_instances += 1
ObjectCounter()
ObjectCounter()
ObjectCounter()
counter = ObjectCounter()
print(counter.num_instances)
print("---------------------------------------------------------------")
class Animal:
num_animals = 0
total_animals = 0
def __init__(self):
type(self).num_animals += 1
Animal.total_animals += 1
class Dog(Animal):
pass
class Cat(Animal):
pass
# Criando instâncias
dog1 = Dog()
cat1 = Cat()
cat2 = Cat()
print("Número total de animais:", Animal.total_animals)
print("Número total de cães:", Dog.num_animals)
print("Número total de gatos:", Cat.num_animals)
print("---------------------------------------------------------------")
class Record:
"""Hold a record of data."""
john = {
"name": "John Doe",
"position": "Python Developer",
"department": "Engineering",
"salary": 80000,
"hire_date": "2020-01-01",
"is_manager": False,
}
john_record = Record()
for field, value in john.items():
setattr(john_record, field, value)
john_record.name
john_record.department