import abc
import numpy as np
# 定义好实现基类的函数
def f1(self):
print('hello:{} + {}'.format(self.x, self.y))
# 基类
class Task(metaclass = abc.ABCMeta):
def __init__(self, x, y):
self.x = x
self.y = y
@abc.abstractmethod
def run(self):
pass
# 子类
class SubTask(Task):
def __init(self, x, y):
super().__init__(x, y)
# 传统方式
# def run(self):
# print('hello:{} + {}'.format(self.x, self.y))
# 另类方式
run = f1
def start(self):
self.run()
a = SubTask(1, 2)
a.start()
运行结果:
hello:1 + 2