99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python中類、實例、方法之間的關(guān)系
Python中類、實例、方法之間的關(guān)系
2018-03-11
收藏

Python中類、實例、方法之間的關(guān)系

類的強大與否取決于它的功能,我們改進類的方法之一就是給類添加功能。類的功能有一個更為通俗的名字就是方法,在Python中,方法定義在類的定義中,但是只能被實例所調(diào)用,調(diào)用一個方法的最終途徑必須是這樣的:(1)定義類和類中的方法(2)創(chuàng)建一個實例或者說將類實例化(3)最后用這個實例調(diào)用方法
class MyDataWithMethod(object): # 定義類
    def printFoo(self): # 定義方法
        print 'You invoked printFoo()!'

在上面的例子中,在定義方法的時候有一個self參數(shù),在所有的方法聲明中都要用到這個參數(shù),這個參數(shù)代表實例對象本身,當你用實例調(diào)用方法的時候,由解釋器自動的把實例對象本身悄悄的傳遞給方法,不需要你自己傳遞self進來,例如有一個帶有兩個參數(shù)的方法,你所有調(diào)用只需要傳遞進來第二個參數(shù)。

現(xiàn)在我們來實例化這個類,然后來調(diào)用這個方法:
>>> myObj = MyDataWithMethod() # 創(chuàng)建實例
>>> myObj.printFoo() # 現(xiàn)在調(diào)用方法
You invoked printFoo()!

在Python中init()其實是一個初始化方法,是一個特殊的方法,通過下面的例子可能更容易理解:

#!/usr/bin/env python

class A(object):
    def __init__(self,nm,ph):
        self.name = nm
        self.phone = ph
        print 'the name of the instance is %s' % self.name
        print 'the name is %s' % self.name
        print 'the phonenumber is %s' % self.phone
    def updatephone(self,newph):
        self.phone = newph
        print 'update the instance of %s' % self.name
        print 'the update phonenumber is %s' % self.phone

a=A('jack','18811223344')
a.updatephone('88888888888')

運行結(jié)果是:

the name of the instance is jack
the name is jack
the phonenumber is 18811223344
update the instance of jack
the update phonenumber is 88888888888

在上面的例子中定義了兩個方法,一個是init(),另一個是updatephone(),你可以認為實例化是對init()的一種隱式的調(diào)用,因為傳給AddrBookEntry()的參數(shù)完全與init()接收到的參數(shù)是一樣的(除了self,它是自動傳遞的)。這里我個人的理解是在實例化時傳遞給self的參數(shù)就是a。語句a=A(‘jack’,’18811223344’)就是實例化調(diào)用,它會自動調(diào)用init(),你可以理解成把方法中的self用實例名字替換掉。
如果沒有設(shè)置默認參數(shù),那么必須傳遞給init相應(yīng)個數(shù)的參數(shù)。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }