python基礎學習(十一)

22.類函數

# 類 class
# 實例  實體 instance
class Student:
    # 空語句 保持結構的完整性
    pass
jack = Student()
jack.name = "Song Ke"
print(jack.name)

pony = Student()
pony.name = "pony"
print(pony.name)

run結果:spa

 23.構造函數code

# 構造函數  constructor
class Student:
    # 聲明構造函數  系統調用的
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
        print("__init__  is  run")


jack = Student("jack.a", 21, "")
pony = Student("pony.b", 22, "")

print(jack.name, jack.age, jack.sex)
print(pony.name, pony.age, pony.sex)

run結果:blog

 

相關文章
相關標籤/搜索