封装 -- 程序安全性
class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('汽车已启动...')
car=Car('宝马X5')
car.start()
print(car.brand)
print(-----------------------------------)
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 进行访问
继承--代码复用性
class Person(object): #Person继承object类 也可以这么写 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
def __init__(self,name,age,stu_no): --Student的类属性
super().__init__(name,age) --调用父类的__init__
self.stu_no=stu_no -Student的类属性stu_no 赋值
class Teacher(Person):
def __init__(self,name,age,teachofyear): -- Teacher的类属性
super().__init__(name,age) --调用父类的__init__
self.teachofyear=teachofyear --Student的类属性teachofyear 赋值
stu=Student('张三',20,'1001')
teacher=Teacher('李四',34,10)
stu.info() -- 父类继承的方法
teacher.info() -- 父类继承的方法
print(------------------------多继承--------------------------------)
class A(object): -- A继承object
pass
class B(object): -- B继承object
pass
class C(A,B): -- C继承A,B
pass
print(------------------------方法重写--------------------------------)
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()
print('---------Object类-------------')
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self): --重写Object的str会输出对象的全部属性
return '我的名字是{0},今年{1}岁'.format(self.name,self.age)
stu=Student('张三',20)
print(dir(stu)) -- 内置函数,可以查看类的所有属性值
print(stu) #默会调用__str__()这样的方法
print(type(stu))
多态--可扩展性和可维护性
# 静态语言:继承,方法重写,父类引用指向子类对象 例如 Java
# 动态语言:鸭子类型的概念,不需要关心对象是什么类型,只关系对象的行为
print('-------多态,跟对象是否有继承关系无关,只要具备 eat方法就会去执行---------------')
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('--------上面这些是有继承关系的 cat,dog继承了animal,animal继承了Objcet--------------')
fun(Person())
print('--------person类是没有继承关系的,但是具有eat()行为,python --------------')
特殊属性和方法
print('------------特殊的属性--------')
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__()) #子类的列表
print('------------特殊的方法--------')
a=20
b=100
c=a+b #两个整数类型的对象的相加操作 底层的操作 a.__add__(b)
d=a.__add__(b)
print(c)
print(d)
class Student:
def __init__(self,name):
self.name=name
def __add__(self, other): # 两个对象的相加要重写__add__()
return self.name+other.name
def __len__(self): # 获取对象的长度要重写__len__()
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))
print('------------创建对象 __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)))
Object的浅拷贝与深拷贝
# 变量的赋值:只是形成两个变量,实际上还是指向同一个对象
''' 浅拷贝:
拷贝时,对象包含的子对象内容不拷贝,因此源对象与拷贝对象会引用同一个子对象
# 深拷贝:
使用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,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)