一、两大编程思想
面向对象和面向过程
二、类和对象
2.1、类
类别,分门别类,物以类聚,人类,鸟类,动物类,植物类......
类是多个类似事物组成的群体的统称。能够帮助我们快速理解和判断事物的性质
2.2、数据类型
不同的数据类型属于不同的类
使用内置函数查看数据类型
2.3、对象
100、99、520都是int类之下包含的相似的不同个例,这个个例专业数语称为实例或对象
三、类和对象的创建
3.1、Python中一切皆对象
3.2、类的创建
class Student:
#Student为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写
pass
#Python中一切皆对象Student是对象吗?内存有开空间吗?
print(id(Student)) #2926222013040
print(type(Student)) #<class 'type'>
print(Student) #<class '__main__.Student'>
3.3、类的组成
类属性;实例方法;类方法;静态方法
class Student:
native_pace='吉林' #直接写在类里的变量,称为类属性
def __init__(self,name,age):
self.name=name #self.name 称为实体属性 ,进行了 一个赋值的操作,将局部变量的name的值赋给实体属性
self.age=age
#实例方法
def eat(self):
print('学生在吃饭...')
#静态方法
@staticmethod
def method():
print('我使用了statticmethod进行修饰,所以我是静态方法')
#类方法
@classmethod
def cm(cls):
print('我是类方法,因为我使用了classmethod进行修饰')
#在类之外定义的称为函数,在类之内定义的称为方法
def drink():
print('喝水')
3.4、对象的创建
1、对象的创建又称为类的实例化
2、语法:
实例名=类名()
3、例子:
#创建Student类的对象
stu1=Student('jack',20)
print(id(stu1))
print(type(stu1))
print(stu1)
print('---------------------')
print(id(Student)) #Student是类的名称
print(type(Student))
print(Student)
4、意义:有了实例,就可以调用类中的内容
四、类对象和类属性
4.1、示例1(类对象)
类属性:类中方法外的变量称为类属性,被该类的所有对象所共享
#创建Student类的对象
stu1=Student('张三',20)
stu1.eat() #对象名.方法名()
print(stu1.name)
print(stu1.age)
print('------------------------')
Student.eat(stu1) #与stu1.eat() 代码功能相同,都是调用Student中的eat方法
#类名.方法名(类的对象)-->实际上就是方法定义处的self
4.2、示例2(类属性)
#类属性的使用方式
# print(Student.native_pace)
stu1=Student('张三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
五、类方法与静态方法
5.1、类方法
使用@classmethod修饰的方法,使用类名直接访问的方法
Student.cm()
5.2、静态方法
使用@staticmethod修饰的主法,使用类名直接访问的方法
Student.method()
六、动态绑定属性和方法
Python是动态语言,在创建对象之后,可以动态地绑定属性和方法
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print(self.name+'在吃饭')
stu1=Student('张三',20)
stu2=Student('李四',30)
print(id(stu1))
print(id(stu2))
print('------------为stu2动态绑定性别属性---------------------------')
stu1.gender='女'
#print(stu1.name,stu1.age,stu1.gender)
print(stu1.name,stu1.age,stu1.gender)
print(stu2.name,stu2.age)
print('--------------------------')
stu1.eat()
stu2.eat()
def show():
print('定义在类之外的,称函数')
stu1.show=show
stu1.show()
#stu2.show() #因为stu2并没有绑定show方法
七、面向对象的三大特征
7.1、封装:
7.1.1、提高程序的安全性
将数据(属性)和行为(方法)包装到类对象中。在方法内部对属性进行操作,在类对象的外部调用方法。这样,无需关心方法内部的具体实现细节,从而隔离了复杂度。
在Python中没有专门的修饰符用于属性的私有,如果该属性不希望在类对象外部被访问,前边使用两个“_” 。
7.1.2、封装的实现
class Student:
def __init__(self,name,age):
self.name=name
self.__age=age #年龄不希望在类的外部被使用,所以加了两个_
def show(self):
print(self.name,self.__age)
stu=Student('张三',20)
stu.show()
#在类的外使用使用name与age
print(stu.name)
#print(stu.__age)
#print(dir(stu))
print(stu._Student__age) #在类的外部可以通过 _Student__age 进行访问
7.2、继承:提高代码的复用性
7.2.1、语法格式
class 子类类名 (父类名1,父类名2....):
pass
注意:
如果一个类没有继承任何类,则默认继承object
Python支持多继承
定义子类时,必须在其构造函数中调用父类的构造函数
--------------
7.2.2、继承的代码实现示例
class Person(object): #Person继承object类
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person):
def __init__(self,name,age,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
stu=Student('张三',20,'1001')
teacher=Teacher('李四',34,10)
stu.info()
teacher.info()
7.2.3、多重继承
一个子类可以有多个“直接父类”,这样,就具备了“多个父类”的特点
通过类的特殊属性__mro__ 可以查看类的组织结构
print(dir(object))
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
class D(A):
pass
#创建C类的对象
x=C('Jack',20) #x是C类型的一个实例对象
print(x.__dict__) #实例对象的属性字典
print(C.__dict__)
print('--------------------')
print(x.__class__) #<class '__main__.C'> 输出了对象所属的类
print(C.__bases__) #C类的父类类型的元素
print(C.__base__) #类的基类
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #子类的列表
7.3、方法重写
1、如果子类对继承自父类的某个属性或方法不满意,可以在子类中对其(方法体)进行重新编写
2、子类重写后的方法中可以通过super().xxx() 调用父类中被重写的方法
3、示例
class Person(object): #Person继承object类
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person):
def __init__(self,name,age,stu_no):
super().__init__(name,age)
self.stu_no=stu_no
def info(self):
super().info()
print(self.stu_no)
class Teacher(Person):
def __init__(self,name,age,teachofyear):
super().__init__(name,age)
self.teachofyear=teachofyear
def info(self):
super().info()
print('教龄',self.teachofyear)
stu=Student('张三',20,'1001')
teacher=Teacher('李四',34,10)
stu.info()
print('----------------------')
teacher.info()
7.3、object类
1、object类是所有类的父类,因此所有类都有object类的属性和方法。内置函数dir()可以查看指定对象所有属性
2、Object有一个__str__()方法,用于返回一个对于“对象的描述”,对应于内置函数str()经常用于print()方法,帮我们查看对象的信息,所以我们经常会对__str__()进行重写
3、示例
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return '我的名字是{0},今年{1}岁'.format(self.name,self.age)
stu=Student('张三',20)
print(dir(stu))
print(stu) #默会调用__str__()这样的方法
print(type(stu))
7.4、多态:提高程序的可扩展性和可维护性
7.4.1、多态
简单地说,多态就是“具有多种形态”,它指的是:即便不知道一个变量所引用的对象到底是什么类
型,仍然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪
个对象中的方法。
7.4.2、示例
class Animal(object):
def eat(self):
print('动物会吃')
class Dog(Animal):
def eat(self):
print('狗吃骨头...')
class Cat(Animal):
def eat(self):
print('猫吃鱼...')
class Person:
def eat(self):
print('人吃五谷杂粮')
#定义一个函数
def fun(obj):
obj.eat()
#开始调用函数
fun(Cat())
fun(Dog())
fun(Animal())
print('----------------------')
fun(Person())
7.4.3、静态语言和动态语言关于多态的区别
1、静态语言实现多态的三个必要条件:继承方法;重写父类;引用指向子类对象
2、动态语言的多态崇尚“鸭子类型”当看到一只鸟走起来像鸭子、游泳起来像鸭子、收起来也像鸭子,那么这只鸟就可以被称为鸭子。在鸭子类型中,不需要关心对象是什么类型,到底是不是鸭子,只关心对象的行为。
八、特殊方法和特殊属性
1、dict()
print(dir(object))
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
class D(A):
pass
#创建C类的对象
x=C('Jack',20) #x是C类型的一个实例对象
print(x.__dict__) #实例对象的属性字典
print(C.__dict__)
print('--------------------')
print(x.__class__) #<class '__main__.C'> 输出了对象所属的类
print(C.__bases__) #C类的父类类型的元素
print(C.__base__) #类的基类
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #子类的列表
2、__init__();__add__();__len()__
a=20
b=100
c=a+b #两个整数类型的对象的相加操作
d=a.__add__(b)
print(c)
print(d)
class Student:
def __init__(self,name):
self.name=name
def __add__(self, other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu1=Student('Jack')
stu2=Student('李四')
s=stu1+stu2 #实现了两个对象的加法运算(因为在Student类中 编写__add__()特殊的方法)
print(s)
s=stu1.__add__(stu2)
print(s)
print('----------------------------------------')
lst=[11,22,33,44]
print(len(lst)) #len是内容函数len
print(lst.__len__())
print(len(stu1))
3、__new__();__init__();
class Person(object):
def __new__(cls, *args, **kwargs):
print('__new__被调用执行了,cls的id值为{0}'.format(id(cls)))
obj=super().__new__(cls)
print('创建的对象的id为:{0}'.format(id(obj)))
return obj
def __init__(self, name, age):
print('__init__被调用了,self的id值为:{0}'.format(id(self)))
self.name = name
self.age = age
print('object这个类对象的id为:{0}'.format(id(object)))
print('Person这个类对象的id为:{0}'.format(id(Person)))
#创建Person类的实例对象
p1=Person('张三',20)
print('p1这个Person类的实例对象的id:{0}'.format(id(p1)))
九、类的浅拷贝与深拷贝
9.1、变量的赋值操作
只是形成两个变量,实际上还是指向同一个对象
9.2、浅拷贝
Python拷贝一般都是浅拷贝,拷贝时,对象包含的子对象内容不拷贝,因此,源对象与拷贝对象会
引用同一个子对象
9.3、深拷贝
使用copy模块的deepcopy函数,递归拷贝对象中包含的子对象,源对象和拷贝对象所有的子对象
也不相同
9.4、示例
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
#(1)变量的赋值
cpu1=CPU()
cpu2=cpu1
print(cpu1,id(cpu1))
print(cpu2,id(cpu2))
#(2)类有浅拷贝
print('------------------------------')
disk=Disk() #创建一个硬盘类的对象
computer=Computer(cpu1,disk) #创建一个计算机类的对象
#浅拷贝
import copy
print(disk)
computer2=copy.copy(computer)
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
print('----------------------------------------')
#深拷贝
computer3=copy.deepcopy(computer)
print(computer,computer.cpu,computer.disk)
print(computer3,computer3.cpu,computer3.disk)