继承的概念:子类 自动拥有(继承) 父类 的所有 方法 和 属性 继承的语法
class 类名(父类名):
pass
- 子类继承自 父类,可以直接享受 父类中已经封装好的方法,不需要再次开发
- 子类中应该根据需求,封装子类特有的 属性和方法
- 当父类的方法实现不能满足子类需求时,可以对方法进行 重写(override)
class Person(object):
"""父类"""
def __init__(self, id, name=None):
self.id = id
self.name = name
def printInfo(self):
print(f'编号: {self.id} 姓名: {self.name}')
class Student(Person):
pass
class Teacher(Person):
pass
s1 = Student(111, "eric")
s1.printInfo()
父类和子类是一个相对概念
- 继承的好吹
不使用继承构造的Circle类和Rectangle类
import math
class Circle(object):
def __init__(self, color, r):
self.r = r
self.color = color
def area(self):
return math.pi * self.r * self.r
def show_color(self):
print(self.color)
class Rectangle(object):
def __init__(self, color, a, b):
self.color = color
self.a, self.b = a, b
def area(self):
return self.a * self.b
def show_color(self):
print(self.color)
circle = Circle('red', 3.0)
print(circle.area())
circle.show_color()
rectangle = Rectangle('blue', 2.0, 3.0)
print(rectangle.area())
rectangle.show_color()
from random import randint
class Sprite():
def __init__(self, name, blood, strength):
self.name = name
self.blood = blood
self.strength = strength
def take_damage(self, sprite):
if isinstance(sprite, Hero):
print("Hero 类")
elif isinstance(sprite, Montser):
print("Montser 类")
damage = randint(sprite.strength - 5, sprite.strength + 5)
self.blood -= damage
print(f'{self.name} 你被 {sprite.name}攻击, 受到了{damage}点伤害!, 还剩{self.blood}滴血..')
if self.blood <= 0:
print(f'{self.name} 你被 {sprite.name}杀死了; 胜败乃兵家常事, 请重新来过!')
return True
else:
return False
class Hero(Sprite):
def __init__(self, name, blood, strength, skin = None):
super().__init__(name, blood, strength)
self.skin = skin
class Montser(Sprite):
pass
if __name__ == '__main__':
h1 = Hero("鲁班", 1000, 300)
m1 = Montser("大龙", 5000, 50)
while True:
is_moster_died = m1.take_damage(h1)
if is_moster_died:
break
is_hero_died = h1.take_damage(m1)
if is_hero_died:
break
类属性 类方法 静态方法 类对象:类名 实例对象:类创建的对象 类属性就是类对象所拥有的属性,它被所有类对象的实例对象所共有,在内存中只存在一个副本(只被初始化一次),这个和C++、Java中类的静态成员变量有点类似。对于公有的类属性,在类外可以通过类对象(类名)和实例对象访问
class People(object):
nick_name = "Tom"
__age = 19
id = 0
def __init__(self, name):
self.name = name
self.score = 0
self.score += 1
People.id += 1
p1 = People("张三")
p2 = People("李四")
print(p1.nick_name)
print(People.nick_name)
print(People.id)
print(p1.score)
print(p2.score)
- 类方法
类对象所拥有的方法,需要用装饰器@classmethod来标识其为类方法,对于类方法,第一个参数必须是类对象,一般以cls作为第一个参数(当然可以用其他名称的变量作为其第一个参数,但是大部分人都习惯以’cls’作为第一个参数的名字),能够通过实例对象和类对象去访问。
class People(object):
nick_name = "Tom"
__age = 19
id = 0
country = "china"
def __init__(self, name):
self.name = name
self.score = 0
self.score += 1
People.id += 1
@classmethod
def get_country(cls):
return cls.country
@classmethod
def set_country(cls, country):
cls.country = country
def hello(self):
print("hello")
People.set_country('us')
print(People.get_country())
- 静态方法
需要通过修饰器@staticmethod来进行修饰,静态方法不需要定义参数
class People(object):
nick_name = "Tom"
__age = 19
id = 0
country = "china"
def __init__(self, name):
self.name = name
self.score = 0
self.score += 1
People.id += 1
@staticmethod
def get_country():
return People.country
@classmethod
def set_country(cls, country):
cls.country = country
def hello(self):
print("hello")
People.set_country('us')
print(People.get_country())
因为python中的列表不能完全代替数学中的向量运算, 我们自己实现一个线性代数的向量类
class Vector(object):
def __init__(self, lst):
self.__value = lst
def __str__(self):
return f'({", ".join([str(i) for i in self.__value])})'
if __name__ == '__main__':
v1= Vector([i for i in range(1, 6)])
print(v1)
|