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

熱線電話:13121318867

登錄
首頁精彩閱讀Python類定義和類繼承詳解
Python類定義和類繼承詳解
2017-07-26
收藏

Python類定義和類繼承詳解

一、類定義:
class <類名>:
  <語句>
類實例化后,可以使用其屬性,實際上,創(chuàng)建一個類之后,可以通過類名訪問其屬性
如果直接使用類名修改其屬性,那么將直接影響到已經(jīng)實例化的對象

類的私有屬性:
__private_attrs  兩個下劃線開頭,聲明該屬性為私有,不能在類地外部被使用或直接訪問
在類內(nèi)部的方法中使用時 self.__private_attrs
 類的方法
在類地內(nèi)部,使用def關(guān)鍵字可以為類定義一個方法,與一般函數(shù)定義不同,類方法必須包含參數(shù)self,且為第一個參數(shù)
私有的類方法
__private_method 兩個下劃線開頭,聲明該方法為私有方法,不能在類地外部調(diào)用
在類的內(nèi)部調(diào)用slef.__private_methods
類的專有方法:
__init__  構(gòu)造函數(shù),在生成對象時調(diào)用
__del__   析構(gòu)函數(shù),釋放對象時使用
__repr__ 打印,轉(zhuǎn)換
__setitem__按照索引賦值
__getitem__按照索引獲取值
__len__獲得長度
__cmp__比較運算
__call__函數(shù)調(diào)用
 
__add__加運算
__sub__減運算
__mul__乘運算
__div__除運算
__mod__求余運算
__pow__稱方
示例:  
#類定義
class people:
  #定義基本屬性
  name = ''
  age = 0
  #定義私有屬性,私有屬性在類外部無法直接進行訪問
  __weight = 0
  #定義構(gòu)造方法
  def __init__(self,n,a,w):
    self.name = n
    self.age = a
    self.__weight = w
  def speak(self):
    print("%s is speaking: I am %d years old" %(self.name,self.age))
 
 
p = people('tom',10,30)
p.speak()
二、繼承類定義:
1.單繼承

class <類名>(父類名)
  <語句>
eg.
class childbook(book)
  age = 10

2.類的多重繼承  
class 類名(父類1,父類2,....,父類n)
   <語句1>
需要注意圓括號中父類的順序,若是父類中有相同的方法名,而在子類使用時未指定,python從左至右搜索,即方法在子類中未找到時,從左到右查找父類中是否包含方法?  
#另一個類,多重繼承之前的準(zhǔn)備
class speaker():
  topic = ''
  name = ''
  def __init__(self,n,t):
    self.name = n
    self.topic = t
  def speak(self):
    print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))
 
#多重繼承
class sample(speaker,student):
  a =''
  def __init__(self,n,a,w,g,t):
    student.__init__(self,n,a,w,g)
    speaker.__init__(self,n,t)
 
test = sample("Tim",25,80,4,"Python")
test.speak()#方法名同,默認(rèn)調(diào)用的是在括號中排前地父類的方法

類方法的覆寫——子類覆蓋掉父類的方法
def 方法名與父類一致
若是在方法中要使用到父類方法 父類名.方法名
若是將類放到了模塊中
使用時
import A
l = A.類()
以上三段程序的執(zhí)行結(jié)果:

數(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); }