Python一切皆對象

來源:互聯網
上載者:User

標籤:str   display   可見   編寫   圖片   mic   token   特徵   body   

一、含義

  python中一切皆為對象,且python3中類與類型(常見資料類型如列表字典等)是一個概念,類型就是類。

二、執行個體 

#!/usr/bin/env python3#-*- coding:utf-8 -*-print(type([1,23,4])) #  <class ‘list‘>print(list)class School_student:    job = ‘student‘    def study(self):        print(‘studying hard‘)    def relax(self):        print(‘realxing happy‘)print(School_student) # <class ‘__main__.School_student‘># 由此可見,列表等資料類型也是類,是特徵和技能的結合li = [1,2,3] # 定義列表(執行個體化)li2 = []li2.append(0) # li.append()和li2.append()調用同樣的方法,但綁定的對象不同,是不同的Binder 方法li.append(4) # 添加元素 [1, 2, 3, 4],append相當於list類的內建方法list.append(li,5) # [1, 2, 3, 4, 5] # 與li.append(4)作用相同print(li)

  再比如:

#類型dict就是類dict>>> list<class ‘list‘>#執行個體化的到3個對象l1,l2,l3>>> l1=list()>>> l2=list()>>> l3=list()#三個對象都有Binder 方法append,是相同的功能,但記憶體位址不同>>> l1.append<built-in method append of list object at 0x10b482b48>>>> l2.append<built-in method append of list object at 0x10b482b88>>>> l3.append<built-in method append of list object at 0x10b482bc8>

#操作Binder 方法l1.append(3),就是在往l1添加3,絕對不會將3添加到l2或l3
>>> l1.append(3) # [3]

#調用類list.append(l3,111)等同於l3.append(111)>>> list.append(l3,111) #等同於l3.append(111) [111]
 

 

三、物件導向練習

  1、編寫一個類,批量生產對象,並統計產生對象的次數

#!/usr/bin/env python3#-*- coding:utf-8 -*-class Students:    country = ‘China‘       # 資料屬性為共有的    school = ‘helloworld‘    count = 0    def __init__(self,name,age,sex,major): # 函數屬性,是給對象用的        self.name = name        self.age = age        self.sex = sex        self.major = major        Students.count += 1  # __init__方法每次被觸發就表示產生了一個對象,計數器則自加一次,更改其資料屬性    def study(self):        print(‘study everyday‘)    def relax(self):        print(‘relaxing‘)stu1 = Students(‘婷子‘,19,‘女‘,‘Comic‘)print(stu1.count) # 1stu2 = Students(‘星爺‘,36,‘男‘,‘Direct‘)print(stu2.count) # 2stu3 = Students(‘琉星‘,21,‘男‘,‘Detective‘)print(Students.count) # 3print(stu1.count) # 3print(Students.__dict__)print(stu1.__dict__) # {‘name‘: ‘婷子‘, ‘age‘: 19, ‘sex‘: ‘女‘, ‘major‘: ‘Comic‘}print(stu2.__dict__)
View Code

  2、 模仿LOL編寫兩個英雄類,英雄有暱稱、攻擊力、生命值等屬性;執行個體化出兩個英雄對象;

    英雄之間要有打鬥,被打的一方會掉血,血量小於0判定死亡。 

#!/usr/bin/env python3#-*- coding:utf-8 -*-class Gaylen:    camp = ‘Desmarcia‘    def __init__(self,nickname,aggressivity,life_valid):        self.nickname = nickname        self.aggressivity = aggressivity        self.life_valid = life_valid    def attack(self,target):        target.life_valid -= self.aggressivity        if target.life_valid <= 0:            print(‘%s is died‘%target.nickname)class Hand_of_Knoxus:    camp = ‘Desmarcia‘    def __init__(self,nickname,aggressivity,life_valid):        self.nickname = nickname        self.aggressivity = aggressivity        self.life_valid = life_valid    def attack(self,target):        target.life_valid -= self.aggressivity        if target.life_valid <= 0:            print(‘%s is died‘%target.nickname)herol = Gaylen(‘大蓋倫‘,60,300)hero2 = Hand_of_Knoxus(‘諾手‘,70,280)herol.attack(hero2)hero2.attack(herol)hero2.attack(herol)hero2.attack(herol)hero2.attack(herol)hero2.attack(herol)print(herol.life_valid)print(hero2.life_valid)
View Code

 

Python一切皆對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.