面向对象

###一、面向对象编程
编程思想:
1.面向对象编程 –> 算法,逻辑
2.函数式编程 –> 函数
3.面向对象编程 –> 类和对象(生活)

###二、类的声明

####1.什么是类和对象
类 —> 是拥有相同属性和相同功能的对象的集合(抽象的)
对象 – > 就是类的实例(具体的)

####2.类的声明
格式:

1
2
3
class 类名(父类列表):
类的说明文档
类的内容

说明:
class –> python中声明类的关键字
类名 –> 标识符,不能是关键字;类名使用驼峰式命名,并且首字母必须大写;见名知义
驼峰式命名:如果一个名字有多个单词组成,第一个单词首字母小写,后面每个单词首字母大写:userName
PEP8命名规范:如果一个名字有多个单词组成,所有字母小写,后面每个单词之间用下划隔开:user_name
父类列表) —> 继承语法,让声明的类继承括号中的父类,这个结构可以省略,让当前类继承python的基类:object
: –> 固定结构
类的说明文档 –>注释,对类进行说明
类的内容 –> 包含属性(变量)和方法(函数)
方法:声明在类中的函数

1
2
3
4
5
6
7
8
9
10
# 声明Person类,
class Person:
""" 人类"""
def eat(self):
print('吃饭')
def sleep(self):
print('睡觉')
# 声明了Person类的p1
p1 = Person()
p2 = Person()

####3.对象的声明
格式:

1
2
3
4
对象名 = 类名()

对象名 -> 变量名
类名 - >必须是声明过的类

###三、对象方法

####类的内容
类的内容包含属性和方法,方法分为对象方法,类方法和静态方法

a.对象方法:直接声明在类中的函数就是对象方法。对象方法都有一个默认参数self,需要使用对象来调用
b.对象方法的调用:对象.方法名()。调用对象方法的时候,不要给默认参数self传参,系统会自动将当前对象传递给self。
c.self:谁调用当前的对象方法,self就指向谁。self就是当前类的对象,类的对象能做的事情,self都能做

###四、构造方法和init方法

####1.构造方法
构造方法就是函数名和类名一样的方法,作用是用来创建对象的。声明类的时候,系统会自动为这个类创建对应构造方法

创建对象的过程:调用构造方法在内存中开辟空间创建对象,并且会自动调用init方法去对这个对象进行初始化,最后将创建好的对象的地址返回

####2.init方法
对象方法
不需要手动调用,创建完对象后,会被自动调用

1
2
3
4
5
6
7
class Dog:
def __init__(self):
print(self)
print('init方法')

dog1 = Dog()
print(dog1)

####3.带参其他参数的init方法
_ _ init _ _方法的参数要通过构造方法来传。构造方法的实参,会传递给init方法的形参)

1
2
3
4
5
class Person:
def __init__(self, name, age):
print(name, age)

p1 = Person('小红',20)

###五、对象的属性
类的内容包含属性和方法,属性又分为对象属性和类的字段

属性:用来在类中去保存数据的变量。
对象属性:属性的值会因为队形不同而不同,这种属性需要声明成对象属性,例如:人的名字,人的年龄等

####1.对象属性的声明
a.必须声明在_ _ init _ _方法中
b.声明的格式:self.属性名 = 初值

####2.使用对象属性:对象.属性

1
2
3
4
5
6
7
8
9
# 声明一个人类,要求有姓名,年龄和性别属性
class Persson:
def __init__(self):
self.name = '张三'
self.age = 18
self.sex = '男'

p1 = Persson()
print(p1.name)

###六、对象属性的增删查改

1
2
3
4
5
6
class Student:
def __init__(self,name1 = '', age1 = 0, study_id1 = '001'):
self.name = name1
self.age = age1
self.study_id = study_id1
stu1 = Student('小明')

####1.查 (获取对象属性的值)
方法一:对象.属性 —> 获取指定属性值,属性不存在会报错

1
2
print(stu1.name)
# print(stu1.name2) # AttributeError: 'Student' object has no attribute 'name2'

方法2:getattr(对象,属性名,默认值) –> 获取指定属性值,如果设置了默认值当属性不存在的时候不会报错,并且会将默认值作为结果。(如果没有设置默认值,属性不存在还是会报错)

1
2
print(getattr(stu1, 'name'))
print(getattr(stu1, 'name2', '张三'))

方法3:对象._ _ getattribute _ _(属性名) –> 获取指定属性值

1
2
print(stu1.__getattribute__('study_id'))
# print(stu1.__getattribute__('study_id2')) # AttributeError: 'Student' object has no attribute 'study_id2'

####2.增和改(给对象添加属性和修改属性)
注意;给对象添加属性只能添加到当前对象中。不会影响当前类的其他对象
方法1:对象.属性 = 值 (属性不存在的时候增加,存在的时候就是修改)
方法2:setattr(对象,属性名,属性值)
方法3:对象._ _ setattr_ _(属性名,属性值)

1
2
3
4
5
6
7
8
9
10
11
12
# 增加
stu1.sex = '男'
print(stu1.sex)
# 修改
stu1.name = '李四'
print(stu1.name)
# 修改
setattr(stu1, 'name', '娜美')
print(stu1.name)
# 添加
setattr(stu1, 'name2', '宝儿姐')
print(stu1.name2)

####3.删(删除对象属性)
注意:删除只删除当前对象的属性,对当前类的其他对象没有影响
方法1:del 对象.属性
方法2:delattr(对象,属性名)
方法3:对象._ _ delattr_ _(属性名)

###七、对象的使用
python中所有的数据都是对象,所有的变量存储的都是对象的地址

1
2
3
4
5
6
7
8
class Student:
def __init__(self,name,age, score):
self.name = name
self.age = age
self.score = score
# 定制当前类对象的打印格式,函数的返回值必须是字符串
def __str__(self):
return str(self.__dict__)

1.将对象给别的变量赋值

1
2
3
4
5
6
7
stu1 =Student('xiaohua',18,90)
stu2 = stu1 # 赋对象地址
stu3 = copy.copy(stu1) # 产生新的对象,将新的地址赋值

stu1.name = '张三'
print(stu2.name)
print(stu3.name)

2.将对象作为列表的元素

1
2
3
4
5
6
7
8
9
students = [Student('小明', 23, 89), stu3, Student('小红', 20, 67)]
# 找到列表students中成绩最好的学生
max1 = students[0].score
name = students[0].name
for stu in students:
if stu.score > max1:
max1 = stu.score
name = stu.name
print(name,max1)

练习1:对列表中的学生按年龄从小到大排序

1
2
3
4
5
students.sort(key=lambda item: item.age)
for stu in students:
print(stu)
max1 = max(students, key=lambda item:item.score)
print('max:', max1)

练习2:1.根据姓名查找指定学生的信息。2.根据姓名修改指定的学生的年龄

1
2
3
4
5
6
name = input('请输入学生的姓名:')
age = input('请输入你要修改的年龄:')
for stu in students:
if stu.name == name:
stu.age = age
print(stu)

###八、slots魔法和类的字段

####1.类的字段
属性:对象属性,类的字段
类的字段:声明在类里面函数的外面的变量就是类的字段.使用的时候要通过类来使用:类.字段

####2._ _ slots _ _:
用来约束当前类的对象的属性有哪些

1
2
3
4
5
6
7
8
9
10
11
12
13
class Dog:
# num就是类的字段
num = 10
__slots__ =('color', 'name', 'type')
def __init__(self, color, name, type):
self.color = color
self.name = name
self.type = type
print(Dog.num)
Dog.num = 100
print(Dog.num)

dog1 = Dog('黄色', '大黄', '金毛')

###九、内置类属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Person():
"""人类"""
# 类的字段
num = 61

def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex

def run(self):
print('%s在跑' % self.name)
def __str__(self):
return '{'+self.__class__.__module__+'.'+self.__class__.__name__+\
' object at '+ hex(id(self))+'}'
p1 = Person('小明', 18, '男')

####1、_ _ name _ _
类._ _ name _ _ – >获取当前类的名字

1
print(Person.__name__,type(Person.__name__))

####2._ _ doc _ _
类._ _ doc _ _ -> 获取类的说明文档

1
2
print(Person.__doc__)
print(int.__doc__)

####3._ _ class _ _
对象._ _ class _ _ ->获取对象的类

1
2
3
4
my_class = p1.__class__
p2 = my_class('小东', 20, '男')
print(p2.name)
print(my_class.num)

####4._ _ dict _ _
类._ _ dict _ _ –> 获取当前类的所有类的字段和其对应的值(了解)
对象. _ _ dict _ _ –>获取去当前对象所有的属性和其对应的值,以字典的形式返回

1
2
print(Person.__dict__)
print(p1.__dict__)

####5._ _ module _ _
类._ _module _ _ -> 获取当前类所在的模块,正在执行的模块为_ _ main _ _

1
print(Person._ _ module _ _)

####6.._ _ bases _ _
类._ _ bases _ _ –> 获取当前类的父类,返回的是一个类

作业

###1. 声明一个电脑类
属性:品牌、颜色、内存大小
方法:打游戏、写代码、看视频 def
a.创建电脑类的对象,然后通过对象点的方式获取、修改、添加和删除它的属性
b.通过attr相关方法去获取、修改、添加和删除它的属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Computer:
def __init__(self, brand, color, RAM):
self.brand = brand
self.color = color
self.RAM = RAM
def playgame(self):
print(self.brand+'用来打游戏')

def write_word(self):
print(self.brand+'用来写代码')
def watch(self):
print(self.brand+'用来看电视')
# 通过对象点的方式获取,修改,添加,修改和删除它的属性
f1 = Computer('联想','白色','8G+256G')
print(f1.brand, f1.color, f1.RAM)
f1.kind = '商务笔记本'
f1.color = '黑色'
print(f1.brand, f1.color, f1.RAM, f1.kind)
del f1.kind
print(f1.brand, f1.color, f1.RAM)
# 通过attr相关方法去获取、修改、添加和删除它的属性
f2 = Computer('神州', '黑色', '16G+256G')
print(getattr(f2, 'brand'), getattr(f2, 'color'), getattr(f2, 'RAM'),)
setattr(f2, 'RAM', '16G+256G')
print(getattr(f2, 'brand'),getattr(f2,'color'),getattr(f2,'RAM'))
setattr(f2,'kind', '游戏本')
print(getattr(f2, 'brand'), getattr(f2, 'color'), getattr(f2, 'RAM'),getattr(f2,'kind'))
delattr(f2, 'kind')
print(getattr(f2, 'brand'), getattr(f2, 'color'), getattr(f2, 'RAM'),getattr(f2,'kind','种类不存在'))

f1.playgame()
f1.write_word()
f1.watch()

2.声明一个人的类和狗的类:

狗的属性:名字、颜色、年龄
狗的方法:叫唤
人的属性:名字、 年龄、狗
人的方法:遛狗
a.创建人的对象名字叫小明,让他拥有一条狗 ,然后让小明去遛狗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Dog:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
def dog(self):
print(self.name+'在叫唤')
class Person:
def __init__(self, name: str, age: int, dog: Dog):
self.name = name
self.age = age
self.dog = dog
def walk_the_dog(self):
self.dog = Dog('小黑', '黑色', 3)
print(self.name+'在遛'+self.dog.name)
f1 = Person('小明', 18, '小黑')
f1.walk_the_dog()

3.声明一个矩形类:

属性: 长、宽
方法:计算周长和面积
a.创建不同的矩形,并且打印其周长和面积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class  Rectangle:
def __init__(self, long, width):
self.long = long
self.width = width
def count(self):
sum1 = (self.long+self.width)*2
area = self.long*self.width
print('矩形周长是:%s\t矩形面积是:%s' % (sum1,area))
f1 = Rectangle(5,6)
f2 = Rectangle(3,5)
f3 = Rectangle(4,9)
# 面积和州长
f1.count()
f2.count()
f3.count()

4.创建一个学生类:

属性:姓名,年龄,学号,成绩
方法:答到,展示学生信息
创建一个班级类: 属性:学生,班级名
方法:添加学生,删除学生,点名, 获取班级中所有学生的平均值, 获取班级中成绩最好的学生

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Students:
def __init__(self, name, age, stu_id, score):
self.name = name
self.age = age
self.stu_id = stu_id
self.score = score
# 达到
def answer(self):
print('%s:到!' % self.name)
# 展示学生信息
def show(self):
print('姓名:%s 年龄:%s 学号:%s 成绩:%s' % (self.name, self.age, self.stu_id, self.score))
class Class:
def __init__(self,students = [], name=''):
self.students = students
self.class_name = name
# 添加学生
def add_stu(self,student = None):
self.students.append(student)
# 删除学生
def del_stu(self,student):
self.students.remove(student)
# 点名
def call_name(self,name):
for stu in self.students:
print(stu.name)
# 平均值
def average(self):
sum1 = 0
n = 0
for stu in self.students:
sum1 += stu.score
n +=1
ave = sum1/n
return ave
# 求成绩最好的
def max1(self):
max1 = self.students[0].score
name = self.students[0].name
for stu in self.students:
if stu.score > max1:
max1 = stu.score
name = stu.name
print(name, max1)

#创建三个学生
f1 = Students('小明',18,'0001',90)
f2 = Students('小红',20,'0002',77)
f3 = Students('张三',22,'0003',80)
#将学生放入班级列表
student = [f1, f2, f3]
#创建一个班级
f4 = Class(student, 'python1807')
print(f1.__dict__)
print(f4)
#增加学生4李四
student1 = Students('李四',25,'0004',74)
f4.add_stu(student1)
print(student1.__dict__)
#删除学生1小明
f4.del_stu(f1)
print(f1.show.__dict__)
#点名
f4.call_name(student)
#求一个班的平均成绩
print(f4.average())
#求一个班成绩最好的学生
f4.max1()