一、面向对象的编程思想
面向过程编程
核心是“过程”二字,过程是解决问题的步骤,即先干啥再干啥后干啥
基于该思想写程序就是在设计一条条的流水线
优点:复杂的问题流程化、进而简单化
缺点:扩展性差
面向对象编程
核心是“对象”二字,对象是一个用来盛放数据与功能的容器
基于该思想写程序就是在整合程序
优点:可扩展性强
缺点:编程的复杂度高
二、类与对象
如何基于面向对象的思想写程序——基础版
# 学生的数据
stu_name = "egon"
stu_age = 18
stu_gender = "male"
# 学生的功能
def choose(name,age,gender):
print('%s:%s:%s 正在选课' %(name,age,gender))
choose(stu_name,stu_age,stu_gender)
基于面向对象的思想改写——简单改进版(把数据和功能放进一个字典里,以键值对的方式展现)
def choose(stu_self):
print('%s:%s:%s 正在选课'%(stu_self["stu_name"],stu_self["stu_age"],stu_self["stu_gender"]))
stu_obj = {
"stu_name":"egon",
"stu_age":18,
"stu_gender":"male",
"choose":choose,
}
print(stu_obj["stu_name"])
stu_obj["choose"](stu_obj)
python提供专门的语法来更漂亮地实现面向对象编程——进阶版
'''
学生对象1
数据:
名字 = "冯疯子
年龄 = 18
性别 = "famale"
学生对象2
数据:
名字 = "郭靖
年龄 = 19
性别 = "male"
学生对象3
数据:
名字 = "大雕"
年龄 = 200
性别 = "male"
学生的类
相同的数据
学校 = "oldboy"
相同的功能
选课
'''
类的使用: 类体代码会在类定义阶段立刻执行,然后将产生的名字都丢到类的名称空间中
class Student: # 类名推荐用驼峰体,全大写或全小写
# 相同的数据
school = "oldboy"
# 相同的功能
def choose(self):
print("正在选课")
stu_obj1 = Student()
stu_obj2 = Student()
stu_obj3 = Student()
stu_obj1.name = "yue"
stu_obj1.__dict__["name"] = "yue"(是一个字典)
stu_obj1.age = 18
stu_obj1.age = 18
stu_obj1.gender = "male"
stu_obj2.name = "xue"
stu_obj2.age = 19
stu_obj2.gender = "female"
stu_obj3.name = "yoyo"
stu_obj3.age = 20
stu_obj3.gender = "male"
print(stu_obj1.__dict__) # 以字典的形式打印出来
print(stu_obj2.__dict__)
print(stu_obj3.__dict__)
print(stu_obj1.name)
stu_obj1.school = "xxx"
print(stu_obj1.school)
结果:xxx
print(Student.__dict__)
stu_obj1 = Student()
stu_obj2 = Student()
stu_obj3 = Student()
三、初始化方法
例一:基本
class Student:
school = "oldboy"
def choose(self):
print('正在选课')
stu_obj1 = Student()
stu_obj2 = Student()
stu_obj3 = Student()
def __init__(obj,x,y,z):
obj.name = x
obj.age = y
obj.gender = z
__init__(stu_obj1,"冯疯子",18,"male")
__init__(stu_obj2,"郭靖",19,"female")
__init__(stu_obj3,"大雕",200,"male")
例二:进阶
class Student:
school = "oldboy"
# 空对象
def __init__(obj,x,y,z):
obj.name = x
obj.age = y
obj.gender = z
# return None 不能有返回值,有的话只能返回None
def choose(self):
print('%s 正在选课' %self.name)
调用类:
1、 创建一个空对象与类相关
2、 把空对象、 "冯疯子" ,18, "male" 一起传给 __init__方法,完成对象的初始化
3、 赋值符号把初始化好的对象内存地址绑定变量名 stu_obj1
stu_obj1 = Student("冯疯子",18,"male")
stu_obj2 = Student("郭靖",19,"female")
stu_obj3 = Student("大雕",200,"male")
print(Student.__dict__)
print(Student.__dict__)
stu_obj3.choose()
stu_obj2.choose()
stu_obj1.choose()
四:属性查找
优先级:先从对象的字典里找,没有,再去类的字典中找
class Student:
school = "oldboy"
stu_obj1 = Student()
print(stu_obj1.school)
#oldboy
类中定义的数据是直接共享给所有对象使用的
class Student:
school = "oldboy"
def __init__(obj,x,y,z):
obj.name = x
obj.age = y
obj.genden = z
def choose(self):
print("%s 正在选课" %self.name )
stu_obj1 = Student("冯疯子",18,"female")
stu_obj2 = Student("郭靖",19,"male")
stu_obj3 = Student("大雕",200,"male")
print(id(stu_obj1.school))
print(id(stu_obj2.school))
print(id(stu_obj3.school))
print(id(Student.school))
#共用一个内存地址
Student.school = "xxx"
print(stu_obj1.school)
print(stu_obj2.school)
print(stu_obj3.school)
print(Student.choose)
print(stu_obj1.choose())
print(stu_obj2.choose)
print(stu_obj3.choose)
类中定义的函数是绑定给所有对象用的,绑定给谁就应该由哪个对象来调用
对象.绑定方法()会把对象当作第一参数传入
类.函数()就是一个函数的玩法,没有自动传参的效果
class Student:
school = "oldboy"
def __init__(obj,x,y,z):
obj.name = x
obj.age = y
obj.gender = z
def choose(self):
print(' %s岁的%s正在选课 ' %(self.age,self.name))
stu_obj1 = Student("yue",18,"male")
stu_obj2 = Student("xue",19,"female")
stu_obj3 = Student("yoyo",20,"male")
stu_obj1.choose()
stu_obj2.choose()
stu_obj3.choose()
# 自动调用对象stu_obj里的内容
#18岁的yue正在选课
#19岁的xue正在选课
#20岁的yoyo正在选课