面向对像(2day)

http://www.cnblogs.com/post/readauth?url=/linhaifeng/articles/6182264.html

上节课复习

class Garen:        #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄;
    camp='Demacia'  #所有玩家的英雄(盖伦)的阵营都是Demacia;
    n=0
    # hobby=[]
    def __init__(self,nickname,aggressivity=58,life_value=455): #英雄的初始攻击力58...;
        self.nickname=nickname  #为自己的盖伦起个别名;
        self.aggressivity=aggressivity #英雄都有自己的攻击力;
        self.life_value=life_value #英雄都有自己的生命值;
        Garen.n+=1
        self.hobby=[]
    def attack(self,enemy):   #普通攻击技能,enemy是敌人;
        enemy.life_value-=self.aggressivity #根据自己的攻击力,攻击敌人就减掉敌人的生命值。

# g1 = Garen('草丛伦')
# g2 = Garen('草丛伦2')
# g3 = Garen('草丛伦3')
# print(g1.hobby,id(g1.hobby))
# print(g2.hobby,id(g2.hobby))
# print(g3.hobby,id(g3.hobby))
# #
# # # g1.camp='123123123123'
# g1.hobby.append('g1 hobby')
# g2.hobby.append('g2 hobby')
# g3.hobby.append('g3 hobby')
# print(g1.hobby)


#类的两种用法:实例化和属性引用
#一:实例化
# g1 = Garen('草丛伦')
# print(g1.__dict__)


#二:属性引用,类的属性就两种:变量和函数
# print(Garen.camp)
# print(Garen.xxxx)
# print(Garen.attack123)
# print(Garen.__dict__)
# print(Garen.camp)
# print(Garen.attack) #'attack'
#
# Garen.camp='123123123123123'
# Garen.x=123  #Garen.__dict__['x']=123
# print(Garen.__dict__)


# Garen.x=1
# print(Garen.__dict__)

# del Garen.camp
# print(Garen.__dict__)


#实例就一种用法:属性引用,且实例本身就只有
# g1 = Garen('草丛伦')
# print(g1.nickname)
# print(g1.aggressivity)
# print(g1.life_value)
#增,删,改,查实例属性,略



#类的名称空间与实例的名称空间

#类的变量是所有对象共享的
class Garen:        #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄;
    camp='Demacia'  #所有玩家的英雄(盖伦)的阵营都是Demacia;
    n=0
    # hobby=[]
    def __init__(self,nickname,aggressivity=58,life_value=455): #英雄的初始攻击力58...;
        self.nickname=nickname  #为自己的盖伦起个别名;
        self.aggressivity=aggressivity #英雄都有自己的攻击力;
        self.life_value=life_value #英雄都有自己的生命值;
        Garen.n+=1
        self.hobby=[]
    def attack(self,enemy):   #普通攻击技能,enemy是敌人;
        enemy.life_value-=self.aggressivity #根据自己的攻击力,攻击敌人就减掉敌人的生命值。

# g1 = Garen('草丛伦')
# g2 = Garen('草丛伦2')
# g3 = Garen('草丛伦3')

# # print(g1.camp)
# # print(g2.camp)
# # Garen.camp=123
# # print(g3.camp)
# # print(g1.camp)
# # print(g2.camp)

# print(g1.hobby,id(g1.hobby))
# print(g2.hobby,id(g2.hobby))
# print(g3.hobby,id(g3.hobby))
# #
# # # g1.camp='123123123123'
# g1.hobby.append('g1 hobby')
# g2.hobby.append('g2 hobby')
# g3.hobby.append('g3 hobby')
# print(g1.hobby)

# # g1.camp='123123123123'
# # print(g1.camp)
# # print(g2.camp)
# # print(g3.camp)
#
# # print(g1.__dict__)
# print(g1.n)
# print(g3.n)
# print(Garen.n)


#类的函数是绑定到每个对象的,每个对象绑定方法都是不一样的,但都是同一种功能

class Garen:        #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄;
    camp='Demacia'  #所有玩家的英雄(盖伦)的阵营都是Demacia;
    n=0
    # hobby=[]
    def __init__(self,nickname,aggressivity=58,life_value=455): #英雄的初始攻击力58...;
        self.nickname=nickname  #为自己的盖伦起个别名;
        self.aggressivity=aggressivity #英雄都有自己的攻击力;
        self.life_value=life_value #英雄都有自己的生命值;
        Garen.n+=1
        self.hobby=[]
    def attack(self,enemy):   #普通攻击技能,enemy是敌人;
        print('=====>',self)
        print('=====>',enemy)
        # enemy.life_value-=self.aggressivity #根据自己的攻击力,攻击敌人就减掉敌人的生命值。

g1 = Garen('草丛伦')
# print(g1.attack)
# print(Garen.attack)

# g1.attack(g1) #Garen.attack(g1,g1)
#

继承类
# class ParentClass1: #定义父类
#     pass
#
# class ParentClass2: #定义父类
#     pass
#
# class SubClass1(ParentClass1): #单继承,基类是ParentClass1,派生类是SubClass
#     pass
#
# class SubClass2(ParentClass1,ParentClass2): #python支持多继承,用逗号分隔开多个继承的类
#     pass
#
# print(ParentClass1.__bases__)
# print(SubClass1.__bases__)
# print(SubClass2.__bases__)

# class Aniaml:
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#
#     def walk(self):
#         print('%s is walking' %self.name)
#
#     def say(self):
#         print('%s is saying' %self.name)
#
#
# class People(Aniaml):
#     pass
#
# class Pig(Aniaml):
#     pass
#
# class Dog(Aniaml):
#     pass
#
#
# p1=People('obama',50)
# print(p1.name)
# print(p1.age)
# p1.walk()
#
# class Hero:
#     def __init__(self, nickname,
#                  aggressivity,
#                  life_value):
#         self.nickname = nickname
#         self.aggressivity = aggressivity
#         self.life_value = life_value
#
#     def attack(self, enemy):
#         enemy.life_value -= self.aggressivity
#
# class Garen(Hero):
#     camp='Demacia'
#     def attack(self, enemy):
#         pass
#     def fire(self):
#         print('%s is firing' %self.nickname)
# class Riven(Hero):
#     camp='Noxus'
# g1=Garen('garen',18,200)
# r1=Riven('rivren',18,200)
# # print(g1.camp)
# # print(r1.camp)
# # g1.fire()
# g1.attack(g1)



#派生
# class Hero:
#     def __init__(self, nickname,aggressivity,life_value):
#         self.nickname = nickname
#         self.aggressivity = aggressivity
#         self.life_value = life_value
#     def attack(self, enemy):
#         print('Hero attack')
# class Garen(Hero):
#     camp = 'Demacia'
#
#     def attack(self, enemy): #self=g1,enemy=r1
#         # self.attack(enemy) #g1.attack()
#         Hero.attack(self,enemy)
#         print('from garen attack')
#
#     def fire(self):
#         print('%s is firing' % self.nickname)
# class Riven(Hero):
#     camp = 'Noxus'
# g1 = Garen('garen', 18, 200)
# r1 = Riven('rivren', 18, 200)
# g1.attack(r1)
# # print(g1.camp)
# # print(r1.camp)
# # g1.fire()






class Hero:
    def __init__(self, nickname, aggressivity, life_value):
        self.nickname = nickname
        self.aggressivity = aggressivity
        self.life_value = life_value
    def attack(self, enemy):
        print('Hero attack')
        enemy.life_value -= self.aggressivity
# print(Hero.__init__)
# print(Hero.attack)
class Garen(Hero):
    camp = 'Demacia'
    def __init__(self, nickname, aggressivity, life_value, script):
        Hero.__init__(self,nickname,aggressivity,life_value)
        # self.nickname = nickname
        # self.aggressivity = aggressivity
        # self.life_value = life_value
        self.script = script
    def attack(self, enemy):  # self=g1,enemy=r1
        # self.attack(enemy) #g1.attack()
        Hero.attack(self, enemy)
        print('from garen attack')
    def fire(self):
        print('%s is firing' % self.nickname)
# g1=Garen('garen',18,200) #Garen.__init__(g1,'garen',18,200)
g1=Garen('garen',18,200,'人在塔在') #Garen.__init__(g1,'garen',18,200)
print(g1.script)

组合


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.period=peroid
python_obj=Course('python',15800,'7m')
t1=Teacher('egon','male',python_obj)
s1=Student('cobila','male',python_obj)

print(s1.course.name)
print(t1.course.name)





class Teacher:
    def __init__(self,name,sex,course_name,course_price,course_period):
        self.name=name
        self.sex=sex
        self.course_name=course_name
        self.course_price=course_price
        self.course_period=course_period


class Student:
    def __init__(self,name,sex,course_name,course_price,course_period):
        self.name=name
        self.sex=sex
        self.course_name=course_name
        self.course_price=course_price
        self.course_period=course_period

t1=Teacher('egon','male','python',15800,'7m')
s1=Student('cobila','male','python',15800,'7m')

print(s1.course.name)
print(t1.course.name)




#老师、学生与课程
#老师、学生与生日
#学生与分数
 
接口与归一化设计

python中没有接口这一概念,都是用继承来实现

class Animal:
    def run(self):
        raise AttributeError('子类必须实现这个方法')
    def speak(self):
        raise AttributeError('子类必须实现这个方法')



class People(Animal):
    def run(self):
        print('人正在走')

    # def speak(self):
    #     print('说话')

class Pig(Animal):
    def run(self):
        print('pig is walking')
    def speak(self):
        print('哼哼哼')

peo1=People()
# peo1.run()
peo1.speak()

# peo1=People()
# pig1=Pig()
#
# peo1.run()
# pig1.run()















# class Interface:#定义接口Interface类来模仿接口的概念,python中压根就没有interface关键字来定义一个接口。
#     def read(self): #定接口函数read
#         pass
#
#     def write(self): #定义接口函数write
#         pass
#
#
# class Txt(Interface): #文本,具体实现read和write
#     def read(self):
#         print('文本数据的读取方法')
#
#     def write(self):
#         print('文本数据的读取方法')
#
# class Sata(Interface): #磁盘,具体实现read和write
#     def read(self):
#         print('硬盘数据的读取方法')
#
#     def write(self):
#         print('硬盘数据的读取方法')
#
# class Process(Interface):
#     def read(self):
#         print('进程数据的读取方法')
#
#     def write(self):
#         print('进程数据的读取方法')
#
#
#
# t1=Txt()
# s1=Sata()
# p1=Process()
#
#
#
# t1.read()
# t1.write()
#
# s1.read()
# s1.write()
#
# p1.read()
# p1.write()

抽像类


import abc
#抽象类:本质还是类,与普通类额外的特点的是:加了装饰器的函数,子类必须实现他们
class Animal(metaclass=abc.ABCMeta):
    tag='123123123123123'
    @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()
print(peo1.tag)

继承类

原文地址:https://www.cnblogs.com/wanchenxi/p/7518773.html