Class


In [2]:
class Rectangle(object):
    def __init__(self, w=0, h=0):
        self.w=w
        self.h=h
    
    def get_area(self):
        return self.w*self.h
    
    def __str__(self):
        return "w:{}, h:{}, area:{}".format(self.w, self.h, self.get_area())

In [4]:
rec = Rectangle()
print(rec)


w:0, h:0, area:0

In [6]:
rec = Rectangle(w=3,h=4)
print(rec)


w:3, h:4, area:12

Inheritance

level2

  • bullet
  • bullet $e^x$

In [7]:
class Square(Rectangle):
    def __init__(self, e=0):
        self.w=e
        self.h=e

In [8]:
sq=Square(5)
print(sq)


w:5, h:5, area:25

In [ ]: