style="padding-left:0px;">class Student: name="xxx" @staticmethod def show(): name="yyy" print(name,Student.name) @staticmethod def display(): show() s=Student() s.display() 结果:
举一反三
- style="padding-left:0px;">class Student:<p style="padding-left:50px;"> name="xxx"<p style="padding-left:50px;"> def show(cls):<p style="padding-left:100px;"> name="yyy"<p style="padding-left:100px;"> print(name,cls.name)<p style="padding-left:0px;">s=Student()<p style="padding-left:0px;">s.show()<p style="padding-left:0px;">结果
- class Student: def __init__(self,n="xxx"): self.name=n def show(self): print(self.name) s=Student("yyy") s.show() 结果
- class Student: def __init__(self,n="xxx",s="男"): self.name=n self.sex=s def show(self): print(s.name,s.sex) s=Student("yyy") Student.show(s) 结果:
- class Student: def __init__(self,n="xxx"): self.name=n def show(self): print(self.name)s=Student("yyy")s.show()结果 A: yyy B: xxx C: None D: 错误
- 以下代码中,实例方法定义正确的是() A: class Student: def setName(self,name): self.name=name B: class Student: def setName(name): self.name=name C: class Student: def setName(self): self.name=name D: class Student: def setName(name,self): self.name=name