20/01/18 Python基础知识学习(7)

包与模块管理

  • 使用目的
    • 代码重用
    • 命名空间
    • 实现数据共享
  • 模块指令
    • import
    • from
    • from 模块名 import 功能名 as 新命名
    • 导入后想修改或使用最新的功能:
      import importlib
      importlib.reload(模块)
  • 步骤
    1. 找到模块文件
    2. 编译为字节码
    3. 运行模块文件
  • 搜索范围
    1. 程序主目录
    2. 环境变量
    3. 标准库
    4. 扩展库
    • init 只在被导入的时候被调用一次

面向对象编程

  • 目标:尽量符合人的思维的方式去分析问题,思考问题,解决问题
  • 步骤
    • OOA 面向对象分析
    • OOD 面向对象设计
    • OOP 面向对象编程
  • 实现
    1. 分析对象特征行为
    2. 写类描述对象模板
    3. 实例化,模拟过程

在这里插入图片描述
从日常人的思维习惯先分析好对象、属性特征、方法行为后,总结出他们之间的关系,然后写类(把分析对象变成代码)

实例:

class Book:
	def __init__(self,title,price,author, publisher,pubdate):   #初始化对象
		self.title=title
		self.price=price
		self.author=author
		self.publisher=publisher
		self.pubdate=pubdate
		
	def __repr__(self):         #如何定义对象
		return '<图书 {}>.format(self.title)'

	def printinfo(self):
		print('当前这本书的信息如下:')
		print('标题:{}'.format(self.title))
		print('定价:{}'.format(self.price))
		print('作者:{}'.format(self.author))
		print('出版社:{}'.format(self.publisher))
		print('出版事件:{}'.format(self.pubdate))

book1 = Book('C#经典',29.9, 'Tom','优品课堂',datetime.date(2016,3,1))

print(book1.title))
book.printinfo()
book1
发布了10 篇原创文章 · 获赞 0 · 访问量 165

猜你喜欢

转载自blog.csdn.net/weixin_44602323/article/details/104028919