2021-08-25
课程介绍
1、封装
2、继承
3、方法重写
4、object类
5、多态
6、特殊方法和特殊属性
110、面向对象的三大特征_封装的实现方式
封装:提高程序的安全型
继承:提高代码的复用性
多态:提高程序的可扩展性和可维护性
封装
将数据(属性)和行为(方法)包装到类对象中,在方法内部对属性进行擦欧洲哦,在类对象的外部调用方法。这样无需、关心内部的具体实现细节。
python中没有专门的修饰符用于属性的私有,如果该属性不希望在类对象外部被方位,前边使用两个“_”
##112、面向对象的三大特征_封装的实现方式
class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('汽车已启动。。。')
car=Car('宝马X5')
print(car.brand)
class Student:
def __init__(self,name,age):
self.name=name
self.__age=age # 不希望age在类的外部被使用,加了两个下划线
def show(self):
print(self.name,self.__age)
stu=Student('张三',20)
stu.show()
#在类的外部使用name与age
print(stu.name)
#加了__是不建议调用的,但是是可以调用的
# print(stu.__age) ##AttributeError: 'Student' object has no attribute 'age'
#加了__是不可以调用的,但是是可以调用的
print(dir(stu)) ##找到很多类别,其中有'_Student__age'
print(stu._Student__age)
113、继承及其实现方式
语法格式
class 子类类名(父类1,父类2...)
pass
父类支持多继承,分类可以有多个
如果一个类没有继任何类,则默认继承object
##111、继承及其实现方式
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person): ##定义Person的子类Student
def __init__(self,name,age,stu_no):
super().__init__(name,age) #调用父类的init的方法
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()
##Python 支持多继承
class A(object):
pass
class B(object):
pass
class C(A,B): #C类同时继承A类和B类
pass
112、方法重写
父类不能提供子类的需求时,子类重写
##112、方法重写
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
class Student(Person): ##定义Person的子类Student
def __init__(self,name,age,stu_no):
super().__init__(name,age) #调用父类的init的方法
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)
113、Object类
object是多有类的父类,因此所有类都有object的属性和方法
内置函数dir()可以查看对象的所有属性和方法
object有一个str方法,对应print(),默认输出id地址;我们常对此方法进行重写
##113、Object类
class Student:
pass
stu=Student()
print(dir(stu)) ##可以查看对象的所有属性和方法,有很多个,都是从 父类Object继承过来的
print(stu) ##默认调用__str__方法
##可以重写 object的方法
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)) ##可以查看对象的所有属性和方法,有很多个,都是从 父类Object继承过来的
print(stu) ##默认调用__str__方法# 方法重写,由输出内存地址,变为输出自定义内容、
print(type(stu))
114、多态
多态指的是即便不知道一个变量所引用的对象到底是什么类型,仍然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法。
##114、多态
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())
image.png
静态语言实现多态的三个必要条件:继承、方法重写、父类引用指向子类对象
动态语言的多态崇尚鸭子类型,当看到一只鸟走起来像鸭子、有用起来像鸭子,那么这只鸟就可以被称为鸭子。鸭子类型中,不关心到底是不是鸭子,只关心有没有鸭子的行为。
Python是动态语言:不需要明确继承关系,有就行
JAVA是静态语言:必须明确继承关系,才能实现动态
115、特殊属性
特殊属性:dict:获得类对象或实例对象所绑定的所有属性和方法的字典
特殊方法:
len: 通过重写-使得内置函数len()的参数可以是自定义类型
add:通过重写-使自定义对象具有“+”功能
new:创建对象
init:对创建对象进行初始化
##115、特殊属性
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__) #输出对象所属的类
print(C.__bases__) #输出对象分类类型的元素
print(C.__base__) #输出C最近的一个父类
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #查看子类
116、特殊方法
##116、特殊方法
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('张三')
stu2=Student('李四')
s=stu1+stu2 ##unsupported operand type(s) for +: 'Student' and 'Student'
##一定要实现两个实例对象的相加的话,需要在Student类中,编写__add__()特殊方法
print(s)
s=stu1.__add__(stu2)
print(s)
print('---------------------------')
lst=[11,22,33,44]
print(len(lst))
print(lst.__len__())
print(len(stu1)) #object of type 'Student' has no len()
#一定要得到长度的话,得定义类的时候,定义一个计算长度的方法
117、new;init创建对象
new在前创立对象,init在后,给实例对象赋值
image.png
##117、__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)))
118-119、类的赋值与浅拷贝、深拷贝
变量的赋值操作:
只是形成两个变量,实际上还是指向同一个对象
浅拷贝:
Python拷贝一般都是浅拷贝,拷贝时,对象包含的子对象内容不拷贝,因此源对象与拷贝对象会引用同一个子对象。
深拷贝:
使用copy模块的deepcopy函数,递归拷贝对象中包含的子对象,源对象和拷贝对象所有的子对象也不相同
image.png
image.png
##118-119、类的赋值与浅拷贝、深拷贝
class CPU:
pass
class Disk:
pass
class Computer:
def __init__(self,cpu,disk):
self.cpu=cpu
self.disk=disk
#变量的赋值,同一个实例对象CPU()赋值给两个变量cpu1、cpu2
cpu1=CPU()
cpu2=cpu1
print(cpu1,id(cpu1))
print(cpu2,id(cpu2))
#类有浅拷贝
print('--------------')
disk=Disk()
computer=Computer(cpu1,disk)
#浅拷贝
import copy
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)
知识点总结
面向对象三大特征:
封装、
继承:多继承、方法重写
多态:
动态语言→关注对象的行为
静态语言:继承、方法重写、父类引用指向子类对象
Object
所有类的父类
new穿件对象
—init—初始化对象
—str—对象的描述











网友评论