'''
定义类:
类中的函数称为方法。函数的一切都适合方法,不同的是调用方法的方式。
可通过实例访问的变量称为 属性
self 代表类的实例,self 在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数。
类的实例化类似函数调用方式
'''
class Dog:#类的首字母大写#
def init(self, name, age):#inti特殊的方法,避免默认方法与普通方法名称发生冲突#
self.name = name#可通过实例访问的变量称为 属性 #
self.age = age
def sit(self):
print(self.name.title()+'is now sitting.')
def roll_over(self):
print(self.name.title()+'rolled over!')
'''
根据类创建实例
'''
class Dog():
def init(self,name,age):
self.name = name
self.age = age
def sit(self):
print(self.name.title()+' is now sitting.')
def roll_over(self):
print(self.name.title()+' rolled over!')
my_dog = Dog('willie',6)
print('My dogs name is '+my_dog.name.title())#my_dog.name访问属性# print('My dog is '+str(my_dog.age)+'years old.') my_dog.sit()#调用方法# my_dog.roll_over() your_dog = Dog('lucy',3)#创建多个实例# print('your dog
s name is '+your_dog.name.title())#my_dog.name访问属性#
print('your dog is '+str(your_dog.age)+'years old.')
'''
使用类和实例
'''
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()
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
'''给属性指定默认值,每个属性都必须初始化'''
class Car():
'''初始化属性'''
def init(self,make,model,year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0#在init()已经做了初始化,所以括号内无需包含形参#
'''返回整洁描述性信息'''
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.')
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
'''修改属性的值:通过实例直接访问'''
my_new_car.odometer_reading = 23#直接在类外通过实例修改#
my_new_car.read_odometer()
'''修改属性的值:通过方法修改属性的值'''
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 update_odometer(self,mileage):#mileage是要修改的属性值#
self.odometer_reading = mileage
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print('you can`t roll back an odometer')
'''通过方法对属性的值进行递增'''
def increase_odometer(self,miles):#mile是要递增的值#
self.odometer_reading += miles
def read_odometer(self):
print('This car has '+str(self.odometer_reading)+' miles on it.')
my_new_car = Car('audi','a4',2016)
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(26)#通过方法修改属性的值#
my_new_car.read_odometer()
'''通过方法对属性的值进行递增'''
my_new_car.increase_odometer(100)
my_new_car.read_odometer()
'''继承'''
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 update_odometer(self, mileage):
self.odometer_reading = mileage
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print('you can`t roll back an odometer')
'''通过方法对属性的值进行递增'''
def increase_odometer(self, miles):
self.odometer_reading += miles
def read_odometer(self):
print('This car has ' + str(self.odometer_reading) + ' miles on it.')
'''子类:继承父类的基础上再编写特殊的属性及方法'''
class ElectricCar(Car):#子类具备父类所有的功能。定义子类时必须在括号中指定父类#
'''子类的独特之处'''
def __init__(self, make, model, year):
'''初始化父类的属性'''
super().__init__(make, model, year)#super().__init__()特殊函数,将子类父类联系起来。父类也称超类#
self.battery_size = 70 #定义子类的属性#
'''描述电瓶的信息'''
def describe_battery(self):
print('This car has a '+str(self.battery_size)+'-kwh battery.')
my_tesla = ElectricCar('telsa', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()
'''重写父类的方法:父类有些方法不符合子类模拟的实物行为则在子类中定义这个方法,他与父类同名'''
class Car():
'''初始化属性'''
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
self.size = 25
'''返回整洁描述性信息'''
def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
'''更新属性的值'''
def update_odometer(self, mileage):
self.odometer_reading = mileage
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print('you can`t roll back an odometer')
'''通过方法对属性的值进行递增'''
def increase_odometer(self, miles):
self.odometer_reading += miles
'''油箱'''
def fill_gas_tank(self):
print('This car has a '+str(self.size)+' tank')
def read_odometer(self):
print('This car has ' + str(self.odometer_reading) + ' miles on it.')
class ElectricCar(Car):
def init(self, make, model, year):
'''初始化父类的属性'''
super().init(make, model, year)
self.battery_size = 70
'''描述电瓶的信息'''
def describe_battery(self):
print('This car has a '+str(self.battery_size)+'-kwh battery.')
def fill_gas_tank(self):#修改父类的行为或属性。子类与父类同名,所以可以覆盖父类放入方法#
print('This car has no tank')
my_tesla = ElectricCar('telsa', 'model s', 2016)
my_tesla.fill_gas_tank()
'''
将实例用作属性:
大类拆分成小类:将方法提取出来作为另一个类
'''
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 update_odometer(self, mileage):
self.odometer_reading = mileage
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print('you can`t roll back an odometer')
'''通过方法对属性的值进行递增'''
def increase_odometer(self, miles):
self.odometer_reading += miles
def read_odometer(self):
print('This car has ' + str(self.odometer_reading) + ' miles on it.')
class Battery():#将方法提取出来单独作为一个类。它没有继承类#
def init(self,battert_size = 70):#电池容量可选,默认为70#
self.battery_size = battert_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()#创建一个新的实例并存储在属性self.battery中#
my_tesla = ElectricCar('telsa', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
'''
导入类:将类存储在模块中然后在主程序中导入所需的模块
'''
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()
'''在同一个模块中存储多个类'''
from car import ElectricCar
my_tesla = ElectricCar('telsa', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
'''从一个模块中导入多个类'''
from car import Car,ElectricCar#从一个模块中导入多个类#
my_tesla = Car('volkswagen','beetle',2016)
print(my_tesla.get_descriptive_name())
my_tesla = ElectricCar('telsa','roadster',2016)
print(my_tesla.get_descriptive_name())
'''导入整个模块'''
import car
my_tesla = Car('volkswagen','beetle',2016)
print(my_tesla.get_descriptive_name())
my_tesla = ElectricCar('telsa','roadster',2016)
print(my_tesla.get_descriptive_name())
'''导入模块中的所有的类'''
from car import *
my_tesla = Car('volkswagen','beetle',2016)
print(my_tesla.get_descriptive_name())
my_tesla = ElectricCar('telsa','roadster',2016)
print(my_tesla.get_descriptive_name())
'''在一个模块中导入另一个模块 :一个模块中的类依赖于另一个模块中的类。
这种情况下可在前一个模块中导入必要的类。下例是在electric_car.py文件中先导入了
模块car中的父类Car,然后再在程序中导入父类所在的文件夹和所要导入的子类所在的文件夹。
'''
from car import Car
from electric_car import 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())
'''
有序添加字典的键值对
'''
'''from collection import OrderedDict#OrderedDict记录了键值对的添加顺序#
favorite_language = OrderedDict()
favorite_language['jen'] = 'python'
favorite_language['sarah'] = 'c'
favorite_language['edward'] = 'ruby'
favorite_language['phil'] = 'python'
for name,language in favorite_language.items():
print(name.title()+' `s favorite language is '+language.title())
'''
'''
类命名时首字母大写,不使用下划线。实例名,模块名都用小写,并在单词之间加下划线
类中每个方法间用一个空行,模块中,两个空行来分隔类
'''
网友评论