# 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")