继承:行为
财产:钱不用孩子挣,但是可以花
皇位:江山不用孩子打,但是可以用
编程:代码不用子类写,但是可以用
class Person:
def say(self):
print("说话")
class Student(Person):
def play(self):
print("玩耍")
# self.say()
# 建议通过super()访问父类成员
super().say()
class Teacher(Person):
def teach(self):
print("教学")
self.say()
# 父类对象只能访问父类成员
p01 = Person()
p01.say()
# 子类对象能访问父类成员也能访问子类成员
s01 = Student()
s01.say()
s01.play()
# 关系判定
# 1. isinstance(对象, 类型)
# 人对象 是一种 人类型
print(isinstance(p01, Person)) # True
# 学生对象 是一种 人类型
print(isinstance(s01, Person)) # True
# 人对象 是一种 学生类型
print(isinstance(p01, Student)) # False
# 2. issubclass(类型, 类型)
# 人类型 是一种 人类型
print(issubclass(Person, Person)) # True
# 学生类型 是一种 人类型
print(issubclass(Student, Person)) # True
# 人类型 是一种 学生类型
print(issubclass(Person, Student)) # False
# 3. type(对象) == 类型
# 人类型 是 人类型
print(type(p01) == Person) # True
# 学生类型 是 人类型
print(type(s01) == Person) # False
# 人类型 是 学生类型
print(type(p01) == Student) # False
. 继承:——数据
class 子类(父类):
def init(self, 父类参数, 子类参数):
super().init(父类参数)
self. 子类参数=子类数据
class Person:
def __init__(self, name="", age=0):
self.name = name
self.age = age
"""
class Student(Person):
pass
# 子类没有构造函数,直接使用父类构造函数
s01 = Student("悟空",26)
print(s01.name)
print(s01.age)
"""
class Student(Person):
# 子类构造函数参数:父类参数 + 子类参数
def __init__(self, name,age,score=0):
super().__init__(name,age)
self.score = score
# 子类如果有构造函数,会覆盖父类构造函数,好像它不存在.
s01 = Student("八戒",24,80)
print(s01.name)
print(s01.age)
. 重写:
语法:
子类和父类方法相同
执行得是子类得方法
价值:
彰显子类个性
内置可重写函数:
python语言规定内的函数
class Car(object):
def __init__(self, bread="", speed=0):
self.bread = bread
self.speed = speed
# 对象 --> 字符串
def __str__(self):
return f"我是{self.bread},速度{self.speed}."
bm = Car("宝马",200)
# 直接打印对象
print(bm)# 我是宝马,速度200.
bc = Car("奔驰",200)
print(bc)
. 自定义对象使用算数运算符:
int类——1 对象
print(1+2) 1.add(2)
str类——"a"对象
print(‘a’+‘b’) ‘a’.add(‘b’)
class Vector2:
"""
二维向量
"""
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "x的分量是%d,y的分量是%d" % (self.x, self.y)
def __add__(self, other):
# other 是 Vector2 类型
x, y = None, None
if type(other) == Vector2:
x = self.x + other.x
y = self.y + other.y
# other 是 int 类型
elif type(other) == int:
x = self.x + other
y = self.y + other
return Vector2(x, y)
pos01 = Vector2(1, 1)
pos02 = Vector2(2, 2)
# 向量与向量运算
pos03 = pos01 + pos02 # pos01.__add__(pos02)
print(pos03) # print( pos03.__str__() )
# 向量与整数运算
pos04 = pos01 + 3 # pos01.__add__(3)
print(pos04)
. 增强运算符重载:
+产生新对象
+= 可变对象,在原对象上进行修改
结论:优先使用+=
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "Vector2(%d,%d)" % (self.x, self.y)
# +
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector2(x, y)
# +=
def __iadd__(self, other):
self.x += other.x
self.y += other.y
return self
pos01 = Vector2(1, 1)
print(id(pos01))
# 在原对象上修改
pos01 += Vector2(2, 2) # 创建新对象
print(id(pos01))
print(pos01)
# 返回新对象
pos02 = pos01 + Vector2(3, 3)
print(id(pos02))
# 可变对象+=应该在原对象上修改
# list01 = [1]
# print(id(list01)) # 140503543804104
# list01 += [2]
# print(id(list01)) # 140503543804104
Python容器提供的各种功能,都依赖于类型的比较运算符
class Vector2:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "Vector2(%d,%d)" % (self.x, self.y)
# 比较相同的逻辑
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# 比较大小的逻辑
def __lt__(self, other):
return self.x + self.y < other.x + other.y
v01 = Vector2(1, 1)
v02 = Vector2(1, 1)
# 如果Vector2类没有__eq__,默认按照地址比较
print(v01 == v02) # True
list01 = [
Vector2(3, 3),
Vector2(2, 2),
Vector2(1, 1),
Vector2(4, 4),
]
print(v01 in list01)
print(list01.count(v01))
print(list01.index(v01))
# 必须重写__lt__
print(min(list01))
list01.sort()
for item in list01:
print(item)