Day 13 面向对象
知识点
-
封装:提高程序的安全性
-
继承:提高代码的复用性
-
继承:如果一个类没有继承任何类,则默认继承object
python支持多继承,定义子类时,必须在其构造函数中
调用父类的构造函数
-
多态:提高程序的可扩展性和可维持性
封装
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/17 21:07
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._Student__age) #在类的外部可以通过 _Student__age访问
运行结果:
----封装--------
张三 20
20
继承
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/17 21:25
print('------继承-------')
class Person(object): #object可写可不写,不写默认是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()
运行结果:
------继承-------
张三 20
李四 34
重写
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/17 21:25
print('------重写-------')
class Person(object): #object可写可不写,不写默认是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()
运行结果:
------重写-------
张三 20
1001
==================
李四 34
10
其它案例
案例一
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/18 9:45
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)
print(type(stu))
运行结果:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name']
我的名字是张三,今年20岁
<class '__main__.Student'>
案例二
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/18 10:07
class A:
pass
class B:
pass
class C(A,B):
def __init__(self,name,age):
self.name=name
self.age=age
x=C('jack',20) #x是c类型的一个实例对象
print(x.__dict__) #实例对象的属性字典
class Student:
def __init__(self,name):
self.name=name
#只有添加了下面这个add的,才能实现两个对象的加法运算
def __add__(self, other):
return self.name+other.name
def __len__(self):
return len(self.name)
stu1=Student('张三')
stu2=Student('李四')
print(stu1.__add__(stu2))
print(stu1+stu2)
print(stu1.__len__())
print(len(stu1))
运行结果;
{'name': 'jack', 'age': 20}
张三李四
张三李四
2
2
案例三
# 教育机构 舒寒科技有限公司
# 学习者 舒寒拽少
# 学习时间: 2022/1/18 14:14
class Person(object):
def __init__(self,name,age):
print('__init__被调用了,self的id值为:{0}'.format(id(self)))
self.name=name
self.age=age
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
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这个类对象的id为:140725986109312
Person这个类对象的id为:1268267625088
__new__被调用执行了,cls的id值为1268267625088
创建的对象为id为:1268268891824
__init__被调用了,self的id值为:1268268891824
p1这个Person类的实例对象的id:1268268891824