Python一切皆對象


一、含義

  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()調用同樣的方法,但綁定的對象不同,是不同的綁定方法
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()

#三個對象都有綁定方法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>

#操作綁定方法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) # 1
stu2 = Students('星爺',36,'','Direct')
print(stu2.count) # 2
stu3 = Students('琉星',21,'','Detective')
print(Students.count) # 3
print(stu1.count) # 3
print(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

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com