class Device(ABC):
@abstractmethod
def fn(self):
raise NotImplementedError
def sync(self):
print("Start Sync ...")
self.fn()
print("Start Done.")
class PC(Device):
def fn(self):
print("PC Device fn!!!")
class Router(Device):
def fn(self):
print("Router Device sync")
def start_sync(d: Device):
d.sync()
if __name__ == '__main__':
p = PC()
r = Router()
start_sync(p) # Run p.fn()
start_sync(r) # Run r.fn()
请熟悉 Go 的大佬指导下,在 Go 中如何用接口与结构体实现类似的功能?(子类只需要重写 fn 方法,在 start_sync 处调用基类的 sync 时,能调用到子类重写后的 fn 方法)