• <noscript id="e0iig"><kbd id="e0iig"></kbd></noscript>
  • <td id="e0iig"></td>
  • <option id="e0iig"></option>
  • <noscript id="e0iig"><source id="e0iig"></source></noscript>
  • Python基礎+數據科學入門(六)類

    標簽: python基礎(數據學科入門)

    聲明:該博客參考深度之眼的視頻課程,如有侵權請聯系小編刪除博文,謝謝! 若總結有所失誤,還請見諒,并歡迎及時指出。

    類-面向對象編程

    在這里插入圖片描述

    #創建類
    class Cat():         #初始化屬性
        
         def __init__(self, name):     #init兩側有2個下劃線
                
            self.name = name
            
         def jump(self):    #模擬貓的跳躍
            
            print(self.name + " is jumping")
            
    #用類創建實例
    my_cat = Cat("Loser")
    your_cat = Cat("Lucky")
    
    #調用屬性
    print(my_cat.name)
    print(your_cat.name)
    
    my_cat.jump()
    your_cat.jump()
    

    1.1 類

    三要素:類名、屬性、方法

    1.1.1 類的命名

    要有實際意義
    采用駝峰命名法

    #class 類名
    """類前空兩行"""
    
    
    class Car();
    """對該類的簡單介紹"""
        pass
    
    
    """類后空兩行"""
    

    1.1.2 類的屬性

    class __init__(self, brand, model, year):
        """初始化汽車屬性"""                  #相當于類的內部變量
        self.brand = brand                    #汽車的品牌
        self.model = model                    #汽車的型號         
        self.year = year                      #汽車的出廠年份
        self.mileage = 0                      #汽車總里程初始化為0
    

    1.1.3 類的方法

    #相對于類內部的定義
    class Car():
        """模擬汽車"""
        
        def __init__(self, brand, model, year):   #初始化汽車屬性
            self.brand = brand                    #汽車的品牌
            self.model = model                    #汽車的型號         
            self.year = year                      #汽車的出廠年份
            self.mileage = 0                      #汽車總里程初始化為0
            
        
        
        def get_main_information(self):      #self不能省
            """獲取汽車主要信息"""
            print("品牌:{} 型號:{} 出廠年份:{} ",format(self.brand, self.model, self.year))
            
            
        def get_mileage(self):
            """獲取總里程數"""
            return "行車總里程:{}公里".format(self.mileage)
    

    1.2 創建實例

    1.2.1 創建實例

    將實例賦值給對象,實例化過程中,傳入相應的參數
    v = 類名 (必要的初始化參數)

    my_new_car = Car("Audi","A6",2018)
    

    1.2.2 訪問屬性

    類名:屬性名

    print(my_new_car.brand)
    print(my_new_car.model)
    print(my_new_car.year)
    

    1.2.3 調用方法

    my_new_car = Car("Audi","A6",2018)
    my_new_car.get_main_information()
    s = my_new_car.get_mileage()
    print(s)
    

    1.2.4 屬性的修改

    1. 直接修改
    my_old_car = Car("BYD", "宋",2016)
    my_old_car.mileage = 12000
    print(my_old_car.mileage)
    print(my_old_car.get_mileage())
    
    1. 通過方法修改屬性
        def set_mileage(self, distance):
            self.mileage = distance
    
    my_old_car.set_mileage(8000)
    print(my_old_car.get_mileage())
    

    將每次的里程數累加

        def increment_mileage(self,distance):
            self.mileage = self.mileage + distance
    
    my_old_car.increment_mileage(500)
    print(my_old_car.get_mileage())
    

    小結

    my_cars = [my_new_car, my_old_car]    #列表
    

    1.3 類的繼承

    1.3.1 簡單的繼承

    父類

    class Car():
        """模擬汽車"""
        
        def __init__(self, brand, model, year):   #初始化汽車屬性
            self.brand = brand                    #汽車的品牌
            self.model = model                    #汽車的型號         
            self.year = year                      #汽車的出廠年份
            self.mileage = 0                      #汽車總里程初始化為0
            
        
        
        def get_main_information(self):      #self不能省
            """獲取汽車主要信息"""
            print("品牌:{} 型號:{} 出廠年份:{} ".format(self.brand, self.model, self.year))
            
            
        def get_mileage(self):
            """獲取總里程數"""
            return "行車總里程:{}公里".format(self.mileage)
        
        def set_mileage(self, distance):
            if distance < 0:
                print("里程數不能為負!")
            else:
                self.mileage = distance
                
        def increment_mileage(self,distance):
            self.mileage = self.mileage + distance
    

    子類
    class 子類名(父類名):

    • 新建一個電動汽車的類
    class ElectricCar(Car):
        """模擬電動汽車"""
        
        def __init__(self, brand, model, year):
            super().__init__(brand, model, year)   #聲明繼承父類的屬性
    
    my_electric_car = ElectricCar("FF91","Tomorrow", 2048)
    my_electric_car.get_main_information()
    

    1.3.2 給子類添加屬性和方法

    class ElectricCar(Car):
        """模擬電動汽車"""
        
        def __init__(self, brand, model, year, bettery_size):
            super().__init__(brand, model, year)   #聲明繼承父類的屬性
            self.bettery_size = bettery_size          #電池容量
            self.bettery_quantity = bettery_size      #電池剩余電量
            self.bettery2distance_ratio = 5           #電池距離換算系數  5公里/kw.h
            self.remainder_range = self.bettery_quantity*self.bettery2distance_ratio  #剩余可行駛里程
            
        def get_electric_quantity(self):   #查看當前電池電量
            print("當前電池剩余電量: {} kw.h".format(self.bettery_quantity))   
            
        def set_electric_quantity(self, bettery_quantity):       #設置當前電池電量
            if bettery_quantity < 0 or bettery_quantity > self.bettery_size:
                print("電量未設置在合理范圍內!")
            else:
                self.bettery_quantity = bettery_quantity
                self.remainder_range = self.bettery_quantity*self.bettery2distance_ratio
                
                
        def get_remainder_range(self):
            print("當前電量還可以繼續行駛{}公里!".format(self.remainder_range))
        
    my_electric_car = ElectricCar("FF91","Tomorrow", 2048, 70)
    my_electric_car.get_main_information()
    my_electric_car.get_electric_quantity()
    my_electric_car.set_electric_quantity(50)
    my_electric_car.get_remainder_range()     
    

    1.3.3 重寫父類的方法–多態

            
        def get_main_information(self):       #重寫父類方法
            print("品牌:{} 型號:{} 出廠年份:{} 續航里程 {} 公里".format(self.brand, self.model, self.year, self.bettery_size))
    

    1.3.4 用在類中的實例

    把電池抽象成一個對象

    class Bettrry():
        
        def __init__(self, bettery_size = 70):
            self.bettery_size = bettery_size          #電池容量
            self.bettery_quantity = bettery_size      #電池剩余電量
            self.bettery2distance_ratio = 5           #電池距離換算系數  5公里/kw.h
            self.remainder_range = self.bettery_quantity*self.bettery2distance_ratio  #剩余可行駛里程
            
        def get_electric_quantity(self):   #查看當前電池電量
            print("當前電池剩余電量: {} kw.h".format(self.bettery_quantity))   
            
        def set_electric_quantity(self, bettery_quantity):       #設置當前電池電量
            if bettery_quantity < 0 or bettery_quantity > self.bettery_size:
                print("電量未設置在合理范圍內!")
            else:
                self.bettery_quantity = bettery_quantity
                self.remainder_range = self.bettery_quantity*self.bettery2distance_ratio
                
                
        def get_remainder_range(self):
            print("當前電量還可以繼續行駛{}公里!".format(self.remainder_range)) 
            
            
    class ElectricCar(Car):
        """模擬電動汽車"""
        
        def __init__(self, brand, model, year, bettery_size):
            super().__init__(brand, model, year)   #聲明繼承父類的屬性
            self.bettery = Bettrry(bettery_size)
            
        def get_main_information(self):       #重寫父類方法
            print("品牌:{} 型號:{} 出廠年份:{} 續航里程 {} 公里".format(self.brand, self.model, self.year, self.bettery.bettery_size*self.bettery.bettery2distance_ratio))
            
            
    my_electric_car = ElectricCar("FF91","Tomorrow", 2048, 70)
    my_electric_car.get_main_information()
    my_electric_car.bettery.set_electric_quantity(50)
    my_electric_car.bettery.get_electric_quantity()
    my_electric_car.bettery.get_remainder_range()
    
    版權聲明:本文為qq_38425288原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
    本文鏈接:https://blog.csdn.net/qq_38425288/article/details/108767997

    智能推薦

    HTML中常用操作關于:頁面跳轉,空格

    1.頁面跳轉 2.空格的代替符...

    freemarker + ItextRender 根據模板生成PDF文件

    1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...

    電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!

    Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...

    requests實現全自動PPT模板

    http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...

    猜你喜歡

    Linux C系統編程-線程互斥鎖(四)

    互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...

    統計學習方法 - 樸素貝葉斯

    引入問題:一機器在良好狀態生產合格產品幾率是 90%,在故障狀態生產合格產品幾率是 30%,機器良好的概率是 75%。若一日第一件產品是合格品,那么此日機器良好的概率是多少。 貝葉斯模型 生成模型與判別模型 判別模型,即要判斷這個東西到底是哪一類,也就是要求y,那就用給定的x去預測。 生成模型,是要生成一個模型,那就是誰根據什么生成了模型,誰就是類別y,根據的內容就是x 以上述例子,判斷一個生產出...

    styled-components —— React 中的 CSS 最佳實踐

    https://zhuanlan.zhihu.com/p/29344146 Styled-components 是目前 React 樣式方案中最受關注的一種,它既具備了 css-in-js 的模塊化與參數化優點,又完全使用CSS的書寫習慣,不會引起額外的學習成本。本文是 styled-components 作者之一 Max Stoiber 所寫,首先總結了前端組件化樣式中的最佳實踐原則,然后在此基...

    基于TCP/IP的網絡聊天室用Java來實現

    基于TCP/IP的網絡聊天室實現 開發工具:eclipse 開發環境:jdk1.8 發送端 接收端 工具類 運行截圖...

    19.vue中封裝echarts組件

    19.vue中封裝echarts組件 1.效果圖 2.echarts組件 3.使用組件 按照組件格式整理好數據格式 傳入組件 home.vue 4.接口返回數據格式...

    精品国产乱码久久久久久蜜桃不卡