testResult = TestOutcomes(TestOutcomes.FAILED) a = testResult .__ str __() 打印一份 已中止 失败 The equivalent to Java''s toString() is __str__() in Python: class TestOutcomes:PASSED = 0FAILED = 1ABORTED = 2 def __init__(self,outcome):self.outcome = outcome def __str__(self):if self.outcome == TestOutcomes.PASSED:return "Passed"elif self.outcome == TestOutcomes.FAILED :return "Failed"else:return "Aborted" if __name__ == "__main__":testResult = TestOutcomes(TestOutcomes.ABORTED)print testResulttestResult = TestOutcomes(TestOutcomes.FAILED)a = testResult.__str__()print a AbortedFailed 这篇关于具有ToString功能的枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 03:52