Nhắc đến first answer về các phương pháp ràng buộc và không ràng buộc trăn ở đây, tôi có một câu hỏi:Trọng một phương pháp tĩnh trong python
class Test:
def method_one(self):
print "Called method_one"
@staticmethod
def method_two():
print "Called method_two"
@staticmethod
def method_three():
Test.method_two()
class T2(Test):
@staticmethod
def method_two():
print "T2"
a_test = Test()
a_test.method_one()
a_test.method_two()
a_test.method_three()
b_test = T2()
b_test.method_three()
sản xuất đầu ra:
Called method_one
Called method_two
Called method_two
Called method_two
Có cách nào để ghi đè lên một phương pháp tĩnh trong python?
Tôi mong đợi b_test.method_three()
để in "T2", nhưng không (in "Called method_two" thay thế).
Cảm ơn rất nhiều! Đây là những gì tôi muốn. – Emma
Thực sự hữu ích. Trong trường hợp của tôi, tôi cần truy cập vào lớp của một cá thể. Tôi đã làm nó như thế này: 'dụ .__ lớp __. My_method()' – Caumons