zoukankan      html  css  js  c++  java
  • python类学习

    创建关于汽车的类

    class Cars():
    
        def __init__(self, brand, country):
            self.brand = brand
            self.country = country
    
        def show_brand(self):
            print(self.brand.title() + " is a popular automobile brand.")
    
        def show_country(self):
            print("Its headquater is in " + self.country.title() + ".")
    
    
    my_car = Cars('toyota', 'Japan')
    my_car.show_brand()
    my_car.show_country()

    输出

    Toyota is a popular automobile brand.
    Its headquater is in Japan.
    View Code

     列子

    class Restaurant():
        def __init__(self, restaurant_name, cuisine_type):
            self.restaurant_name = restaurant_name
            self.cuisine = cuisine_type
    
        def describe_restaurant(self):
            print("There is a restaurant called " + self.restaurant_name.title() + ", its cuisine is " + self.cuisine.title() + ".")
    
        def open_restaurant(self):
            print("The restaurant " + self.restaurant_name.title() + " is opening...")
    
    
    restaurant = Restaurant('xianghui xin', 'xiang cuisine')
    restaurant.describe_restaurant()
    print("I like " + restaurant.restaurant_name.title() + '.')
    restaurant.open_restaurant()
    print("Let's go " + restaurant.restaurant_name.title() + " and have lunch.")

    输出

    There is a restaurant called Xianghui Xin, its cuisine is Xiang Cuisine.
    I like Xianghui Xin.
    The restaurant Xianghui Xin is opening...
    Let's go Xianghui Xin and have lunch.
    View Code

     类的默认属性值和修改方法1

    class Car():
        def __init__(self, make, model, year):
            """初始化汽车描述属性"""
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0
    
        def read_odometer(self):
            print("My car is " + self.make.title() + " " + self.model.title() + ", production date is " + str(self.year) + ".")
            print("This car has " + str(self.odometer_reading) + "KM mileage on it.")
    
    
    my_new_car = Car('audi', 'a8l', 2019)
    my_new_car.read_odometer()
    
    my_new_car.odometer_reading = 1000
    my_new_car.read_odometer()

    输出

    My car is Audi A8L, production date is 2019.
    This car has 0KM mileage on it.
    My car is Audi A8L, production date is 2019.
    This car has 1000KM mileage on it.
    View Code

    修改方法2

    class Car():
        """一次模拟汽车的简单尝试"""
        def __init__(self, make, model, year):
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0
    
        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) + "KM mileage on it.")
    
        def update_odometer(self, mileage):
            if mileage >= self.odometer_reading:
                self.odometer_reading = mileage
            else:
                print("You can't roll back an odometer !")
    
        def increment_odometer(self, miles):
            self.odometer_reading += miles
    
    
    my_new_car = Car('audi', 'a8l', 2019)
    get_descriptive_name = my_new_car.get_descriptive_name()
    print(get_descriptive_name)
    my_new_car.read_odometer()
    my_new_car.update_odometer(100)
    my_new_car.read_odometer()
    my_new_car.update_odometer(90)
    my_new_car.increment_odometer(10)
    my_new_car.read_odometer()

    输出

    2019 Audi A8L
    This car has 0KM mileage on it.
    This car has 100KM mileage on it.
    You can't roll back an odometer !
    This car has 110KM mileage on it.
    View Code

    父类和子类

    class Car():
        """传统汽车的属性"""
        def __init__(self, make, model, year):
            self.make = make
            self.model = model
            self.year = year
            self.odometer_reading = 0
    
        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) + "KM mileage on it.")
    
        def update_odometer(self, mileage):
            if mileage >= self.odometer_reading:
                self.odometer_reading = mileage
            else:
                print("You can't roll back an odometer !")
    
        def increment_odometer(self, miles):
            self.odometer_reading += miles
    
    
    class ElectricCar(Car):
        """电动汽车的独特属性"""
        def __init__(self, make, model, year):
            super().__init__(make, model, year)
            self.battery = 70
    
        def describe_battery(self):
            print("This car has a " + str(self.battery) + "-kWh battery.")
    
    
    my_car = Car('audi', 'a8l', 2019)
    get_my_car = my_car.get_descriptive_name()
    print(get_my_car)
    my_car.read_odometer()
    
    my_tesla = ElectricCar('tesla', 'mode s', 2019)
    get_my_tesla = my_tesla.get_descriptive_name()
    print(get_my_tesla)
    my_tesla.describe_battery()

    输出

    2019 Audi A8L
    This car has 0KM mileage on it.
    2019 Tesla Mode S
    This car has a 70-kWh battery.
    View Code

       

  • 相关阅读:
    李航统计学习方法(第二版)(六):k 近邻算法实现(kd树(kd tree)方法)
    ActiveMQ的安装和启动
    HTML select autofocus 属性
    macpath (File & Directory Access) – Python 中文开发手册
    Java Bitset类
    Linux zip命令
    HTML DOM Keygen 对象
    tanh (Numerics) – C 中文开发手册
    no-shadow (Rules) – Eslint 中文开发手册
    require-await (Rules) – Eslint 中文开发手册
  • 原文地址:https://www.cnblogs.com/ilifeilong/p/12078588.html
Copyright © 2011-2022 走看看