面向对象-面向对象的可拓展性

class Chinese:
    country = 'China'

    def __init__(self, name, sex, age):
        self.Name = name
        self.Sex = sex
        self.Age = age

    def eat(self):
        print('%s is eatting' % self.Name)

    def learn(self):
        print("%s is learning, %s is %s years old" % (self.Name, self.Name, self.Age))


stu1 = Chinese('egon', 'male', 18)
stu2 = Chinese('alex', 'male', 28)
stu3 = Chinese('wusi', 'male', 38)
print(stu1)  # ---><__main__.Chinese object at 0x11039b320>

print(stu1.eat())  # ---> egon is eatting;对象.方法
print(stu2.learn()) # ---> alex is learning, alex is 28 years old

猜你喜欢

转载自www.cnblogs.com/hexiaorui123/p/10201374.html