class Test:
# 類屬性
num = 100
def __init__(self):
# 實例屬性,其實就像對象中的屬性
self.age = 20
# def setNum(self, num):
# self.num = num
# Test.num=200 print(Test.num) 200
# test_3 =Test()
# test_3.setNum(200)
# print(test_3.num) #200
# print(Test.num)#100
@classmethod #類方法
def setNum(self,num):
self.num=num
test_4=Test()
test_4.setNum(200)
print(test_4.num)#200
print(Test.num)#200
class ShortInputException(Exception): '''你定義的異常類''' def __init__(self, length, atleast): Exception.__init__(self) self.length = length self.atleast = atleast try: s = input('請輸入-->') if len(s) < 3: raise ShortInputException(len(s), 3) except EOFError: print('/你輸入了一個結束標記eof') except (ShortInputException,s): # 這個變量綁定到錯誤的實例 print('ShortInputException:輸入的長度是%d,長度至少應該是%d'%(s.length,s.atleast)) else: print('沒有異常')