Python全栈之路Day26

时间:2022-12-15 14:13:15

初次编辑2017年11月12日,星期日

摘要
一.上节课复习
二. 继承
三. 派生
四. 组合
五. 接口与归一化设计
六. 抽象类
作业
今日总结

摘要

引用:百度

  1. 继承
  2. 派生
  3. 组合
  4. 接口与归一化设计
  5. 抽象类

一.上节课复习

  1. 每次实例化一次,计数加1
#每次实例化一次,计数加1
class Garen:
camp = 'Demacia'
n = 0
def __init__(self,nickname,aggressivity = 54,life_value =414 ): #英雄初始攻击力为54
self.nickname = nickname #为自己的盖伦起个别名
self.aggressivity = aggressivity #英雄攻击力
self.life_value = life_value #英雄生命值
Garen.n += 1
def attack(self,enemy): #普通攻击技能,enemy是敌人
enemy.life_value -= self.aggressivity #根据自己攻击力,减少敌人的血量
#
g1 = Garen('lex')
g2 = Garen('lex')
print(g1.n) #2
print(g2.n) #2
  1. 类的变量一般定义为不可变类型
class Garen:
camp = 'Demacia'
hobby = []
def __init__(self,nickname,aggressivity = 54,life_value =414 ): #英雄初始攻击力为54
self.nickname = nickname #为自己的盖伦起个别名
self.aggressivity = aggressivity #英雄攻击力
self.life_value = life_value #英雄生命值
def attack(self,enemy): #普通攻击技能,enemy是敌人
enemy.life_value -= self.aggressivity #根据自己攻击力,减少敌人的血量
#
g1 = Garen('lex')
g2 = Garen('alex')
g1.camp = '6666' #如果为不可变类型,修改的是对象自己的名称空间的
print(g1.camp) #6666
print(g2.camp) #Demacia
g1.hobby.append('g1 hobby') #不可变类型的修改,则还是修改的原内存地址对应的
g2.hobby.append('g2 hobby')
print(g1.hobby) #['g1 hobby', 'g2 hobby']
print(g2.hobby) #['g1 hobby', 'g2 hobby']

二. 继承

  1. 什么是继承:继承是一种创建类的方式,原始类称为基类或超类,新建类称为派生类或者子类
class ParentClass1:    #定义父类
pass
#
class ParentClass2: #定义父类
pass
#
class SubClass1(ParentClass1): #单继承,基类是ParentClass1,派生类是SubClass1
pass
class SubClass2(ParentClass1,ParentClass2): #python支持多继承,用逗号分隔开多个继承的类
pass
#
print(ParentClass1.__bases__) #(<class 'object'>,)
print(SubClass1.__bases__) #(<class '__main__.ParentClass1'>,)
print(SubClass2.__bases__) #(<class '__main__.ParentClass1'>, <class '__main__.ParentClass2'>)
  1. 继承实例
class Aniaml:
def __init__(self,name,age):
self.name = name
self.age = age
def walk(self):
print('%s is wakling'% self.name)
def say(self):
print('%s is saying'% self.name)
#
class People(Aniaml):
pass
#
p1 = People('lex',27)
print(p1.name) #lex
print(p1.age) #27
p1.walk() #lex is wakling

三. 派生

class Hero:
def __init__(self,nickname,aggressivity,life_value):
self.nickname = nickname
self.aggressivity = aggressivity
self.life_value = life_value
print('from Hero init')
def attack(self):
print('from Hero attck')

class Garen(Hero):
camp = 'Demacia'

def __init__(self, nickname, aggressivity, life_value,script):
Hero.__init__(self, nickname, aggressivity, life_value) #调用父类
self.script = script #派生新类

def attack(self):
Hero.attack(self) #调用父类
print('from Garen attack') #派生新类

def fire(self):
print('%s is firing'%self.nickname)

g1 = Garen('garen',18,200,'人在塔在')
g1.attack()
print(g1.script)

#from Hero init
#from Hero attck
#from Garen attack
#人在塔在

四. 组合

class Teacher:
def __init__(self,name,sex,course):
self.name = name
self.sex = sex
self.course = course

class Student:
def __init__(self,name,sex,course):
self.name = name
self.sex = sex
self.course = course

class Course:
def __init__(self,name,price,peroid):
self.name = name
self.price = price
self.peroid = peroid

t1 = Teacher('egon','male',Course('python',15800,'7m'))
s1 = Student('lex','male',Course('python',15800,'7m'))
print(t1.course) #<__main__.Course object at 0x00000000027BE4A8>
print(s1.course.name) #python

python_obj = Course('python',15800,'7m')
t1 = Teacher('egon','male',python_obj)
s1 = Student('lex','male',python_obj)
print(t1.course) #<__main__.Course object at 0x00000000027BE710>
print(s1.course.name) #python

五. 接口与归一化设计

  1. 接口归一化设计初步:
    但是此种方法,无法避免子类如果没有父类的方法
class Animal:
def run(self):
pass

def speak(self):
pass
#
class People(Animal):
def run(self):
print('人在跑')

def speak(self):
print('人在说话')
#
Peo1 = People()
#
Peo1.run()
Peo1.speak()
  1. 接口与归一化设计:此种方法可以抛出异常,避免以上错误
class Animal:
def run(self):
raise AttributeError('子类必须实现这个方法')
#
def speak(self):
raise AttributeError('子类必须实现这个方法')
#
class People(Animal):
# def run(self):
# print('人在跑')

# def speak(self):
# print('人在说话')
pass
#
Peo1 = People()
#
Peo1.run() #AttributeError: 子类必须实现这个方法
Peo1.speak()

六. 抽象类

  1. 抽象类:本质还是类,与普通类相比,其额外特点是:加了装饰器的函数,子类必须实现他们
import abc        #借助模块abc
#
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def run(self):
pass
@abc.abstractmethod
def speak(self):
pass
#
class People(Animal):
def run(self):
pass

def speak(self):
pass
#
Peo1 = People()
  1. 如果子类没有实现,如下
import abc
#
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def run(self):
pass
@abc.abstractmethod
def speak(self):
pass
#
class People(Animal):
# def run(self):
# pass
#
# def speak(self):
# pass
pass
#
Peo1 = People() #TypeError: Can't instantiate abstract class People with abstract methods run, speak

作业

  1. pass

今日总结

  1. pass
    1