面向对象:静态属性,静态方法,组合,继承,衍生,继承之mro线性顺序列表,面向对象综合实例
静态属性,静态方法,组合,继承,衍生,继承之mro线性顺序列表,面向对象综合实例
1.静态属性(附有装饰器)
class Room:
def __init__(self,name,owner,width,length,height):
self.name=name
self.owner=owner
self.width=width
self.length=length
self.height=height
@property
def cal_area(self):
return self.width*self.length
r1=Room("厕所","alex",100,100,100000)
r2=Room("公共厕所","yuanhao",1,1,1)
print("%s住的%s总面积是%s" %(r1.owner,r1.name,r1.width*r1.length))
print("%s住的%s总面积是%s" %(r2.owner,r2.name,r2.width*r2.length))
# r1.cal_area
# r2.cal_area
print(r1.cal_area)
print(r2.cal_area)
print(r1.name)
print(r2.name)
利用常规类实例计算值和利用装饰器封装函数,调用取值,让用户觉察不到数据类型的处理方法
class Volume:
def __init__(self,length,width,height):
self.length=length
self.width=width
self.height=height
@property
def cal_volume(self):
return self.length*self.width*self.height
v1 = Volume(10,20,30)
print("长宽高分别是%s,%s,%s" %(v1.length,v1.width,v1.height))
print("体积是%s" %(v1.length*v1.width*v1.height))
res = v1.cal_volume
print("体积是%s" %res)
2.静态方法
#静态方法:
class Room:
tag = 1
def __init__(self,name,owner,width,length,height):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.height = height
@property
def cal_area(self):
return self.width*self.length
def test(self):
print("from test",self.name) @classmethod #类方法,只用于实例属性
def tell_info(cls,x): #cls为函数本身的参数,不用人为传参
print("---->",cls.tag,x)
#类方法能否调用实例属性?可以
print(Room.tag)
# r1 = Room("toilet", "alex",100,100,10000)
# Room.tell_info(r1) #类方法:只和类捆绑,不和任何实例捆绑
Room.tell_info(10) #默认Room=cls
r1 = Room("toilet", "alex",100,100,10000)
r1.tell_info(100) #实例调用可传入类
静态方法
class Room:
tag = 1
def __init__(self,name,owner,width,length,height):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.height = height
@property #封装逻辑
def cal_area(self):
return self.length * self.width
@classmethod#自动传入类
def tell_info(cls,x): #类,函数,不能访问实例属性,静态类变量只是名义上的归属类管理,不能使用类变量和实例变量,是类的工具包
print(cls)
print("-->",cls.tag,x)
@staticmethod
def wash_body(a,b,c):
print("%s %s %s正在洗澡"%(a,b,c))
def test(x,y):
print(x,y)
print(Room.__dict__)
r1 = Room("厕所","alex",100,100,100000) #定义类
print(r1.__dict__)
3.组合
# 组合
class Hand:
pass
class Foot:
pass
class Trunk:
pass
class Head:
pass
class Person:
def __init__(self,id_num,name):
self.id_num = id_num
self.name = name
self.hand = Hand()
self.foot = Foot()
self.trunk = Trunk()
self.head = Head()
# p1 = Person("1111","alex")
# print(p1.__dict__)
class School:
def __init__(self,name,addr):
self.name = name
self.addr = addr
def zhao_sheng(self):
print("%s 正在招生" %self.name)
class Course:
def __init__(self,name,price,period,school):
self.name = name
self.price = price
self.period = period
self.school = school
s1 = School("oldboy","peking")
s2 = School("oldboy","nanjing")
s3 = School("oldboy","Tokyo")
c1 = Course("linux",10,"1h",s1)
print(c1.__dict__)
print(c1.school)
print(s1)
print(c1.school.name) #----------------------------------------------选课系统
class Hand:
pass
class Foot:
pass
class Trunk:
pass
class Head:
pass
class Person:
def __init__(self,id_num,name):
self.id_num = id_num
self.name = name
self.hand = Hand()
self.foot = Foot()
self.trunk = Trunk()
self.head = Head()
# p1 = Person("1111","alex")
# print(p1.__dict__)
class School:
def __init__(self,name,addr):
self.name = name
self.addr = addr
def zhao_sheng(self):
print("%s 正在招生" %self.name)
class Course:
def __init__(self,name,price,period,school):
self.name = name
self.price = price
self.period = period
self.school = school
s1 = School("oldboy","peking")
s2 = School("oldboy","nanjing")
s3 = School("oldboy","Tokyo")
c1 = Course("linux",10,"1h",s1)
msg = '''
1 老男孩 北京校区
2 老男孩 南京校区
3 老男孩 东京校区
'''
while True:
print(msg)
menu = {
"":s1,
"":s2,
"":s3
}
choice = input("选择学校>>")
school_obj = menu[choice] name = input("课程名>>: ")
price = input("课程费用>>: ")
period = input("课程周期>>: ") new_course = Course(name,price,period,school_obj)
print("磕碜【%s】 属于【%s】学校" %(new_course.name,new_course.school.name)) #组合方法_创建3个类:课程,老师,课程关联老师,老师管理课程
#组合应用场合:两个类本质上没有共同点,但是有关联的
#----------------------------------------------------------------------------
class Hand:
pass
class Foot:
pass
class Trunk:
pass
class Head:
pass
class Person:
def __init__(self,id_num,name):
self.id_num = id_num
self.name = name
self.hand = Hand()
self.foot = Foot()
self.trunk = Trunk()
self.head = Head()
# p1 = Person("1111","alex")
# print(p1.__dict__)
class School:
def __init__(self,name,addr):
self.name = name
self.addr = addr
def zhao_sheng(self):
print("%s 正在招生" %self.name)
class Course:
def __init__(self,name,price,period,school):
self.name = name
self.price = price
self.period = period
self.school = school
s1 = School("oldboy","peking")
s2 = School("oldboy","nanjing")
s3 = School("oldboy","Tokyo")
c1 = Course("linux",10,"1h",s1)
print(c1.__dict__)
print(c1.school)
print(s1)
print(c1.school.name)
4.不同类组合的实例(动态输出多个组合结果)
class Znj:
def __init__(self,word1,word2):
self.word1 = word1
self.word2 = word2
class School:
def __init__(self,name,addr,course):
self.name = name
self.addr = addr
self.course = course
self.znj = Znj
def zhao_sheng(self):
print("%s 正在招生" %self.name)
def Znj(self):
print("想对znj说一句%s" %self.znj)
class Course:
def __init__(self,name,price,period,num):
self.name = name
self.price = price
self.period = period
self.num = num
c3 = Znj("","")
c1 = Course("linux",10,"1h",c3)
s1 = School("oldboy","南京",c1)
s2 = School("girl",24,c1)
print(c1.num.word1,c1.num.word2) #实现不同类组合!
print(c3.__dict__)
print(s1.__dict__)
print(s2.__dict__)
print(s1.course.name) #类组合就是在实例中添加另一个实例,作为参数传到类中
5.继承
class Dad:
"this is a dad class"
money = 10
def __init__(self,name):
print("dad")
self.name = name
def hit_son(self):
print("%s 正在打儿子" %self.name)
class Son(Dad):
money = 10000008
def __init__(self,name,age):
self.name = name
self.age = age
def hit_son(self):
print("from son class")
Son.hit_son("self")
print(Dad.__dict__)
print(Son.__dict__)
s1 = Son("alex",18)
s1.hit_son() #实例调用hit_son()
Son.hit_son("self") #类调用hit_son()
print(Dad.money)
print(s1.name)
print(s1.__dict__) #实例字典
#父类在字典中包括更全key,value值
#子类在字典中缺少部分父类拥有的key,value值
6.继承与派生
什么时候用继承:
a)当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好。
b)当类之间有很多相同的功能,提取这些共同的功能叫做基类,用继承比较好。
继承有两种含义:1继承基类的方法,并作出自己的改变或者扩展(代码可重用);2接口继承:声明某个子类兼容与某基类,定义一个接口类,子类继承接口类,并且实现接口中定义的方法
接口技术:定义父类,规定子类必须读或写,否则不能调用实例。
#接口实际是函数
#接口继承是定义一个基类,基类中实现自己函数(装饰器)
# 子类中必须实现父类中方法,否则不可实例化
import abc
class All_file(metaclass = abc.ABCMeta):
@abc.abstractmethod
def read(self):
print("all_file read")
@abc.abstractclassmethod
def write(self):
print("all_file write")
class cdrom(All_file):
def read(self):
print("cdrom read")
def write(self):
print("cdrom write")
class disk(All_file):
def read(self):
print("disk read")
def write(self):
print("disk write")
class mem(All_file):
def read(self):
print("mem read")
def write(self):
print("mem write")
m1 = mem()
m1.read()
m1.write()
7.继承顺序值mro线性顺序列表
class A:
def test(self):
print("A")
pass
class B(A):
def test(self):
print("B")
pass
class C(A):
def test(self):
print("C")
pass
class D(B):
def test(self):
print("D")
pass
class E(C):
def test(self):
print("E")
pass
class F(D,E):
def test(self):
print("D,E")
pass
f1 = F() #实例化
f1.test() #实例化调用函数 继承顺序 F->D->B->E->C->A
8.PY3新式类和PY2经典类
class A:
def test(self):
print("A")
pass
class B(A):
# def test(self):
# print("B")
pass
class C(A):
def test(self):
print("C")
pass
class D(B):
# def test(self):
# print("D")
pass
class E(C):
# def test(self):
# print("E")
pass
class F(D,E):
# def test(self):
# print("D,E")
pass
f1 = F() #实例化
f1.test() #实例化调用函数 新式类继承顺序 F->D->B->E->C->A
# print(F.__mro__) #新式类方法
f1 = F()
f1.test() #python2 经典类继承顺序 F->D->B->A->E->C
9.在子类中调用父类
class Vehicle:
Country = "China"
def __init__(self,name,speed,load,power):
self.name = name
self.speed = speed
self.load = load
self.power = power
# self.line = line
def run(self):
print("开动了")
class Subway(Vehicle):
def __init__(self,name,speed,load,power,line):
Vehicle.__init__(self,name,speed,load,power)
self.line = line
def show_info(self):
print(self.name,self.line)
def run(self):
Vehicle.run(self)
Vehicle.run(self)
Vehicle.run(self)
Vehicle.run(self)
print('%s 开动啦' %self.name)
line13 = Subway("13号线","30m/s","","电",13)
line13.show_info()
line13.run()
10.super调用父类方法
class Vehicle:
Country = "China"
def __init__(self,name,speed,load,power):
self.name = name
self.speed = speed
self.load = load
self.power = power
# self.line = line
def run(self):
print("开动了")
class Subway(Vehicle):
def __init__(self,name,speed,load,power,line):
# Vehicle.__init__(self,name,speed,load,power)
super().__init__(name,speed,load,power) #运行super函数,调用__init__函数,可以调用到父类函数
self.line = line
def show_info(self):
print(self.name,self.line)
def run(self):
# Vehicle.run(self)
super().run() #super()函数好处,1不用父类名 2不用传self参数
super().run()
super().run()
print('%s 开动啦' %self.name)
line13 = Subway("13号线","30m/s","","电",13)
line13.show_info()
line13.run()
11.学校类创建综合实例(序列化方式存取文件)
import pickle
class School:
def __init__(self,name,addr):
self.name = name
self.addr = addr
def save(self):
with open("school.db","wb") as f:
pickle.dump(self,f) #以序列化方式创建文件
class Course:
def __init__(self,name,price,period,school):
self.name = name
self.price = price
self.period = period
self.school = school
s1 = School("oldboy","北京")
s1.save()
# school_obj = pickle.load(open("school.db","rb"))
# print(school_obj.name,school_obj.db) #以上永久建立实例
#以下取出实例
import pickle
class School:
def __init__(self,name,addr):
self.name = name
self.addr = addr
def save(self):
with open("school.db","wb") as f:
pickle.dump(self,f) #以序列化方式创建文件
class Course:
def __init__(self,name,price,period,school):
self.name = name
self.price = price
self.period = period
self.school = school
school_obj = pickle.load(open("school.db","rb"))
print(school_obj.name,school_obj.addr)
12.补充:深度查找方法和广度查找方法
深度优先查找顺序:A-B-D-G-I-E-C-F-G
广度优先查找顺序:A-B-C-D-E-F-G-H-I
图2 广度优先和广度优先查找方法解释图
13.面向对象综合实例
#xxx.db文件没有生成,一个类对应一个序列的操作没有实现
import pickle
import hashlib
import time
def create_md5():
m = hashlib.md5()
m.update(str(time.time()).encode("utf-8"))
return m.hexdigest()
id=create_md5()
time.sleep(1)
id1=create_md5()
time.sleep(1)
id2=create_md5() #每一个ID作为标识,存储文件
print(id)
print(id1)
print(id2) class Base:
def save(self):
with open("school.db","wb") as f:
pickle.dump(self,f) class School(Base):
def __init__(self,name,addr):
self.id=create_md5()
self.name=name
self.addr=addr class Course(Base):
def __init__(self,name,school):
# self.price=price
# self.period=period
self.id=create_md5()
self.course_name=name
self.school=school class SchoolMember(Base):
"""
学习成员基类
"""
def __init__(self,name,age,sex,school):
self.id=create_md5()
self.name=name
self.age=age
self.sex=sex
self.school=school class Lecturer(Base):
def __init__(self,name,age,sex,school):
self.id=create_md5()
self.name=name
self.age=age
self.sex=sex
self.school=school s1=School('oldboy','北京')
s1.save()
s2=School('oldboy','上海')
s2.save()
# c1=Course('linux',10,'1h','oldboy 北京')
# c1=Course('linux',10,'1h',s1)
msg='''
1 老男孩 北京校区
2 老男孩 上海校区
'''
while True:
print(msg)
menu={
'北京':s1,
'上海':s2,
}
sch_choice=menu[input('学校>>: ')]
Course_name=input('课程名>>: ')
Course1=Course(Course_name,sch_choice)
# print(sch_choice,Course_name,Course1.school.name)
print('课程【%s】属于学校【%s】【%s】'% (Course1.course_name,Course1.school.name,Course1.school.addr)) sch_choice1 = menu[input('学校(学员)>>: ')]
sch_peo=SchoolMember("alex","","female",sch_choice1)
print("成员【%s】属于学校【%s】[%s]" %(sch_peo.name,sch_peo.school.name,sch_peo.school.addr)) sch_choice2 = menu[input('学校(教员)>>: ')]
lec = Lecturer("wusir", "", "male", sch_choice2)
print("教员【%s】属于学校【%s】[%s]" %(lec.name,lec.school.name,lec.school.addr))
school_obj = pickle.load(open("school.db","rb"))
print(school_obj.name, school_obj.addr)
f图2 面向对象综合实例作业要求
14.面向对象建立目录时文件夹分类:
bin:可执行文件
conf:配置文件,一般设置成setting.py
db:数据
lib:图书馆
log:日志模块
src:存放主逻辑
面向对象:静态属性,静态方法,组合,继承,衍生,继承之mro线性顺序列表,面向对象综合实例的更多相关文章
- typescript静态属性,静态方法,抽象类,多态
/* 1.vscode配置自动编译 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": "./js", 2.第二步 任务 ...
- python面向对象之静态属性/静态方法/类方法/组合
继续学习,不要松懈 #!/usr/bin/env python # coding:utf-8 class Campus: def __init__(self,name,addr,type): self ...
- PHP面向对象——静态属性和静态方法
静态属性 所谓静态属性,也就是这个属性对于这个类来说是唯一的,不管有多少个对象,只要它引用了一个静态对象,那么这些对象引用出来的值肯定是同一个. 静态变量不能使用->这种箭头符号,而是使用::这 ...
- ReactiveCocoa源码解析(四) Signal中的静态属性静态方法以及面向协议扩展
上篇博客我们聊了Signal的几种状态.Signal与Observer的关联方式以及Signal是如何向关联的Observer发送事件的.本篇博客继续上篇博客的内容,来聊一下Signal类中静态的ne ...
- ReactiveSwift源码解析(四) Signal中的静态属性静态方法以及面向协议扩展
上篇博客我们聊了Signal的几种状态.Signal与Observer的关联方式以及Signal是如何向关联的Observer发送事件的.本篇博客继续上篇博客的内容,来聊一下Signal类中静态的ne ...
- springboot中静态属性/静态方法从YAML(yml)读取配置属性
启动类添加注解@EnableConfigurationProperties import jnetman.session.SnmpPref; import org.springframework.bo ...
- day25、 静态属性、类方法、静态方法、组合、继承、
一. 静态属性.类方法.静态方法 1.1静态属性 class Room: def __init__(self,name,owner,width,length): self.name=name self ...
- java中静态属性和和静态方法的继承问题 以及多态的实质
首先结论是:java中静态属性和和静态方法可以被继承,但是没有被重写(overwrite)而是被隐藏. 静态方法和属性是属于类的,调用的时候直接通过类名.方法名完成的,不需继承机制就可以调用如果子类里 ...
- Python之路(第二十三篇) 面向对象初级:静态属性、静态方法、类方法
一.静态属性 静态属性相当于数据属性. 用@property语法糖装饰器将类的函数属性变成可以不用加括号直接的类似数据属性. 可以封装逻辑,让用户感觉是在调用一个普通的数据属性. 例子 class R ...
随机推荐
- day 14 三元运算符,列表字典推导式,递归,匿名函数,内置函数(排序,映射,过滤,合并)
一.三元运算符 就是if.....else..... 语法糖 前提:if和else只有一条语句 #原始版 cmd=input('cmd') if cmd.isdigit(): print('1') e ...
- git bash + gitee
使用Git Bash从Gitee上下载代码到本地以及上传代码到码云Git: https://www.cnblogs.com/babysbreath/p/7274195.html 指定克隆远端分支 ht ...
- return 返回值
# ### return 返回值 """ (1) return + 数据类型 : 将这个数据弹到函数的调用处,后面除了可以接六大标准数据类型之外,还可以返回类 对象 函数 ...
- task打印执行结果
使用debug输出task执行的register: - name: check extract session # script: /app/ansiblecfg/XXX/roles/test/tas ...
- TP5 model层 返回的对象转数组
打开 database.php 增加或修改参数'resultset_type' => '\think\Collection',即可连贯操作 model('user')->select()- ...
- reat + cesium。 实现 初始化时自动定位,鼠标移动实时展示坐标及视角高度, 淹没分析
只贴实现淹没分析这块的代码. import styles from './cesium.less'; import React from 'react'; import Cesium from 'ce ...
- 一道笔试题来理顺Java中的值传递和引用传递
题目如下: private static void change(StringBuffer str11, StringBuffer str12) { str12 = str11; str11 = ...
- C# 对结构体和指针的使用
//结构体的定义 [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] pub ...
- webpack基本配置
module: { rules: [ { test: /\.css$/, use: ['style-loader','css-loader?minimize'] } ] } 一.入门 loader可以 ...
- Codeforces 1105B:Zuhair and Strings(字符串水题)
time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...