1. 程式人生 > 實用技巧 >第二十天

第二十天

今日內容

一、繼承應用

1.1在子類派生的新方法中重用父類的功能

  方式一:與繼承無關,直接呼叫另一個類的功能
          class Summary():
             school = "上海校區"
             def __init__(self,name,age,gender):
                 self.name = name
                 self.age = age
                 self.male = gender
        
         class Student(Summary):
             def choose(self):
                 print("%s 選課成功" % (self.name))

         stu1 = Student("aaa",18,"male")
         stu2 = Student("bbb",19,"male")
         stu3 = Student("ccc",20,"male")

         class Teacher():
             school = "上海校區"
             def __init__(self,name,age,gender,level):
                 Summary.__init__(self,name,age,gender)   #直接呼叫Summary功能
                 self.level = level
             def score(self):
                 print("%s 正在打分" % (self.name))

         ter1 = Teacher("xxx",38,"male",10)
         ter2 = Teacher("yyy",28,"male",1)
         print(ter1.__dict__)

  方式二:supwe的運用,嚴格按照繼承順序來查詢屬性
        class Summary():
            school = "上海校區"

            def __init__(self,name,age,gender):
                self.name = name
                self.age = age
                self.male = gender

        class Teacher(Summary):
            def __init__(self,name,age,gender,level):
                super(Teacher, self).__init__(name,age,gender)
                self.level = level

            def score(self):
                print("%s 正在打分" % (self.name))

        tea1 = Teacher("XXX",18,"meal",10)
        print(tea1.__dict__)

  案例一:super是按照發起者的mro列表依次往後查詢
        class A():
            def test(self):
                print("from A")
                super().test()

        class B():
            def test(self):
                print("from B")

        class C(A,B):
            pass
        obj = C()     #當前發起者為C類,C的mro列表為[C,A,B,object],當test先找到A內,
        obj.test()    #然後A內的super().test()再沿著A繼續往下找就找到了B

        obj1 = A()
        obj1.test()  #會報錯,因為A內的super().test()會找到object內,而object內沒有test

1.2mixins機制

  繼承表達的是一個is a 的關係,在使用多繼承時主要繼承的父類放在繼承括號的右邊,
  只需要使用其功能的父類放在左邊並取名為Mixins結尾
  例:
   class Vehicle():
       def vehicle(self):
           print("交通工具的功能")

   class FlyMixins():
       def flying(self):
           print("飛行的功能")

   class CivlAircraft(FlyMixins,Vehicle):
       print("明航飛機")

   class Helicopter(FlyMixins,Vehicle):
       print("直升飛機")
  
   class Car(Vehicle):
       print("汽車")

   obj2 = CivlAircraft()
   obj2.flying()

1.3組合運用

  原來繼承用的是 is a (某種東西是某一類)的概念,組合是某種東西有某一功能的概念
  當你使用的功能不是is a 的概念就用組合
  例:
        class Summary():
            school = "上海校區"

            def __init__(self,name,age,gender):
                self.name = name
                self.age = age
                self.male = gender

        class Student(Summary):
            school = "上海校區"

            def choose(self):
                print("%s 選課成功" % (self.name))

        class Teacher():
            school = "上海校區"

            def __init__(self,name,age,gender,level):
                Summary.__init__(self,name,age,gender)
                self.level = level

            def score(self):
                print("%s 正在打分" % (self.name))

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

            def tell(self):
                print("課程資訊<%s:%s:%s>" % (self.name,self.price,self.period))

        python = Course("python全棧開發",19800,"6mons")
        linux = Course("linux",19000,"5mons")

        stu1 = Student("XXX",18,"male")
        stu1.choose = python        #這時stu1不僅能調自己的資訊還可以呼叫python物件的Course類功能
        stu1.choose.tell()
        print(stu1.name)