一、面向对象三大特征
1.封装
demo1:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 21:19
class Car:
def __init__(self,brand):
self.brand=brand
def start(self):
print('汽车已启动。。。')
car=Car('宝马X5')
car.start()
print(car.brand)
输出结果:
demo2:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 21:22
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进行访问
输出结果:
2.继承
demo3:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 21:32
class Person(object): #Person继承object类
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
#创建Student和Teacher的类
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
#创建Student和Teacher类的对象
stu=Student('张三',20,'1001')
teacher=Teacher('李四',34,10)
stu.info()
teacher.info()
输出结果:
demo4:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 21:42
class A(object):
pass
class B(object):
pass
class C(A,B):
pass
3.方法重写
demo5:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 21:44
class Person(object): #Person继承object类
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print(self.name,self.age)
#创建Student和Teacher的类
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)
#创建Student和Teacher类的对象
stu=Student('张三',20,'1001')
teacher=Teacher('李四',34,10)
stu.info()
print('-------------------------')
teacher.info()
输出结果:
4.object类
demo6:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 21:55
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))
输出结果:
5.多态的实现
demo7:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/26 22:01
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())
输出结果:
注:Java是静态语言,Python是动态语言。
6.特殊方法和特殊属性
(1)特殊方法
demo8:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/27 8:41
#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__) #输出了对象所属的类
print(C.__bases__) #C类的父类类型的元素
print(C.__base__) #类的基类
print(C.__mro__) #类的层次结构
print(A.__subclasses__()) #子类的列表
输出结果:
(2)特殊属性
demo9:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/27 8:54
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)) #len是内容函数len
print(lst.__len__())
print(len(lst))
输出结果:
demo10:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/27 9:05
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(object)))
#创建Person类的实例对象
p1=Person('张三',20)
print('p1这个Person类的实例对象的id:{0}'.format(id(p1)))
输出结果:
7.类的赋值、浅拷贝与深拷贝
demo11:
# 姓名:薛之芹
# QQ:1120514609
# 开发时间:2021/9/27 9:34
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) #创建一个计算机类的对象
#浅拷贝:
# Python拷贝一般都是浅拷贝,拷贝时,对象包含的子对象内容不拷贝
#因此,源对象与拷贝对象会引用同一个子对象
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)
输出结果:
总结



