Python基礎第18天

來源:互聯網
上載者:User

標籤:pytho   port   http   自動   --   span   lap   匯入模組   sed   

一:多態  指出了對象如何通過他們共同的屬性和動作來操作及訪問,不用考慮他們具體的類,反映執行時的一種狀態

 

eg.    len(str1)  -------------->  str1.__len__()

class H2O:    def __init__(self,name,temperature):        self.name=name        self.temperature=temperature    def turn_ice(self):        if self.temperature<0:            print(‘[%s]溫度太低結冰了‘%self.name)        elif self.temperature>0 and self.temperature<100:            print(‘[%s]液化成冰‘%self.name)        elif self.temperature > 100:            print(‘[%s]溫度太高成了水蒸氣‘%self.name)class Water(H2O):    passclass Ice(H2O):    passclass Steam(H2O):    passw1=Water(‘水‘,25)i1=Ice(‘冰‘,-20)s1=Steam(‘蒸汽‘,3000)# w1.turn_ice()# i1.turn_ice()# s1.turn_ice()def func(obj):    obj.turn_ice()func(w1)func(i1)func(s1)

二 :封裝  本質是明確地區分內外,內部的實現邏輯,外部無法知曉,且為封裝到內部的邏輯提供了一個提供者給外部使用

類的繼承  ①  改變   ② 擴充

多態就是累的兩層意義的一種具體的實現機制,即調用不同類執行個體化得到對象的相同方法,實現的過程不一樣。

物件導向優點:  封裝明確了內外

                     繼承+多態語言層面支援了歸一化設計

class People:    __star=‘earth‘    def __init__(self,id,name,age,salary):        self.id=id        self.name=name        self.age=age        self.salary=salary    def get_id(self):        print(‘我是私人方法,我找到的id是[%s]‘%self.id)    def get_star(self):        print(self.__star)p1=People(‘123456‘,‘alex‘,‘18‘,10000000)# print(p1._star)# print(p1.__star)#出錯,原因是python自動重新命名了print(People.__dict__)print(p1._People__star)p1.get_star()  #內部就可以調用
class Room:    def __init__(self,name,owner,width,length,heigh):        self.name=name        self.owner=owner        self.__width=width        self.__length=length  #隱藏,私人        self.__heigh=heigh    def tell_area(self):        return self.__width*self.__length*self.__heighr1=Room(‘廁所‘,‘alex‘,100,100,100000)print(r1.tell_area())
封裝示範

三:反射(自省)

這四種方法都是基於字串的,四種方法實現自省的功能

  • hasattr   檢測對象能不能調用到裡面的屬性或方法
  • getattr    
  • setattr    設定屬性
  • delattr    刪除
class BlackMedium:    feature=‘Ugly‘    def __init__(self,name,addr):        self.name=name        self.addr=addr    def sell_hourse(self):        print(‘[%s]正在買房子,sb才買呢‘%self.name)    def rent_hourse(self):        print(‘[%s]正在租房子,sb才租房‘%self.name)b1=BlackMedium(‘萬成置地‘,‘天露園‘)print(hasattr(b1,‘name‘)) #判斷object中有沒有一個name字串對應的方法或屬性print(hasattr(b1,‘sell_hpurse‘))    #能不能調用該方法print(getattr(b1,‘name‘))func=getattr(b1,‘rent_hourse‘)func()print(getattr(b1,‘rent_hoursefghj‘)) #沒有則會報錯print(getattr(b1,‘rent_hoursefghj‘,‘沒有這個屬性‘)) #設定預設參數,不會報錯setattr(b1,‘sb‘,True)setattr(b1,‘sb1‘,123)print(b1.__dict__)delattr(b1,‘sb‘)print(b1.__dict__)setattr(b1,‘func‘,lambda x:x+1)  #設定函數屬性setattr(b1,‘func1‘,lambda self:self.name+‘sb‘)print(b1.__dict__)print(b1.func(10))print(b1.func1(b1))

四:反射及動態匯入模組

#t.pydef test1():    print(‘test1‘)def _test2():  #私人    print(‘test2‘)

按字串匯入:

module_t=__import__(‘m1.t‘)  #傳入字串,提模數塊名,拿到的都是最頂層的那個print(module_t)module_t.t.test1()#一步一步往下調用

封裝私人屬性:

from m1.t import *  #只有這種情況下_test2()才報錯from m1.t import test1,_test2test1()# test2()#報錯_test2() #不報錯,說明python只是遵守某種約定不是真的限制

利用模組匯入:

import importlibm=importlib.import_module(‘m1.t‘)  #定位到所導的模組,所以不要m.t.testprint(m)m.test1()m._test2()

 

Python基礎第18天

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.