一、自定义类
定义
格式
class className:
suite
class className(base_classes):
suite
属性与方法
class Point(object):
def __init__(self,x = 0,y = 0):
self.x = x
self.y = y
def distence_from_origin(self):
return math.hypot(self.x,self.y)
def __eq__(self,other):
return self.x == other.x and self.y == other.y
def __repr__(self):
return "Point({0.x!r},{0.y!r}).format(self)"
def __str__(self):
return "({0.x!r},{0.y!r}).format(self)"
继承
class Circle(Point):
def __init__(self,radius,x = 0,y = 0):
super().__init__(x,y)
self.radius() = radius
def edge_distance_from_origin(self):
return abs(self.distance_from_origin()-self.radius)
def area(self):
return math.pi * (self.raduis ** 2)
def circumference(self):
return 2 * math.pi * self.radius
def __eq__(self,other):
return self.radius == other.radius and super().__eq__(otehr)
def __repr__(self):
return "Circle({0.raduis!r},{0.x!r},{0.y!r})".format(self)
def __str__(self):
return repr(self)
多态