**
python 魔术方法
**
class Student:
# 可以省略
name = None
age = None
tel = None
def __init__(self, name, age, tel):
self.name = name
self.age = age
self.tel = tel
print("Student创建一个类对象")
def __str__(self):
return f"Student类对象,name:{self.name},{self.age},{self.tel}"
# 小于大于
def __lt__(self, other):
return self.age < other.age
# 小于等于 大于等于
def __le__(self, other):
return self.age <= other.age
#
def __eq__(self, other):
return self.age==other.age
stu = Student("莉莉", 18, "13800138000")
stu1 = Student("李红", 20, "13800138000")
print(stu < stu1)
print(stu <= stu1)
print(stu == stu1)
运行结果:
Student创建一个类对象
Student创建一个类对象
True
True
False