Python面向對象

定義一個類

class 類名

class HelloWorld

定義類的構造方法


def init(self)

  def __init__(self,name,age):
         self.name=name
         self.age=age
         print("my name is {0} age is {1}".format(name,age));
h=HelloWorld("郭金龍",22)

繼承一個類

class Hi(HelloWorld):
    def __init__(self, name):
      HelloWorld.__init__(self,name,22)//引用該類的父類的構造方法
    def sayHi(self):
        print("say my name---{0}".format(self.name))

h=HelloWorld("郭金龍",22)
h1=Hi("郭金龍")
h1.sayHi()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章