如何解决Python:重构一个函数在类层次结构中,该函数调用私有函数
我在Python中有一个类层次结构,其中类C
是从B
派生的,而A
是从recurse
派生的。在所有调用父级recurse
的类中都定义了函数__private
,还调用了私有函数recurse
并执行其他一些通用代码,即recurse
的定义除基类外完全相同。请参见下面的代码。
我的问题:如何在不对层次结构的所有类中重复class A:
def recurse(self):
# end of recursion
self.__private()
# execute some more code
def __private(self):
# do something here
print("A")
class B(A):
def recurse(self):
super().recurse()
self.__private()
# execute same code (as in A.recurse())
def __private(self):
# do something here
print("B")
class C(B):
def recurse(self):
super().recurse()
self.__private()
# execute same code (as in A.recurse())
def __private(self):
# do something here
print("C")
c = C()
c.recurse()
几乎相同的代码的情况下重写以下代码?
@Entity()
export class Anime {
@PrimaryGeneratedColumn('uuid')
id?: string;
@Column({ nullable: false })
name: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Category[];
}
@Entity()
export class Category {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ nullable: false })
name: string;
@Column({ default: 'Descrição não fornecida' })
description: string;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。