"""
作业1:
定义一个类:Person类, name , age
person = Person("", 19)
print(person) => "My name is xxx, age is xx"
bool(person) => 返回False
len(person) => 1024
person2 = Person("", 20)
person == person2 #如果两个年龄相等返回True, 不相等返回False
person < person2 #如果person年龄> person2的年龄,返回False, 否则 返回True
person + person2: 返回: person的名字和person2的名字
作业2:
定义一个装饰器:计算函数执行时间: 使用模块time
time.sleep(2)
#计算函数运行时间;
import time
print(time.time())
time.sleep(2)
print(time.time())
"""
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __eq__(self, other):
return True
def __ne__(self, other):
return False
def __bool__(self):
return False
def __add__(self, other):
return f"{self.name}+{other.name}"
def __gt__(self, other):
return False
def __lt__(self, other):
return True
def __len__(self):
return 1024
def print(self):
print(f"My name is {self.name}, age is {self.age}")
person = Person("张三", 19)
person2 = Person("李四", 20)
person.print()
print(person.age == person2.age)
print(person.age > person2.age)
print(person+person2)
print(person.age < person2.age)
print(bool(person))
print(len(person))