# 1.类的属性class cat: name = '布偶' age = 9p1 = cat # 这是一个cat类print(p1)print(p1.name)p2 = cat() # 这是一个cat对象print(p2)print(p1.name)p3 = catp3.name = '毛线' # 属性会优先找对象,对象没有才会去类找print(p2.name)print(p2.__dict__) # {} __dict__获取对象的字典信息print(p2.__class__) # 查看类型print(cat.__dict__)# 2.初始化属性class Teacher: school = "oldboy" # def init(obj, name, age): # obj.name = name # obj.age = age def __init__(self, name, age): print(self) self.name = name self.age = aget1 = Teacher("jack", 18)print(t1.name)print(t1)# 3.行为定制class Student: school = "oldgirl" def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender # def study(self): # print(self) def say_hi(self): # print(self) print("hello i am a student! my name:%s" % self.name) def a(self): picklestu1 = Student("jack", 20, "male") # 这是一个对象stu2 = Student("rose", 18, "female")# stu1.say_hi()# stu2.say_hi()# print(stu1)# stu2.say_hi() # 对象调用函数# Student.say_hi(stu1) # 类调用函数print(type(Student.say_hi))print(type(stu1.say_hi))# 4.类绑定方法class OldBoyStudent: school = "oldboy" def __init__(self,name): self.name = name @classmethod # 类绑定方法 def show_school(cls): # print(self.school) print(cls) @staticmethod # 看成普通函数 def print_hello(): print("hello world")