python - 在另一个类方法中调用类方法
问题描述
在 Python 3 中,如何在另一个类方法中调用继承的方法?
class A:
name = 'foo'
def get_name(self):
return self.name
class B(A):
@classmethod
def do_other(cls):
cls.get_name()
在 cls.get_name() 中,它抱怨“参数“self”未填充”。我怎样才能克服这个问题而不必将 do_other 更改为常规方法?
解决方案
您实际上只需要返回cls.get_name(cls)
class A:
name = 'foo'
def get_name(self):
return self.name
class B(A):
@classmethod
def do_other(cls):
return cls.get_name(cls)
print(B.do_other())
推荐阅读
- flutter - Flutter 将值添加到列表
- c# - 如何在 WCF 服务中调用 REST API?
- go - 如何在 go-kit 中使用 zap logger?
- reporting-services - 从开发人员导出修改后的报告以进行测试。现在报告抱怨缺少参数值
- snowflake-cloud-data-platform - 在 Snowflake 中灵活命名表
- flutter - 单个小部件中的多个 API 请求
- javascript - 如何防止移动键盘(android)弄乱布局?
- java - 如何第一次运行 FreePBX?
- c# - 如何在单个 NuGet 包中提供多个“dotnet new”模板?
- java - 使用来自自定义 JR 数据源的数据创建图表