I'm new to Python. I have investigated inheritance, so I will keep it as a memorandum.
Windows7 64bit Python 2.7
About classes Acquisition of super class
classTest.py
# -*- coding: utf-8 -*-
class BaseClass(object): #Inheriting the object class
def __init__(self, a, b):
self.a = a
self.b = b
def sum(self):
return self.a + self.b
class DerivedClass(BaseClass):
def __init__(self, a, b):
#Reusing
super(DerivedClass, self).__init__(a, b)
#Superclass methods can also be used here
print self.sum()
if __name__ == '__main__':
cls = DerivedClass(10, 5)
print "sum:" + str(cls.sum())
print "a:" + str(cls.a)
print "b:" + str(cls.b)
classTest.Output result of py
15
sum:15
a:10
b:5
In init () of DeruvedClass (subclass)
super(DerivedClass, self).__init__(a, b)
Describe as. If you don't want to reuse it, you'll have to write the same code again in Derived Class. So I'm using super () to get the superclass and reuse the constructor.
Also,
print self.sum()
You can use it immediately (that's right)
Recommended Posts