Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python inheritance

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
Source by www.python-course.eu #
 
PREVIOUS NEXT
Tagged: #python #inheritance
ADD COMMENT
Topic
Name
4+3 =