-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclase_objetos_2.py
41 lines (29 loc) · 975 Bytes
/
clase_objetos_2.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
# Python program to demonstrate
# use of class method and static method.
from datetime import date
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def giveMeYourName(self):
return self.name
# a class method to create a Person object by birth year.
@classmethod
def fromBirthYear(cls, name, year):
return cls(name, date.today().year - year)
@classmethod
def isAdultFromBirthYear(cls, year):
return cls.isAdult(date.today().year - year)
# a static method to check if a Person is adult or not.
@staticmethod
def isAdult(age):
return age > 18
person1 = Person('mayank', 21)
print (person1.giveMeYourName())
person2 = Person.fromBirthYear('mayank', 1996)
print (person1.age)
print (person2.age)
# print the result
print (Person.isAdult(15))
print (Person.isAdult(person2.age))
print(Person.isAdultFromBirthYear(1999))