• day⑥:面向对象



    一.类初识-cs_game

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #!/usr/bin/env python
    #coding=utf-8
    class Role(object): #新式类的写法
        ac=None
        def __init__(self,name,role,weapon,life_value=100,money=15000):
            self.name=name
            self.role=role
            self.weapon=weapon
            self.life_value=life_value
            self.money=money
        def shot(self):
            print("shooting...")
        def got_shot(self):
            print("ah....,I got shot...")
        def buy_gun(self,gun_name):
            print("just bought %s"%gun_name)
            self.weapon=gun_name
    #实例化
    t1=Role("yaobin","boy","AK47"#此时self相当于t1,Role(t1,"yaobin","boy","AK47")
    t2=Role("test","girl","B22")    #此时self相当于t1,Role(t1,"test","girl","B22")
    t3=Role("test2","girl","B23")
    t4=Role("test3","girl","B24")
    #执行方法
    #t1.buy_gun("new1") #相当于Role.buy_gun(t1,"new1")
    #t2.buy_gun("new2")  #相当于Role.buy_gun(t2,"new2")
    #print(t1.weapon)
    #print(t2.weapon)
    t1.ac="China Brand"  #实例改属性
    t2.ac="US Brand"     #实例改属性
    Role.ac="Janpanese Brand"  #改类属性
    print("t1:",t1.weapon,t1.ac)
    print("t2:",t2.weapon,t2.ac)
    print("t3:",t3.weapon,t3.ac)
    print("t4:",t4.weapon,t4.ac)

    # print(Role.buy_gun)


    #结果:
    t1: AK47 China Brand
    t2: B22 US Brand
    t3: B23 Janpanese Brand
    t4: B24 Janpanese Brand




    二.继承_school_class

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    #!/usr/bin/env python
    #coding=utf-8
    class SchoolMember(object):
        members=0
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
            self.enroll()
        def tell(self):
            print("my name is %s"  %self.name)
        def enroll(self):
            SchoolMember.members+=1
            print("33[32;1mnew member [%s] is enrolled,now there are [%s] members.33[0m"%(self.name,SchoolMember.members) )
        #def __del__(self):
        #    print("33[31;1mmember [%s] is dead!33[0m" %self.name)
    class Teacher(SchoolMember):
        def __init__(self,name,age,sex,course,salary):
            super(Teacher,self).__init__(name,age,sex)
            #SchoolMember.__init__(self,name,age,sex) #经典类的写法,旧,不要用
            self.course=course
            self.salary=salary
        def teaching(self):
            print("Teacher [%s] is teaching [%s] for class [%s]"%(self.name,self.course,"s12"))
    class Student(SchoolMember):
        def __init__(self,name,age,sex,course,tuition):
            super(Student,self).__init__(name,age,sex)
            self.course=course
            self.tuition=tuition
        def pay_tution(self):
            print("cao ,student [%s] paying tution" %self.tuition)
    s1=Student("yaobin",24,"boy","py","1800")
    t1=Teacher("alex",30,"boy","py","100000")
    s2=Student("meimei",25,"girl","py","1800")
    t2=Teacher("wusir",27,"boy","py","120000")
    s1.tell()
    s1.pay_tution()
    t1.tell()

    t1.teaching()


    #结果:
    new member [yaobin] is enrolled,now there are [1] members.
    new member [alex] is enrolled,now there are [2] members.
    new member [meimei] is enrolled,now there are [3] members.
    new member [wusir] is enrolled,now there are [4] members.
    my name is yaobin
    cao ,student [1800] paying tution
    my name is alex
    Teacher [alex] is teaching [py] for class [s12]









  • 相关阅读:
    浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓
    Spring中ClassPathXmlApplicationContext类的简单使用
    测试计划/系统风险 (设计方面、开发方面、测试本身 等风险)
    浏览器兼容
    4.0 爬虫
    2.1 IDEA
    2.3 接口测试
    1.1测试分类
    1.3 ODPS
    2.1 Word 插入 smartart、图表
  • 原文地址:https://www.cnblogs.com/binhy0428/p/5221462.html
Copyright © 2020-2023  润新知