Python學習筆記-物件導向

來源:互聯網
上載者:User

標籤:try   gif   複雜度   共用   定義類   課程   解決問題   思維   ssm   

  一、什麼是物件導向的程式設計1、面向過程的程式設計  

  面向過程:核心是過程二字,過程即解決問題的步驟,就是先幹什麼,再幹什麼。基於該思想寫程式就好比在設計一條流水線,是一種機械式的思維方式。
  優點:複雜的過程流程化,進而簡單化
  缺點:擴充性差

2、物件導向的程式設計

  物件導向:核心是對象二字,對象是特徵與技能的結合體。基於該思想編寫程式就好比在創造一個世界,世界是由一個個對象組成,是一種“上帝式”的思維方式
  優點:可擴充性強
  缺點:編程複雜度高,容易出現過度設計問題

二、類與對象

  對象是特徵和技能的結合體,類就是一系列對象相似的特徵與技能的結合體。

  • 現實世界中:一定是現有的一個個具體存在的對象,後總結出的類
  • 在程式中:一定保證先定義類,後產生對象
    對象1:        特徵:            學校=清華            名字=lionel            性別=男            年齡=18        技能:            學習            選課    對象2:        特徵:            學校=清華            名字=xiaohong            性別=女            年齡=28        技能:            學習            選課    對象3:        特徵:            學校=清華            名字=daming            性別=男            年齡=38        技能:            學習            選課
現實世界中站在學校的角度
程式中的學校學生類:    清華學生類        相似的特徵            學校=清華        相似的技能            學習            選課
程式中的學校學生類
# 類體代碼在類的定義階段就會立刻執行class Student:    school=‘oldboy‘    def learn(self):        print(‘is learning‘)    def choose_course(self):        print(‘choose course‘)    print(‘=====run‘)print(Student.__dict__)
python中類與對象三、屬性尋找1、類屬性:資料屬性、函數屬性
  • 類的資料屬性是所有對象共用的,id都是一樣的。
  • 類的函數屬性是綁定給對象用的,稱為Binder 方法,記憶體位址都不一樣。
2、執行個體屬性:資料屬性3、類屬性的增刪改查
class Student:    school=‘清華‘    def learn(self):        print(‘is learning‘)    def choose_course(self):        print(‘choose course‘)    print(‘=====run‘)# 查看類屬性print(Student.school) # 資料屬性print(Student.learn) # 函數屬性"""=====run清華<function Student.learn at 0x101a60488>"""# 增加類屬性Student.country=‘China‘print(Student.country)"""=====runChina"""# 修改類屬性Student.school=‘北大‘print(Student.school)"""=====run北大"""# 刪除類屬性del Student.countryprint(Student.country)
類屬性的增刪改查4、類與對象練習
class Student:    school=‘oldboy‘    def __init__(self,name,age,sex,course):        self.name=name        self.age=age        self.sex=sex        self.course=course    def xue_xi():        print(‘學習使我快樂‘)    def xuan_ke(self):        print(‘我叫%s,選擇學習%s‘ %(self.name,self.course))s1=Student(‘lionel‘,18,‘male‘,‘python‘)print(s1.school)print(Student.__dict__)print(s1.__dict__)Student.xue_xi()Student.xuan_ke(s1)s1.xuan_ke()"""清華{‘__module__‘: ‘__main__‘, ‘school‘: ‘清華‘, ‘__init__‘: <function Student.__init__ at 0x101c60488>, ‘xue_xi‘: <function Student.xue_xi at 0x101c60598>, ‘xuan_ke‘: <function Student.xuan_ke at 0x101c60620>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Student‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Student‘ objects>, ‘__doc__‘: None}{‘name‘: ‘lionel‘, ‘age‘: 18, ‘sex‘: ‘male‘, ‘course‘: ‘python‘}學習使我快樂我叫lionel,選擇學習python我叫lionel,選擇學習python"""
練習四、類的三大特性1、靜態屬性:既能訪問到類屬性也能訪問到執行個體屬性
class Room:    def __init__(self,name,owner,width,length,heigh):        self.name=name        self.owner=owner        self.width=width        self.length=length        self.heigh=heigh    @property # 既能訪問到類屬性也能訪問到執行個體屬性    def cal_area(self):        # print(‘%s 住的 %s 總面積是 %s‘ %(self.owner,self.name,self.length*self.width))        return self.length*self.widthr1=Room(‘學區房‘,‘lionel‘,100,100,100)print(r1.cal_area)
靜態屬性2、類方法:只能訪問到類屬性,不能訪問到執行個體屬性
class Room:    tag=1    def __init__(self,name,owner,width,length,heigh):        self.name=name        self.owner=owner        self.width=width        self.length=length        self.heigh=heigh    @property # 既能訪問到類屬性也能訪問到執行個體屬性    def cal_area(self):        return self.length*self.width    @classmethod #只能訪問到類屬性,不能訪問到執行個體屬性    def tell_info(cls,x):        print(‘--->‘,cls.tag,x)Room.tell_info(10)"""---> 1 10"""
類方法3、靜態方法:既不能訪問到類屬性,也不能訪問到執行個體屬性
class Room:    tag = 1    def __init__(self, name, owner, width, length, heigh):        self.name = name        self.owner = owner        self.width = width        self.length = length        self.heigh = heigh    @property # 既能訪問到類屬性也能訪問到執行個體屬性    def cal_area(self):        return self.length * self.width    @classmethod # 只能訪問到類屬性,不能訪問到執行個體屬性    def tell_info(cls, x):        print(‘--->‘, cls.tag, x)    @staticmethod # 既不能訪問到類屬性,也不能訪問到執行個體屬性    def student(a,b,c):        print(‘%s %s %s正在學習‘ %(a,b,c))Room.student(‘daming‘,‘xiaohong‘,‘lionel‘)r1=Room(‘學區房‘,‘lionel‘,100,100,100)r1.student(‘alex‘,‘egon‘,‘lionel‘)"""daming xiaohong lionel正在學習daming xiaohong lionel正在學習"""
靜態方法

  注意:靜態方法和類方法雖然是給類準備的,但是如果執行個體去用,也是可以用的,只不過執行個體去調用的時候容易讓人混淆,不知道你要幹啥。

五、組合什麼是組合?

  組合指的是,在一個類中以另外一個類的對象作為資料屬性,稱為類的組合。

class School:    def __init__(self,name,addr):        self.name=name        self.addr=addr    def zhao_sheng(self):        print(‘%s 正在招生‘ %self.name)class Course:    def __init__(self,name,price,period,school):        self.name=name        self.price=price        self.period=period        self.school=schools1=School(‘oldboy‘,‘北京‘)s2=School(‘oldboy‘,‘南京‘)s3=School(‘oldboy‘,‘上海‘)msg="""1 老男孩 北京校區2 老男孩 南京校區3 老男孩 上海校區"""while True:    print(msg)    menu={        ‘1‘:s1,        ‘2‘:s2,        ‘3‘:s3    }    choice=input(‘選擇學校>>: ‘)    school_obj=menu[choice]    name=input(‘課程名>>: ‘)    price=input(‘課程費用>>: ‘)    period=input(‘課程周期>>: ‘)    new_course=Course(name,price,period,school_obj)    print(‘課程【%s】屬於【%s】【%s】學校‘ %(new_course.name,new_course.school.name,new_course.school.addr))
組合

 

Python學習筆記-物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.