class Base():
""" My base class """
__nb_instances = 0
def __init__(self):
Base.__nb_instances += 1
self.id = Base.__nb_instances
class User(Base):
""" My User class """
def __init__(self):
super().__init__()
self.id += 99
u = User()
print(u.id)
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
x = Student("Michael", "Smith", 2020)
class Coordinates:
def __init__(self, x, y):
self.__x = x #variables that start with double _ are private and only can be accessed by method
self.__y = y
def x(self):
return self.__x
def y(self):
return self.__y
def position(self):
return (self.__x, self.__y)
class Square(Coordinates):
def __init__(self, x, y, w, h):
super().__init__(x, y)
self.__w = w
self.__h = h
def w(self):
return self.__w
def h(self):
return self.__h
def area(self):
return self.__w * self.__h
class Cube(Square):
def __init__(self, x, y, w, h, d):
super().__init__(x,y,w,h)
self.__d = d
def d(self):
return self.__d
def area(self): #overwrites square area function
return 2 * (self.__w * self.__h + self.__d * self.__h + self.__w * self.__d)
def volume(self):
return self.__w * self.__h * self.__d
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
# Python inheritance is super simple
class BaseClass: ...
class SubClass(BaseClass): ... # inherits all values from BaseClass
# See, real simple