import types
class Student:
def __init__(self):
pass
def PrintName(self):
print('name = ', self.name)
def PrintAge(self):
print('age = ', self.age)
def PrintScore(self):
print('score = ', self.score)
def SetName(self, name1):
self.name = name1
def SetAge(self, age1):
self.age = age1
def SetScore(self, score1):
self.score = score1
stu1 = Student()
stu2 = Student()
stu3 = Student()
stu1.SetName = types.MethodType(SetName, stu1, Student)
stu2.SetName = types.MethodType(SetName, stu2, Student)
stu3.SetName = types.MethodType(SetName, stu3, Student)
stu1.SetName('hello')
stu2.SetName('world')
stu3.SetName('python')
stu1.PrintName()
stu2.PrintName()
stu3.PrintName()
Student.SetAge = types.MethodType(SetAge, None, Student)
stu4 = Student()
stu5 = Student()
stu6 = Student()
stu4.SetAge(10)
stu5.SetAge(20)
stu6.SetAge(30)
stu4.PrintAge()
stu5.PrintAge()
stu6.PrintAge()
Student.SetScore = types.MethodType(SetScore, Student)
stu7 = Student()
stu8 = Student()
stu9 = Student()
stu7.SetScore(70.5)
stu8.SetScore(80.5)
stu9.SetScore(90.5)
stu7.PrintScore()
stu8.PrintScore()
stu9.PrintScore()
- 执行结果
