目录
- (一)包和模块的管理
- (二)面向对象
(一)包和模块的管理
模块:模块可以理解为一个.py文件
包:一个包包含多个目录(可以看做一个特殊的目录)
包下边有个__init__.py模块,只有在包被第一次导入时才会被调用
主要几个关键字:
import:导入某个包
import math
from:从某个包中导入某个变量或方法-
from models import page 【as 别名】
导入的步骤:
(1)找到模块文件
- 首先从当前程序的主目录
- 环境变量
- 标准库
- 扩展库
(2)编译文件为字节码
(3)运行字节码
专门用于处理调入的模块importlib(只能导入模块)
import importlib
importlib.reload(models)
(二)面向对象
面向对象的步骤:
- 面线对象分析OOA
- 面向对象设计OOD
- 面向对象编程OOP
面向对象的实现:
- 分析对象的特征和行为
- 写类描述对象末模板
- 实例化,模拟过程
封装
- 构造函数
- 静态成员
- 静态方法
import datetime
class Book:
count = 0
def __init__(self,title,price,author,publisher,pubdate):
self.title = title
self.price = price
self.author = author
self.publisher = publisher
self.pubdate = pubdate
Book.count += 1
def print_info(self):
print('标题:{}'.format(self.title))
print('价格:{}'.format(self.price))
print('作者:{}'.format(self.author))
print('出版社:{}'.format(self.publisher))
print('出版日期:{}'.format(self.pubdate))
def __repr__(self):
return '<图书:{}>'.format(self.title,id(self))
def __str__(self):
return '<图书:{} at {}>'.format(self.title,id(self))
def __del__(self):
Book.count -= 1
# 虽然是static,但是调用的时候也必须传入类的实例对象
def cls_method(cls):
print('类函数')
# 调用时不用传入类的实例对象,注意对象不能调用该方法
def cls_method1():
print('静态函数与实例无关')
# 调用时不用传入类的实例对象,对象也调用该方法
@staticmethod
def cls_method2():
print('静态函数与实例无关')
book1 = Book('Python',123.0,'Tom',' QINGHUA',datetime.date(2019, 1, 1))
book1.print_info()
print(book1)
print('===================================')
book2 = Book('Java',123.0,'Tom',' QINGHUA',datetime.date.today())
book2.print_info()
print(book2)
print('Book.count = {}'.format(Book.count))
del(book2)
print('Book.count = {}'.format(Book.count))
print('===================================')
print(book1.count)
book1.count = 10
print(book1.count)
print(Book.count)
Book.cls_method(book1)
Book.cls_method1()
book1.cls_method2()
------------------------------------------------------------------------
标题:Python
价格:123.0
作者:Tom
出版社: QINGHUA
出版日期:2019-01-01
<图书:Python at 2384219167656>
===================================
标题:Java
价格:123.0
作者:Tom
出版社: QINGHUA
出版日期:2020-01-03
<图书:Java at 2384219167768>
Book.count = 2
Book.count = 1
===================================
1
10
1
类函数
静态函数与实例无关
静态函数与实例无关
属性
class Student:
def __init__(self,name,birthdate):
self.name = name
self.birthdate = birthdate
@property
def age(self):
return datetime.date.today().year-self.birthdate.year
@age.setter
def age(self,value):
raise AttributeError("禁止赋值年龄")
@age.deleter
def age(self):
raise AttributeError("禁止删除年龄")
继承、多态
import datetime
class Employee:
def __init__(self,department,name,birthdate,salary):
self.department = department
self.name = name
self.birthdate = birthdate
self.salary = salary
def __str__(self):
return '<员工:{}>'.format(self.name)
@property
def age(self):
return datetime.date.today().year - self.birthdate.year
def working(self):
print('员工{}在工作'.format(self.name))
class Programmer(Employee):
def __init__(self,department,name,birthdate,salary,specility,project):
super().__init__(department,name,birthdate,salary)
self.specility = specility
self.project = project
def working(self):
print('程序员{},在开发项目:{}'.format(self.name,self.project))
class HR(Employee):
def __init__(self,department,name,birthdate,salary,qualification_level=1):
Employee.__init__(self,department,name,birthdate,salary)
self.qualification_level = qualification_level
def working(self):
print('HR{},正在面试新员工'.format(self.name))
p = Programmer('技术部','Tom',datetime.date(1993,12,2),10000,'Python',"大数据")
print(p.age)
hr = HR('HR','Tom',datetime.date(1993,12,2),10000)
#多态:
hr.working()
p.working()