类-__init__

Posted soberkkk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类-__init__相关的知识,希望对你有一定的参考价值。

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
p = Person(杰拉考, 200)
print(p.name)
print(p.age)

输出结果:杰拉考  200

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def desc(self):
        print("我叫%s,今年%d岁" % (self.name, self.age))
p = Person(杰拉考, 200)
# 调用自我介绍方法 desc方法中的self就是外部的这个p
p.desc()

输出为:我叫杰拉考,今年200岁

以上是关于类-__init__的主要内容,如果未能解决你的问题,请参考以下文章