·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 *


文章评论