一、day24复习

class school:
x=1
#__init__初始化函数,用来帮类实例化一个具体的对象
def __init__(self,name,addr):
#前面的Name是一个需要封到字典里面的一个key,后面的name是传过来的一个值
self.Name=name
self.Addr=addr
def tell_info(self):
print('学校的详细信息是:name:%s addr:%s'%(self.Name,self.Addr))
#实例化的过程
s1=school('charon','qqhr')
print(school.__dict__)
print(s1.__dict__)
s1.tell_info() 结果:
{'__module__': '__main__', 'x': 1, '__init__': <function school.__init__ at 0x7f8835927158>, 'tell_info': <function school.tell_info at 0x7f88359271e0>, '__dict__': <attribute '__dict__' of 'school' objects>, '__weakref__': <attribute '__weakref__' of 'school' objects>, '__doc__': None}
{'Name': 'charon', 'Addr': 'qqhr'}
学校的详细信息是:name:charon addr:qqhr

二、静态属性

封装操作好处可以将背后的操作隐藏起来

class Room:
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh
@property
#@property加上这个后面调用不用加括号
def cal_area(self):
######print('%s 住的 %s 总面积是%s' %(self.owner,self.name,self.width*self.length))
return self.width*self.heigh r1=Room('WC','pluto',100,12,123)
#####r1.cal_area
print(r1.cal_area)
print(r1.name) 结果:
12300
WC

调用cal_area函数相当于调用了直接参数,将背后的操作隐藏起来,@property可以封装你函数的逻辑,像是调用普通逻辑一样s

三、类方法

类有属性,包含数据属性跟函数属性,

class Room:
tag=1
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh
@property
#@property加上这个后面调用不用加括号
def cal_area(self):
######print('%s 住的 %s 总面积是%s' %(self.owner,self.name,self.width*self.length))
return self.width*self.heigh
def test(self,name):
print('from test',self.name)
@classmethod
#cls接收一个类名,类方法,自动传类名参数
def tell_info(cls,x):
print(cls)
print('----->',cls.tag,x) print(Room.tag)
Room.tell_info(10) 结果:
1
<class '__main__.Room'>
-----> 1 10

四、静态方法

class Room:
tag=1
def __init__(self,name,owner,width,length,heigh):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.heigh=heigh @property
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length @classmethod
def tell_info(cls,x):
print(cls)
print('--》',cls.tag,x)#print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag)
@staticmethod
#相当于类的工具包
def wash_body(a,b,c):
print('%s %s %s正在洗澡' %(a,b,c)) r1=Room('厕所','alex',100,100,100000)
r1.wash_body('charon','pluto','to')
Room.wash_body('charon','pluto','to') 结果:
charon pluto to正在洗澡
charon pluto to正在洗澡

@staticmethod静态方法只是名义上归属类管理,不能使用类变量和实例变量,是类的工具包

五、小节

静态属性:@property把函数封装成一个数据属性的形似,让外部调的时候看不到内部逻辑,因为可以传递self。所有静态属性既可以访问实例属性,又可以访问类属性

类方法:@classmethod函数默认参数写成cls,cls代表类。类能访问类的数据属性,类的函数属性,不能访问到实例的属性,实例是类里面(下的)包的东西,不能从外面作用域访问的里面作用域

静态方法:@staticmethod静态方法只是名义上归属类管理,不能使用类变量和实例变量,是类的工具包,不能访问类属性,也不能访问实例属性

六、组合

class School:
def __init__(self,name,addr):
self.name=name
self.addr=addr class Course:
def __init__(self,name,price,preiod,school):
self.name=name
self.price=price
self.preiod=preiod
self.school=school s1=School('oldboy','BJ')
s2=School('oldboy','NJ')
s3=School('oldboy','DJ') #c1=Course('linux',10,'1h',s1)
#print(c1.__dict__)
##print(s1)
#print(c1.school.name)
msg='''
1.oldboy BJschool
2.oldboy NJschool
3.oldboy DJschool
'''
while True:
print(msg) menu={
'1':s1,
'2':s2,
'3':s3,
} choice=input('>>>>>choine school:') school_obj=menu[choice] name = input('>>>>course name:')
price = input('>>>>>>>course price:')
preiod = input('>>>>>course preiod:') new_course=Course(name,price,preiod,school_obj)
print('course [%s] of [%s] school' % (new_course.name1,new_course.school.name))

day25 Python的更多相关文章

  1. day25 Python四个可以实现自省的函数,反射

    python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) 四个可以实现自省的函数 下列方法适用于类和对象(一切皆对象,类本身也是一个对象) ...

  2. day25 python学习 继承,钻石继承 多态

    ---恢复内容开始--- 通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' d ...

  3. day25 python学习 继承,钻石继承

    通过一个列子认识父类和子类中,子类的如何实现对父类默认属性调用,同时拥有自己的属性,如何在子类中调用父类的方法,class Ainmal: country='afdas' def __init__(s ...

  4. day25 Python __setattr__

    #__getattr__只有在使用点调用属性且属性不存在的时候才会触发 class Foo: x=1 def __init__(self,y): self.y=y def __getattr__(se ...

  5. python 全栈开发,Day25(复习,序列化模块json,pickle,shelve,hashlib模块)

    一.复习 反射 必须会 必须能看懂 必须知道在哪儿用 hasattr getattr setattr delattr内置方法 必须能看懂 能用尽量用__len__ len(obj)的结果依赖于obj. ...

  6. Python:Day25 成员修饰符、特殊成员、反射、单例

    一.成员修饰符 共有成员 私有成员,__字段名,__方法 - 无法直接访问,只能间接访问 class Foo: def __init__(self,name,age): self.name = nam ...

  7. python面向对象之 封装(Day25)

    封装: 隐藏对象的属性和实现细节,仅对外提供公共访问方式 好处:1.将变化隔离 2.便于使用 3.提高复用性 4.提高安全性 封装原则: 1.将不需要对外提供的内容隐藏起来 2.把属性都隐藏,提供公共 ...

  8. python练习题-day25

    class Person: __key=123 def __init__(self,username,password): self.username=username self.__password ...

  9. python day25 正则表达式

    2019.4.30 S21 day25笔记总结 正则表达式 1. 正则表达式 re模块:re模块本身只是用来操作正则表达式的,和正则本身没关系. 正则表达式:是一种规则 匹配字符串的规则. 为什么要有 ...

随机推荐

  1. Linux下Python安装完成后如何使用pip命令

    一.很多读者Python安装完成之后,想要下载相关的包,例如:numpy.pandas等Python中这些基础的包,但是,发现pip根本用不了,主要表现在一下几种情况: 二.出现这种情况其实并不意外, ...

  2. BZOJ2783: [JLOI2012]树(树上前缀和+set)

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1215  Solved: 768[Submit][Status][Discuss] Descriptio ...

  3. leetcode-977. 有序数组的平方

    leetcode-977. 有序数组的平方 (来自 120周赛) 题意 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1 ...

  4. python爬虫从入门到放弃(九)之 Requests+正则表达式爬取猫眼电影TOP100

    import requests from requests.exceptions import RequestException import re import json from multipro ...

  5. 章节四、3-While循环-DoWhile语句

    一.while死循环 package introduction5; public class WhileDemo { public static void main(String[] args) { ...

  6. PyCharm 使用Github管理Django项目

    不管是对于教程代码免费分享的需要,还是项目开发过程中的版本管理,Github都是我们首选的开源代码仓库,如果你没有私有仓库,并且不用保护代码,那么将项目上传到Github上是最佳的选择. 关于如何使用 ...

  7. 大约当你拿捏的准世事的分寸时,你便会成功了。(NULL)

    (网络盗图)

  8. Uvision4创建工程

    创建工程 创建好工程会出现如下的界面 创建文件( .asm 或 .c ) 选择菜单栏中的File->New- 按 Ctrl+S 保存文件并填写文件名 添加到之前创建的工程中 选择Target1- ...

  9. linux (fedora 28) 制作启动U盘,启动盘

    最近需要安装一款Linux, 由于使用的计算机系统为 fedora 28, 所以只能在linux 制作U盘 使用 df 或者 fdisk -l 查看 U盘文件: Disk /dev/sdb: byte ...

  10. ping百度域名时的收获

    ping百度 你会发现ping www.baidu.com的时候,会转为ping www.a.shifen.com.但是ping baidu.com的时候却是普通的ip地址,而且ip地址还会变化.那么 ...