zoukankan      html  css  js  c++  java
  • 经典类和新式类

    经典类:  class People:
    新式类:  class People(object):
    区别主要体现在多继承上构造函数顺序问题:
     
    python2:
      经典类是按深度优先来继承的,新式类按广度优先来继承的
     
    python3:
      都是按照广度优先来继承
     
    多继承:
    class People(object):
      def __init__(self, name, age):
        self.name = name
        self.age = age
      def eat(self):
        print("%s is eating ...." % self.name)
     
    class Relation(object):
      def make_friend(self, obj):
        print("%s make friend with %s" % (self.name, obj.name))
     
    class Men(People, Relation):
      def __init__(self, name, age, momey):
        #People.__init__(self, name, age)
        super(Men, self).__init__(name, age)
        self.momey = momey
      def sleep(self):
        print("%s is sleeping" % self.name)
     
     class Wemen(People, Relation):
      def xx(self):
        print("===================")
     
     t1 = Men("Brace", 29, 23)
     t2 = Wemen("Lucy", 29)
     
     t1.make_friend(t2)
     输出:
     Brace make friend with Lucy 
     
     class People(object):
      def __init__(self, name, age):
        self.name = name
        self.age = age
        self.friends=[]
      def eat(self):
        print("%s is eating ...." % self.name)
     
    class Relation(object):
      def make_friend(self, obj):
        print("%s make friend with %s" % (self.name, obj.name))
        self.friends.append(obj)
     
     class Men(People, Relation):
      def __init__(self, name, age, momey):
        #People.__init__(self, name, age)
        super(Men, self).__init__(name, age)
        self.momey = momey
    def sleep(self):
        print("%s is sleeping" % self.name)
     
    class Wemen(People, Relation):
      def xx(self):
        print("===================")
     
     t1 = Men("Brace", 29, 30)
     #t1.sleep()
     #print(t1.momey)
     t2 = Wemen("Lucy", 29)
     
     t1.make_friend(t2)
     t2.name = "Kitty"
     print(t1.friends[0].name)
     
     输出:
     Brace make friend with Lucy
     Kitty
     
     
    案例:
     学校,讲师,学员
     
     class School(object):
      def __init__(self, name, addr):
       self.name = name
       self.addr = addr
       self.students = []
       self.staffs = []
      def entrool(self, stu_obj):
       print("为学员  %s 办理注册登记手续" % stu_obj.name)
       self.students.append(stu_obj)
     
      def hire(self, staff_obj):
       print("%s 被雇佣为学校 %s 老师" % (staff_obj.name, self.name))
       self.staffs.append(staff_obj)
     
     class Schoolmembers(object):
      def __init__(self, name, age, gender):
       self.name = name
       self.age = age
       self.gender = gender
      def tell(self):
       pass
     
     class Teachers(Schoolmembers):
      def __init__(self, name, age, gender, salary, course):
       super(Teachers,self).__init__(name, age, gender)
       self.salary = salary
       self.course = course
     
      def tell(self):
       print("""
       ======info of teacher: %s ========
       name: %s
       age: %s
       gender: %s
       Salary: %s
       Course;%s
       """ % (self.name, self.name, self.age, self.gender, self.salary, self.course))
     
      def teach(self):
       print("%s is teaching course [%s]" %(self.name, self.course))
     
     class Students(Schoolmembers):
      def __init__(self, name, age, gender, stu_id, grade):
       super(Students, self).__init__(name, age, gender)
       self.stu_id = stu_id
       self.grade = grade
      def tell(self):
       print("""
       ======info of student: %s ========
       name: %s
       age: %s
       gender: %s
       stu_id: %s
       grade;%s
       """ % (self.name, self.name, self.age, self.gender, self.stu_id, self.grade))
     
      def pay_tuition(self, amount):
       print("%s has pay tution for $ %d" % (self.name, amount))
     
     school = School("惊风学院", "北京朝阳区")
     t1 = Teachers("Brace", 23, "Male", 20000, "Linux")
     t2 = Teachers("Lucy", 25, "Female", 10000, "Python")
     
     s1 = Students("Zhangsan",18, "male","10001", "python")
     s2 = Students("Lisi",16, "female","10002", "Linux")
     
     
     t1.tell()
     s1.tell()
     t2.tell()
     s2.tell()
     
     school.entrool(s1)
     school.hire(t1)
     school.entrool(s2)
     school.hire(t2)
     
     print(school.students)
     print(school.staffs)
     
     school.staffs[0].teach()
     
     for stu in school.students:
      stu.pay_tuition(5000)
     
     输出:
       ======info of teacher: Brace ========
       name: Brace
       age: 23
       gender: Male
       Salary: 20000
       Course;Linux
       
     
       ======info of student: Zhangsan ========
       name: Zhangsan
       age: 18
       gender: male
       stu_id: 10001
       grade;python
       
     
       ======info of teacher: Lucy ========
       name: Lucy
       age: 25
       gender: Female
       Salary: 10000
       Course;Python
       
     
       ======info of student: Lisi ========
       name: Lisi
       age: 16
       gender: female
       stu_id: 10002
       grade;Linux
       
       为学员  Zhangsan 办理注册登记手续
       Brace 被雇佣为学校 惊风学院 老师
       为学员  Lisi 办理注册登记手续
       Lucy 被雇佣为学校 惊风学院 老师
       [<__main__.Students object at 0x03B7CED0>, <__main__.Students object at 0x03B7CEF0>]
       [<__main__.Teachers object at 0x03B7CE90>, <__main__.Teachers object at 0x03B7CEB0>]
       Brace is teaching course [Linux]
       Zhangsan has pay tution for $ 5000
       Lisi has pay tution for $ 5000
  • 相关阅读:
    Codeforces Round #568 (Div. 2) D. Extra Element
    Codeforces Round #567 (Div. 2) B. Split a Number
    [dp+博弈]棋盘的必胜策略
    [暴力+前缀和]2019牛客暑期多校训练营(第六场)Upgrading Technology
    [set]Codeforces 830B-Cards Sorting
    [二分]煤气灶
    [STL] Codeforces 69E Subsegments
    剑指offer——判断B树是否是A树的子结构
    在浏览器地址栏输入URL执行后网页显示全过程
    链表反转【图解】
  • 原文地址:https://www.cnblogs.com/brace2011/p/9291444.html
Copyright © 2011-2022 走看看