python-对象的初始状态(构造函数)

class Person(object):
    #name = ""
    #age = 0
    #height = 0
    #weight = 0
    def run(self):
        print("run")
    def eat(self,food):
        print("eat "+food)
    def openDoor(self):
        print("我已经打开了冰箱")
    def fillEle(self):
        print("我已经把大象放进冰箱")
    def closeDoor(self):
        print("我已经关上了冰箱门")
    def __init__(self,name,age,height,weight):
        #print(name,age,height,weight)
        #定义属性,上面的就不需要了
        self.name = name
        self.age = age
        self.height = height
        self.weight =weight
        #print("这里是init")
        pass
'''
构造函数:__init__在使用类创建对象的时候自动调用
注意:如果不显示的写出构造函数,默认自动添加一个空的构造函数
'''
per1 = Person("hanheimei",18,170,55)
print(per1.name,per1.age,per1.weight,per1.height)
per2 = Person("yangjiji",20,180,55)
print(per2.name,per2.age,per2.weight,per2.height)

猜你喜欢

转载自blog.csdn.net/qq_37298351/article/details/85784828