PyThon3类的基本使用
代码:
class Person: def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def desc(self): print("name:{},age:{},sex:{}".format(self.name,self.age,self.sex)) if __name__ == '__main__': person1 = Person("Jack",18,"man") person2 = Person("Tom",20,"man") person3 = Person("Rose",18,"woman") person1.desc() person2.desc() person3.desc()
运行结果:
name:Jack,age:18,sex:man name:Tom,age:20,sex:man name:Rose,age:18,sex:woma