クラスの例

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



タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2009年08月02日 13:19