by shigemk2

当面は技術的なことしか書かない

継承

class FirstClass():
    def setData(self, value):
        self.data = value

    def display(self):
        print self.data

x = FirstClass()
y = FirstClass()
x.setData("King Arthur")
y.setData(3.14159)
x.display() # King Arthur
y.display() # 3.14159
x.data = "New value"
x.display() # New value
x.anothername = "spam"
print x.anothername # spam

class SecondClass(FirstClass):
    def display(self):
        print 'Current value = "%s"!' % self.data

x = SecondClass()
x.setData(42)
x.display() # Current value = "42"!

class ThirdClass(SecondClass):
    def __init__(self, value):
        self.data = value

    def __add__(self, other):
        return ThirdClass(self.data + other)

    def __mul__(self, other):
        self.data = self.data * other

a = ThirdClass("abc")
a.display() # Current value = "abc"!

b = a + 'xyz'
b.display() # Current value = "abcxyz"!

a * 3
a.display() # Current value = "abcabcabc"!