小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Python
  5. 正文

Python chapter 9 learning notes

2017年10月17日 1520点热度 0人点赞 0条评论

·CLASS

For example,

class Restaurant():
    def __init__(self, restaurant_name, cuisine_type):#DO NOT FORGET __init__
        self.name = restaurant_name
        self.type = cuisine_type
        
    def describe_restaurant(self):#DO NOT FORGET self
        print("This restaurant's name is " + self.name + ".")
        print("This restaurant's cuisine type is" + self.type + ".")
    
    def open_restaurant(self):#DO NOT FORGET self
        print("This restaurant is opening.")
 
op_rest = Restaurant("Chinese Restaurant", "LUCAI")
op_rest.describe_restaurant()
op_rest.open_restaurant()

 

This is a good example to show how to use CLASS in python.

Just add some sentences as followed, that shows some ways to modify elements.

class Car():
    
    def __init__(self, make, model, year):
        """初始化描述汽车的属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 100
        
    def get_descriptive_name(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()
    
    def read_odometer(self):
        print("This car has " + str(self.odometer_reading) + " miles on it.")
        
    def update_odometer(self, mileage):
        if mileage >= self.odometer_reading:
            self.odometer_reading = mileage
        else:
            print("Forrbideen")
            
    def increment_odometer(self, miles):
        self.odometer_reading += miles
    
my_car = Car('Toyota', 'GO9', 2017)
print(my_car.get_descriptive_name())
#my_car.odometer_reading = 100 #Direct assignment
you = input("Please input the miles.\n")
you = int(you)
#my_car.update_odometer(you) #Modify value by method
my_car.increment_odometer(you) #Adding by method
my_car.read_odometer()

·        Inheritance

class Car():
    
    def __init__(self, make, model, year):
        """初始化描述汽车的属性"""
        self.make = make
        self.model = model
        self.year = year
        
    def get_descriptive_name(self):
        long_name = str(self.year) + ' ' + self.make + ' ' + self.model
        return long_name.title()
 
        
    def fill_gas_tank(self):
        print("ONLY ONE")
        
class ElectricCar(Car): #Define Inheritance
    """电动汽车的独特之处"""
    def __init__(self, make, model, year):
        super().__init__(make, model, year)
        self.battery_size = 70 #Adding a property
        
    def describe_battery(self): #Adding a method
        """打印一条描述电瓶容量的信息"""
        print("This car has a " + str(self.battery_size) + "-KWh battery.")
        
    def fill_gas_tank(self):#If the parent class has something that it do not need, 
    #just define a method and overwriting.
        print("This car doesn't need a gas tank!")
 
my_tesla = ElectricCar('tesla', 'model s', 2017)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
my_tesla.fill_gas_tank()

o   In order to make codes more beautiful,put some properties that only electricCar has into a new class.

class Car():
--snip--
 
class Battery(): #A new class, and not belong to class Car.
    """一次模拟电动汽车电瓶的简单尝试"""
    def __init__(self, battery_size=70):
        """初始化电瓶的属性"""
        self.battery_size = battery_size
    def describe_battery(self):
        """打印一条描述电瓶容量的消息"""
        print("This car has a " + str(self.battery_size) + "-kWh battery.")
 
class ElectricCar(Car):
    """电动汽车的独特之处"""
    def __init__(self, make, model, year):
        """
        初始化父类的属性,再初始化电动汽车特有的属性
        """
        super().__init__(make, model, year)
        self.battery = Battery()
 
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()#可以直接用my_tesla.来调用battery()里面的方法
  • Import Class:

    • Import       single class

#外面有一个car.py文件,里面有一个Car类
from car import Car
 
my_new_car = Car('audi', 'a4', 2016)
print(my_new_car.get_descriptive_name())
my_new_car.odometer_reading = 23
my_new_car.read_odometer()
  • Import Many class in one module

#外面有一个car.py文件,里面有多个类,Car类,ElectricCar类
from car import ElectricCar
 
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

  • Import many class      from only one module

from car import Car, ElectricCar
 
my_beetle = Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())
my_tesla = ElectricCar('tesla', 'roadster', 2016)
print(my_tesla.get_descriptive_name())

  • Import all      module[RECOMMEND]

#外面有一个car.py文件,里面有多个类
import car
 
my_beetle = car.Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())
my_tesla = car.ElectricCar('tesla', 'roadster', 2016)
print(my_tesla.get_descriptive_name())

  • Import all class      from module[NOT RECOMMEND]

from module_name import *

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python python学习
最后更新:2017年10月17日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程小奥看房之鸿荣源珈誉府论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
换个风格,换个心情 想象(作者:汪峰) 想家(2010 S.V Beijing Travel ARTICE) 《优化阵列信号处理》学习笔记(第十章) 全国大学生英语四六级口语考生手册 在 Linux 下使用 CMake 构建应用程序
标签聚合
leetcode linux Java Python 高中 python学习 算法 学习 生活 鸟哥的linux私房菜
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号