前言:
现在同学们对“python中类的基本特征”大概比较关注,兄弟们都需要知道一些“python中类的基本特征”的相关资讯。那么小编也在网络上收集了一些关于“python中类的基本特征””的相关内容,希望同学们能喜欢,兄弟们一起来了解一下吧!# _*_ coding:utf-8 _*_
##########类############
#创建和使用类
#创建Dog类---在Python中,首字母大写的名称指类
#方法_init_()是一个特殊的方法,每当你根据Dog类创建新实例时,Python都会自动运行它
#以self为前缀的变量都可供类中的所有方法使用
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self,name,age):
"""初始化属性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指的是根据类创建的实例
my_dog = Dog('willie', 6)
print("My dog's name is " + my_dog.name.title() + ".")
print("My dog is " + str(my_dog.age) + " years old.")
-------------------------------------------
My dog's name is Willie.
My dog is 6 years old.
-------------------------------------------
#调用方法
my_dog.sit()
my_dog.roll_over()
-------------------------------------------
Willie is now sitting.
Willie rolled over!
-------------------------------------------
#创建多个实例
your_dog = Dog('lucy', 3)
print("\nYour dog's name is " + your_dog.name.title() + ".")
print("Your dog is " + str(your_dog.age) + " years old.")
your_dog.sit()
-------------------------------------------
Your dog's name is Lucy.
Your dog is 3 years old.
Lucy is now sitting.
-------------------------------------------
#使用类和实例
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) + " miles 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', 'a4', 2016)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
-------------------------------------------
2016 Audi A4
This car has 0 miles on it.
-------------------------------------------
#修改属性的值
#直接修改属性的值
my_new_car.odometer_reading = 23
my_new_car.read_odometer()
-------------------------------------------
This car has 23 miles on it.
-------------------------------------------
#通过方法修改属性值
my_new_car.update_odometer(34)
my_new_car.read_odometer()
-------------------------------------------
This car has 34 miles on it.
-------------------------------------------
#通过方法对属性的值进行递增
my_used_car = Car('subaru', 'outback', 2013)
print(my_used_car.get_descriptive_name())
my_used_car.update_odometer(23500)
my_used_car.read_odometer()
my_used_car.increment_odometer(100)
my_used_car.read_odometer()
-------------------------------------------
2013 Subaru Outback
This car has 23500 miles on it.
This car has 23600 miles on it.
-------------------------------------------
#继承---一个类继承另一个类时,它将自动获得另一个类的所有属性和方法,原有的类称为父类,而新类称为子类
#子类的方法__init__
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():
"""电动汽车没有油箱"""
print("This car doesn't need a gas tank!")
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
#给子类定义属性和方法
my_tesla.describe_battery()
-------------------------------------------
2016 Tesla Model S
This car has a 70-kwh battery.
-------------------------------------------
#重写父类的方法
#将实例用作属性
class Battery():
"""一次模拟电动汽车电瓶的简单尝试"""
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.")
def get_range(self):
"""打印一条信息,指出电瓶的续航里程"""
if self.battery_size == 70:
range = 240
elif self.battery ==85:
range = 270
message = "This car go approximately " + str(range)
message += " miles on a full charge."
print(message)
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())
#这行代码让Python在实例my_tesla中查找属性battery,并对存储在该属性中的Battery实例调用方法describe_battery()
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
-------------------------------------------
2016 Tesla Model S
This car has a 70-kwh battery.
This car go approximately 240 miles on a full charge.
-------------------------------------------
#导入类
#导入单个类
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()
-------------------------------------------
2016 Audi A4
This car has 23 miles on it.
-------------------------------------------
#在一个模块中存储多个类
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()
-------------------------------------------
2016 Tesla Model S
This car has a 70-kwh battery.
This car go approximately 240 miles on a full charge.
-------------------------------------------
#从一个模块中导入多个类
from car import Car, ElectricCar
my_beetle = Car('vplkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
-------------------------------------------
2016 Vplkswagen Beetle
2016 Tesla Model S
-------------------------------------------
#导入整个模块
import car
my_beetle = car.Car('vplkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())
my_tesla = car.ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
-------------------------------------------
2016 Vplkswagen Beetle
2016 Tesla Model S
-------------------------------------------
#在一个模块中导入另外一个模块
from car import Car
from electric_car import ElectricCar
my_beetle = Car('vplkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
-------------------------------------------
2016 Vplkswagen Beetle
2016 Tesla Model S
-------------------------------------------
#Python标准库
from collections import OrderedDict
favorite_languages = OrderedDict()
favorite_languages['jen'] = 'python'
favorite_languages['sarah'] = 'c'
favorite_languages['edward'] = 'ruby'
favorite_languages['phil'] = 'python'
for name, language in favorite_languages.items():
print(name.title() + "'s favorite language is " +
language.title() + ".")
-------------------------------------------
Jen's favorite language is Python.
Sarah's favorite language is C.
Edward's favorite language is Ruby.
Phil's favorite language is Python.
-------------------------------------------
标签: #python中类的基本特征