1. 程式人生 > 其它 >Pyuthon一切皆物件

Pyuthon一切皆物件

'''
#1.一切皆物件
# 資料型別 : 數字型別 布林型別 字串型別 列表 字典 集合 元組
s = "hello yuan"
s2 = str("hello yuan")
print(s, type(s2)) # hello yuan <class 'str'>

print(s.upper(), type(s2)) # HELLO YUAN <class 'str'>


l = [1, 2, 3]
l2 = list([2, 3, 4])
print(l2, type(l2)) # [2, 3, 4] <class 'list'>
l2.append(5)
print(l2, type(l2)) # [2, 3, 4, 5] <class 'list'>



#2.擴充套件案例
class Weapon(object):
def __init__(self, name, color, attack):
self.name = name # 武器的名字
self.color = color # 武器的顏色
self.attack = attack # 武器的攻擊

class Hero(object):
def __init__(self, name, sex, hp, weapon, level=2, exp=2000, money=10000):
self.name = name # 英雄的名字
self.sex = sex # 英雄的性別
self.hp = hp # 英雄生命值
self.level = level # 英雄的等級
self.exp = exp # 英雄的經驗值
self.money = money # 英雄的金幣
self.weapon = weapon # 英雄的武器
w1 = Weapon("弓箭", "黃色", 180)
print(w1) # <__main__.Weapon object at 0x00000250DA3E7FD0>
huchangxi = Hero("huchangxi","男" ,100, w1)
print(huchangxi) # <__main__.Hero object at 0x00000250DA3E7940>
print(huchangxi.weapon) # <__main__.Weapon object at 0x00000250DA3E7FD0>


#3.誤區
#宣告類
class Student(object):
# 類屬性
clsas_name = "34期"
num = 30
listen = 100
# 方法
def listen(self):
print("聽課")

def homework(self):
print("寫作業")


print(Student.clsas_name) # 34期
Student.clsas_name = "xxx"

s1 = Student()
print(s1.clsas_name) # xxx
s1.listen = 100
s1.listen() # 報錯:TypeError: 'int' object is not callable

#詳解
#===============================

def TEST():
TEST = "呼長喜"
print(TEST)

print (TEST) # 呼長喜
#===============================
TEST2 = 150
def TEST2():
TEST2 = "呼長喜"
print(TEST2)

print (TEST2) # 呼長喜

def TEST3():
TEST3 = "呼長喜"
print(TEST3)
TEST3 = 150
print (TEST3) # 150
#===============================
'''