• 第六节


    类和方法

    1.类的作用及析构函数

    class role:
    def __init__(self,name,age,life_valie):  #类的初始化变量
    self.name = name
    self.age = age
    self.__life_value = life_valie

    def __del__(self):
    print("this is 析构函数")

    def got_shot(self):
    self.__life_value -= 50  #定义私有变量,只能在类的内部调用

    def show_life_value(self):    
    print(self.__life_value)  #私有变量可以通过调用某些方法来取出

    r1=role("jack",18,100)
    print(r1.name)
    r1.got_shot()
    r1.show_life_value()

    运行结果:
    jack
    50
    this is 析构函数


    2.父类和子类

    class people:
    def __init__(self,name,age):
    self.name = name
    self.age = age

    def talk(self):
    print("%s is talking" % self.name)

    def sleep(self):
    print("%s is sleeping" % self.name)

    class man(people):
    def sleep(self):
    people.sleep(self)
    print("man %s is sleeping" % self.name)

    class woman(people):
    def sleep(self):
    print("woman %s is sleeping" % self.name)

    m1=man("jack",25)
    m1.sleep()
    m2=woman("mary",24)
    m2.sleep()

    运行结果:
    jack is sleeping
    man jack is sleeping
    woman mary is sleeping

    3.经典类与新式类
    #class people:   #经典类
    class people(object):
    def __init__(self,name,age):
    self.name = name
    self.age = age

    def talk(self):
    print("%s is talking" % self.name)

    def sleep(self):
    print("%s is sleeping" % self.name)

    class man(people):
    def __init__(self,name,age,weight):
    #people.__init__(self,name,age) #经典类
    super(man,self).__init__(name,age) #新式类
    self.weight = weight
    print("%s 的年龄%s,体重%d" % (self.name,self.age,self.weight))
    def sleep(self):
    people.sleep(self)
    print("man %s is sleeping" % self.name)

    class woman(people):
    def sleep(self):
    print("woman %s is sleeping" % self.name)

    m1=man("jack",25,30)
    m1.sleep()
    m2=woman("mary",24)
    m2.sleep()
    运行结果:
    jack 的年龄25,体重30
    jack is sleeping
    man jack is sleeping
    woman mary is sleeping

    4.继承多个类
    #class people:   #经典类
    class people(object):
    def __init__(self,name,age):
    self.name = name
    self.age = age

    def talk(self):
    print("%s is talking" % self.name)

    def sleep(self):
    print("%s is sleeping" % self.name)

    class relationship(object):
    def make_friends(self,obj):
    print("%s is making friends with %s" % (self.name,obj.name))

    class man(people,relationship):
    def __init__(self,name,age,weight):
    #people.__init__(self,name,age) #经典类
    super(man,self).__init__(name,age) #新式类
    self.weight = weight
    print("%s 的年龄%s,体重%d" % (self.name,self.age,self.weight))
    def sleep(self):
    people.sleep(self)
    print("man %s is sleeping" % self.name)

    class woman(people):
    def sleep(self):
    print("woman %s is sleeping" % self.name)

    m1=man("jack",25,30)
    m1.sleep()
    m2=woman("mary",24)
    m2.sleep()
    m1.make_friends(m2)
    运行结果:
    jack 的年龄25,体重30
    jack is sleeping
    man jack is sleeping
    woman mary is sleeping
    jack is making friends with mary

    5.深度优先与广度优先
    python2类的继承为深度优先
    python3类的继承为广度优先
    6.继承
    class School(object):
    def __init__(self,name,addr):
    self.name= name
    self.addr = addr
    self.students = []
    self.staffs = []

    def enroll(self,stu_obj):
    self.students.append(stu_obj)
    print("为新学生%s办理注册手续" % stu_obj.name)

    def hire(self,staf_obj):
    self.staffs.append(staf_obj)
    print("雇佣新员工%s" % staf_obj.name)

    class SchoolNumber(object):
    def __init__(self,name,age,sex):
    self.name = name
    self.age = age
    self.sex = sex

    def tell(self):
    pass

    class Teacher(SchoolNumber):
    def __init__(self,name,age,sex,salary,course):
    super(Teacher, self).__init__(name,age,sex)
    self.salary = salary
    self.course = course

    def tell(self):
    print('''
    ----info of teacher----
    Name:%s
    Age:%s
    Sex:%s
    Salary:%s
    Course:%s
    ''' % (self.name,self.age,self.sex,self.salary,self.course))

    def teach(self):
    print("%s is teaching course [%s]" % (self.name,self.course))

    class Student(SchoolNumber):
    def __init__(self,name,age,sex,stu_id,grade):
    super(Student, self).__init__(name,age,sex)
    self.stu_id = stu_id
    self.grade = grade

    def tell(self):
    print('''
    ----info of teacher----
    Name:%s
    Age:%s
    Sex:%s
    Stu_id:%s
    Grade:%s
    ''' % (self.name,self.age,self.sex,self.stu_id,self.grade))

    def pay_tuition(self,amount):
    print("%s has paid tuition for $%s" % (self.name,amount))


    school = School("河北农业大学","保定")

    t1=Teacher("tom",53,"male",10000,"Python")
    t2=Teacher("jack",48,"female",10000,"Math")
    s1=Student("panjiatao",25,"male",10001,"大四")
    s2=Student("mary",22,"female",10002,"大二")

    t1.tell()
    s1.tell()
    school.hire(t1)
    school.enroll(s1)
    school.enroll(s2)

    print(school.students) #两个列表
    print(school.staffs)

    school.staffs[0].teach()

    for stu in school.students:
    stu.pay_tuition(5000)

    运行结果:
            ----info of teacher----
            Name:tom
            Age:53
            Sex:male
            Salary:10000
            Course:Python
            

            ----info of teacher----
            Name:panjiatao
            Age:25
            Sex:male
            Stu_id:10001
            Grade:大四
            
    雇佣新员工tom
    为新学生panjiatao办理注册手续
    为新学生mary办理注册手续
    [<__main__.Student object at 0x000000304CC05080>, <__main__.Student object at 0x000000304CC050B8>]
    [<__main__.Teacher object at 0x000000304CBF9FD0>]
    tom is teaching course [Python]
    panjiatao has paid tuition for $5000
    mary has paid tuition for $5000

     多态



























  • 相关阅读:
    Ajax中XML和JSON格式的优劣比较
    多语言的网站怎么做呀
    asp.net 2.0多语言网站解决方案
    FrameSet 与 IFrame 彻底剖析
    概要设计说明书
    用C#如何实现大文件的断点上传!
    PowerDesigner生成sql生成不了
    TransactionScope的正确用法
    iframe
    动网转型网游一年获得成功:每月盈利超500万
  • 原文地址:https://www.cnblogs.com/ttyypjt/p/7253087.html
Copyright © 2020-2023  润新知