|
发表于 2018-9-22 16:33:48
|
显示全部楼层
0.可以节约代码量
1.调用会报错
TypeError: __init__() should return None, not 'str'
2.子类定义了之后子类的这些属性和方法会覆盖父类的
3.可以在企鹅类下面重新定义飞的方法,覆盖父类的定义
4.可以自动找出父类的方法,而且符合哪个什么mro方法
5.结果会在调用B,C时都调用了基类A的__init__
6.
class A():
def __init__(self):
print("进入A…")
print("离开A…")
class B(A):
def __init__(self):
print("进入B…")
super().__init__()
print("离开B…")
class C(A):
def __init__(self):
print("进入C…")
super().__init__()
print("离开C…")
class D(B, C):
def __init__(self):
print("进入D…")
super().__init__()
print("离开D…")
0.
import random as r
import math as m
class Point:
def __init__(self):
self.x = r.randint(-100,100)
self.y = r.randint(-100,100)
class Line:
def __init__(self,a,b):
ab = (a[0]-b[0])**2 +(a[1]-b[1])**2
print(round(m.sqrt(ab),2))
a = Point()
b = Point()
spot = []
spot.append((a.x,a.y))
spot.append((b.x,b.y))
l = Line(spot[0],spot[1])
|
|