python基礎4—物件導向

來源:互聯網
上載者:User

標籤:建構函式   成員   color   面向   nbsp   not   sel   王思聰   一個   

感覺和java相比一個是大公司,一個是小公司, 大公司規範有文檔,小公司簡約效率高

 

物件導向  # 成員屬性名稱前 加上 __ 意為private# get / set :  get_name()   set_name(name) class Student:    def __init__(self, name, age):        self.name = name        self.age = age            def detail(self):        print(self.name)        print(self.age)      class PrimaryStudent(Student):  # inherent    def lol(self):        print(‘can not win then run faster than others‘)class CollegeStudent(Student):    def __init__(self, name, age, gf):  # overrite建構函式        self.name = name        self.age = age        self.gf = gf    def gf_detail(self):        print(self.gf)obj1 = PrimaryStudent(‘小學生‘, 7)obj1.lol()obj1.detail()obj2 = CollegeStudent(‘王思聰‘, 29, ‘張雨欣‘)obj2.detail()obj2.gf_detail()print(dir(obj1))                            # class info as listprint(hasattr(obj1, ‘name‘))        # Truesetattr(obj1, ‘name‘, ‘jack‘)  print(getattr(obj1, ‘name‘))          # jackprint(getattr(obj1, ‘name‘, 404))  # jackfn = getattr(obj1, ‘detail‘)             #7fn()#  執行個體屬性和類屬性class Student(object):    name = ‘Student‘    def __init__(self, name):        self.name = name   # 類屬性s = Student(‘Bob‘)s.score = 90               # 執行個體屬性print(s.name) s.name = ‘Jack‘          # 給執行個體屬性綁定name屬性, 執行個體屬性優先順序比類屬性高 print(s.name)              # Jackprint(Student.name)   # Studentdel s.name                 # 刪除執行個體name屬性print(s.name)             # Student

 

python基礎4—物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.