「逆引きPython/クラスとオブジェクト」の編集履歴(バックアップ)一覧に戻る

逆引きPython/クラスとオブジェクト - (2009/08/02 (日) 13:19:11) のソース

#contents(fromhere=true)

**クラスの例
 class MyClass(BaseClass1,BaseClass2):
     def __init__(self):
         self.data = []

 class MyStack:
     def __init__(self):
         self.data = list()
 
     def push(self,x):
         self.data.append(x)
 
     def pop(self):
         return self.data.pop()
 
     def clear(self):
         self.data = list()
 
     def size(self):
         return len(self.data)
 
 
 stack = MyStack()
 stack.push(4)
 stack.push(5)
 stack.push(8)
 print(stack.size()) #3
 print(stack.pop())  #8
 print(stack.size()) #2
 stack.clear()
 print(stack.size()) #0


 class MyTestClass:
     def __init__(self):
         self.__count = 0
  
     def inc(self):
         self.__count += 1
   
     def dec(self):
         if self.__count > 0:
             self.__count -= 1
  
     def getcount(self):
         return self.__count
 
  
 test = MyTestClass()
 test.inc()
 test.inc()
 print(test.getcount()) #2
 test.dec()
 print(test.getcount()) #1
 test.dec()
 test.dec()
 print(test.getcount()) #0
 print(test.__count)    #Error


----