Python 小栈_17:Python 面向对象及静态属性、类方法、静态方法
今日所学:
一、面向对象作业
1、学校类
class School: def __init__(self,name,addr,type): self.name=name self.addr=addr self.type=type def test(self,name): print("%s 正在举行考试" %self.name) def athlete(self,name): print("%s 正在举行运动会" %self.name) def zhao_sheng(self,name): print("%s 正在招生" %self.name) print(School.__dict__) s1=School("清华大学","北京","公立大学") print(s1.name) print(s1.__dict__) s1.test("清华大学") >>> {'__module__': '__main__', '__init__': <function School.__init__ at 0x000001F8E737B4C0>, 'test': <function School.test at 0x000001F8E737B550>, 'athlete': <function School.athlete at 0x000001F8E737B5E0>, 'zhao_sheng': <function School.zhao_sheng at 0x000001F8E737B670>, '__dict__': <attribute '__dict__' of 'School' objects>, '__weakref__': <attribute '__weakref__' of 'School' objects>, '__doc__': None} 清华大学 {'name': '清华大学', 'addr': '北京', 'type': '公立大学'} 清华大学 正在举行考试
2、老师类
#老师类 class Teacher: def __init__(self,name,gender,age,subject): self.name=name self.gender=gender self.age=age self.subject=subject def teach_class(self,name,subject): print("%s教%s科目" %(self.name,self.subject)) def give_homework(self,name): print("%s布置了作业" %self.name) t1=Teacher("happy","female",30,"英语") print(Teacher.__dict__) print(t1.name) t1.teach_class("happy","英语") t1.give_homework("happy") >>> {'__module__': '__main__', '__init__': <function Teacher.__init__ at 0x00000283134CB4C0>, 'teach_class': <function Teacher.teach_class at 0x00000283134CB550>, 'give_homework': <function Teacher.give_homework at 0x00000283134CB5E0>, '__dict__': <attribute '__dict__' of 'Teacher' objects>, '__weakref__': <attribute '__weakref__' of 'Teacher' objects>, '__doc__': None} happy happy教英语科目 happy布置了作业
3、课程类
#课程类 class Course: def __init__(self,kind,price,teacher): self.kind=kind self.price=price self.teacher=teacher def set_course(self,teacher,kind): print("%s设置了%s课" %(self.teacher,self.kind)) def set_price(self,kind,price): print("%s课的价钱为%s" %(self.kind,self.price)) c1=Course("python",1000,"happy") print(Course.__dict__) print(c1.__dict__) print(c1.kind) c1.set_course("happy","python") c1.set_price("python","1000") >>> {'__module__': '__main__', '__init__': <function Course.__init__ at 0x000001CC4078B4C0>, 'set_course': <function Course.set_course at 0x000001CC4078B550>, 'set_price': <function Course.set_price at 0x000001CC4078B5E0>, '__dict__': <attribute '__dict__' of 'Course' objects>, '__weakref__': <attribute '__weakref__' of 'Course' objects>, '__doc__': None} {'kind': 'python', 'price': 1000, 'teacher': 'happy'} python happy设置了python课 python课的价钱为1000
二、静态属性、类方法和静态方法
1、静态属性
用法:封装函数属性,跟实例绑定,可访问实例属性也可访问类属性
class Course: def __init__(self,kind,price,teacher): self.kind=kind self.price=price self.teacher=teacher def set_course(self,teacher,kind): print("%s设置了%s课" %(self.teacher,self.kind)) @property #调用静态属性 def set_price(self): #property中自动有self参数,一般用return来返回值,且在调用时不用加括号,即不用再加任何参数 return "%s课的价钱为%s" %(self.kind,self.price) c1=Course("python",1000,"happy") print(Course.__dict__) print(c1.__dict__) print(c1.kind) c1.set_course("happy","python") print(c1.set_price) #像调实例数据属性一样去调用函数属性,封装你的逻辑,实际是底层做了转换 print(Course.set_price) #类和实例都可调用 >>> {'__module__': '__main__', '__init__': <function Course.__init__ at 0x000001B25CCDB4C0>, 'set_course': <function Course.set_course at 0x000001B25CCDB550>, 'set_price': <property object at 0x000001B25CCE8040>, '__dict__': <attribute '__dict__' of 'Course' objects>, '__weakref__': <attribute '__weakref__' of 'Course' objects>, '__doc__': None} {'kind': 'python', 'price': 1000, 'teacher': 'happy'} python happy设置了python课 python课的价钱为1000 <property object at 0x000001B25CCE8040>
2、类方法
类方法即不用跟实例捆绑在一起的方法,只是类调用自己的属性。在定义属性时会自动有cls参数。
class Student: tag="中学学生" def __init__(self,name,gender,age): self.name=name self.gender=gender self.age=age def have_class(self,name): print("%s在上课" %self.name) @classmethod def tell_info(cls): print(cls) print(cls.tag) Student.tell_info() #只用来查看类的信息,不用再经过实例即可调用 >>> <class '__main__.Student'> 中学学生
3、静态方法
只是名义上的归属类管理,不能使用类变量和实例变量。没有任何参数。
class Classroom: def __init__(self,classroom,grade,amount): self.classroom=classroom self.grade=grade self.amount=amount @staticmethod #一个类的工具包,来调用跟类和变量都没有关系的属性 def have_class(x,y): print("%s班有%s人" %(x,y)) def test(x,y): print(x,y) c1=Classroom("五班","高二级",40) print(Classroom.__dict__) #在类字典中,have_class函数属于staticmethod object,而test函数则属于function c1.have_class(1,2) print(c1.__dict__) #在实例字典中只有数据属性 # c1.test(1,2) #因为test函数没有self变量,而python又会自动传入实例本身,因此会报错 >>> {'__module__': '__main__', '__init__': <function Classroom.__init__ at 0x000001BCB042B4C0>, 'have_class': <staticmethod object at 0x000001BCB03EAA60>, 'test': <function Classroom.test at 0x000001BCB042B5E0>, '__dict__': <attribute '__dict__' of 'Classroom' objects>, '__weakref__': <attribute '__weakref__' of 'Classroom' objects>, '__doc__': None} 1班有2人 {'classroom': '五班', 'grade': '高二级', 'amount': 40}
以上。

浙公网安备 33010602011771号