第九章 类

第九章 类

根据类来创建对象称为实例化。

9.1创建和使用类

9.1.1创建Dog类

class Dog():
	"""一次模拟小狗的简单尝试"""
	def __init__(self , name , age):
		self.name = name
		self.age = age
	
	def sit(self):
		print(self.name.title() + " is sitting .")

	def roll_over(self):
		print(self.name.title() + "rolled over !")


my_dog = Dog('white', 6)
print("my dog's name is " + my_dog.name.title() + ".")
print("my dog is " + str(my_dog.age) + " years old.")

首字母大写的名称指的是类。这个类定义中的括号是空的。

方法__init__()

类中的函数称为方法。__init__()是一个特殊的方法,每当根据Dog类创建新实例时,Python都会自动运行它,旨在避免Python默认方法与普通方法名称冲突。每个与类相关联的方法调用都自动传递实参self,它是一个指向实例本身的引用,让实例能够访问类中的属性和方法。
定义的两个变量都有前缀self,以self为前缀的变量都可供类中的所有方法使用,同时还可以通过类的任何实例来访问这些变量。self.name = name获取存储在形参name中的值,并将其存储到变量name中,然后该变量被关联到当前创建的实例。

9.1.2根据类创建实例

1.访问属性
my_dog = Dog('white', 6)
my_dog.name
2.调用方法
my_dog = Dog('white', 6)
my_dog.sit()
3.创建多个实例
my_dog = Dog('white', 6)
my_dog = Dog('black', 7)

9.2使用类和实例

9.2.1Car类

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")

my_new_car = Car('audi','a4','2016')
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()

9.2.2 修改属性的值

1直接修改属性的值
my_new_car.odometer_reading= 23
my_new_car.read_odometer()
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)  + " miles on it")
	
	def update_odometer(self, mileage):
		self.odometer_reading = mileage

my_new_car = Car('audi','a4','2016')
print(my_new_car.get_descriptive_name())
my_new_car.update_odometer(23)
my_new_car.read_odometer()
3通过方法对属性的值进行递增
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):
		self.odometer_reading = mileage

	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.update_odometer(23)
my_new_car.increment_odometer(5)
my_new_car.read_odometer()

9.3继承

创建子类的实例是,Python首先需要完成的任务是给父类的所有属性赋值

9.3.1子类的方法__init__()

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):
		self.odometer_reading = mileage

	def increment_odometer(self, miles):
		self.odometer_reading += miles

class EletricCar(Car):
	def __init__(self, make, model, year)
		super().__init__(make, model, year)

9.3.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)  + " miles on it")
	
	def update_odometer(self, mileage):
		self.odometer_reading = mileage

	def increment_odometer(self, miles):
		self.odometer_reading += miles

class EletricCar(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.")
	
	
my_tesla = EletrricCar('tesla','model s','2019')
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()

假设Car类中有一个名为fill_gas_tank()的方法,Python将忽略Car类中的方法fill_gas_tank(),
转运EletricCar(Car)中的fill_gas_tank()

9.3.3重写父类的方法和将实例用作属性

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):
		self.odometer_reading = mileage

	def increment_odometer(self, miles):
		self.odometer_reading += miles
		
class Bettery():
	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 EletricCar(Car):
#将实例用作属性
	def __init__(self, make, model, year)
		super().__init__(make, model, year)
		self.battery_size= Bettery()
	
	def describe_battery(self):
		print("This car has a str(self.battery_size)" + "-KWh battery.")
	
	def fill_gas_tank(self):
	#重写父类的方法
		print("This car doesn't need a gas tank !")

9.4导入类

导入类类似导入函数,不赘述。
不推荐使用:

from module_name import *

原因有二:

  • 该方式没有明确地指出以使用了模块中的哪些类;
  • 引发名称困惑,如果导入一个与程序文件其他同名的类,将引发诊断错误。

9.5类编码风格

  • 类名应采用驼峰命名法,即将类名中的每个单词的首字母都大写,而不适用下划线。
  • 对于每个类,都应紧跟在类后面包含一个文档字符串。
  • 在类中,可使用一个空行来分割方法;在模块中,可使用两个空行来分割类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值