面向对象的三大特征
封装:提高程序的安全性
将数据(属性)和行为(方法)包装到类对象中。在方法内部对属性进行操作,在类对象的外部调用方法。这样,无需关心方法内部的具体实现细节,从而隔离了复杂度。
在Python中没有专门的修饰符用于属性的私有,如果该属性不希望在类对象外部被访问,前边使用两个“_”。
#封装
class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('汽车已启动')
car=Car('宝马X5')
car.start()
print(car.brand)
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) #__age只能在类的内部使用
print(dir(stu))
print(stu._Student__age) #在类的外部可以通过_Student__age进行访问
继承:提高代码的复用性
语法格式
class 子类类名(父类1,父类2…):
pass
如果一个类没有继承任何类,则默认继承object
Python支持多继承
定义子类时,必须在其构造函数中调用父类的构造函数
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()
class A(object):
pass
class B(object):
pass
class C(A,B): #多继承
pass
方法重写:如果子类对继承自父类的某个属性或方法不满意,可以在子类中对其(方法体)进行重新编写
子类重写后的方法中可以通过super().XXX()调用父类中被重写的方法
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()
object类:object类是所有类的父亲,因此所有类都有object类的属性和方法。
内置函数dir()可以查看指定对象所有属性
Object有一个_str_()方法,用于返回一个对于“对象的描述”,对应于内置函数str()经常用于print()方法,帮我们查看对象的信息,所以我们经常会对_str_()进行重写
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__()这样的方法
多态:提高程序的可扩展性和可维护性
简单地说,多态就是“具有多种形态”,它指的是:即便不知道一个变量所引用的对象到底是什么类型,仍然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法
class Animal(object):
def eat(self):
print('动物会吃')
class Dog(Animal):
def eat(self):
print('狗吃肉')
class Cat(Animal):
def eat(self):
print('猫吃鱼')
class Person(object):
def eat(self):
print('小哥要吃苹果吗')
#定义一个函数
def fun(obj):
obj.eat()
#开始调用函数
fun(Cat())
fun(Dog())
fun(Animal())
print('----------------------------')
fun(Person())
静态语言与动态语言
静态语言与动态语言关于多态的区别
静态语言实现多态的三个必要条件:继承、方法重写、父类引用指向子类对象
动态语言的多态崇尚“鸭子类型”当看到一只鸟走起来像鸭子、游泳起来像鸭子、收起来也像鸭子,那么这只鸟就可以被称为鸭子。在鸭子类型中,不需要关心对象是什么类型,到底是不是鸭子,只关心对象的行为
#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__()) #子类的列表
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 #实现了两个对象的加法运算(因为在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))
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__被调用了,selfid值为{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)))
类的浅拷贝与深拷贝
变量的赋值操作:只是形成两个变量,实际上还是指向同一个对象
浅拷贝:Python拷贝一般都是浅拷贝,拷贝时对象包含的子对象内容不拷贝,因此,源对象与拷贝对象会引用同一个子对象
深拷贝:使用copy模块的deepcopy函数,递归拷贝对象中包含的子对象,源对象和拷贝对象所有的子对象也不相同
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)
print(cpu2)
#(2)类的浅拷贝
print('-------------------------')
disk1=disk() #创建一个硬盘类的对象
computer1=computer(cpu1,disk1) #创建一个计算机类的对象
#浅拷贝
import copy
computer2=copy.copy(computer1)
print(computer1,computer1.cpu,computer1.disk)
print(computer2,computer2.cpu,computer2.disk)
print('-------------------------')
#深拷贝
computer3=copy.deepcopy(computer1)
print(computer1,computer1.cpu,computer1.disk)
print(computer3,computer3.cpu,computer3.disk)