AI智能
改变未来

python进阶(6)–类

文档目录:

一、创建类

二、使用类与实例

三、继承

—————————————分割线:正文——————————————————–

一、创建类

1、类由类名(首字母大写)、类的实例、类的方法组成

2、举例创建类

class Dog():\"\"\"模拟小狗的简单尝试\"\"\"def __init__(self,name,age):\"\"\"初始化属性name,age\"\"\"self.name=nameself.age=agedef sit(self):\"\"\"小狗蹲下\"\"\"print(f\"{self.name} is now sitting.\")def roll_over(self):\"\"\"小狗打滚\"\"\"print(f\"{self.name} rolled over\")my_dog=Dog(\'xiaobai\',12)print(f\'My dog\\\'s name is {my_dog.name}.\')print(f\'My dog is {my_dog.age} years old\')

查看结果

My dog\'s name is xiaobai.My dog is 12 years old

二、使用类与实例
1、创建实例(使用Dog类)

my_dog=Dog(\'xiaobai\',12)

2、使用方法(使用Dog类)

my_dog.sit()my_dog.roll_over()

3、给属性指定默认值(使用Car类)

class Car:\"\"\"汽车信息\"\"\"def __init__(self,make,model,year):self.make=makeself.model=modelself.year=year#给默认值self.odometer_reading=0def get_descriptive_name(self):\"\"\"返回描述性信息\"\"\"long_name=f\"{self.year} {self.make} {self.model}\"return long_name.title()def read_odometer(self):\"\"\"打印汽车的里程\"\"\"print(f\"This car has {self.odometer_reading} miles on it.\")my_new_car=Car(\'audi\',\'a4\',2019)print(my_new_car.get_descriptive_name())my_new_car.read_odometer()

查看结果:

2019 Audi A4This car has 0 miles on it.

4、修改属性的值(使用Car类)

my_new_car.odometer_reading=3000my_new_car.read_odometer()

查看结果:

This car has 3000 miles on it.

5、通过方法修改属性的值(使用Car类)

#新增修改里程数的方法def update_odometer(self,mileage):\"\"\"将里程表读数设置为指定值\"\"\"self.odometer_reading=mileage

查看实例并调用方法

my_new_car=Car(\'audi\',\'a4\',2019)my_new_car.update_odometer(50)my_new_car.read_odometer()

查看结果:

This car has 50 miles on it.

三、继承

1、子类的init()方法

from test06_class_car import Car
#ElectricCar继承Carclass ElectricCar(Car):\"\"\"电动汽车\"\"\"def __init__(self,make,model,year):\"\"\"初始化父类方法\"\"\"super().__init__(make,model,year)
#创建ElecticCar实例,赋给变量my_teslamy_tesla=ElectricCar(\'tesla\',\'model s\',2020)print(my_tesla.get_descriptive_name())

查看结果:

2020 Tesla Model S

2、子类定义属性的方法(使用ElectricCar类)

from test06_class_car import Carclass ElectricCar(Car):\"\"\"电动汽车\"\"\"def __init__(self,make,model,year):\"\"\"初始化父类方法\"\"\"super().__init__(make,model,year)#设置电瓶容量初始值self.battery_size=75#新增方法def describe_battery(self):\"\"\"打印电瓶容量信息\"\"\"print(f\"This car has a {self.battery_size} -kwh battery.\")#创建ElecticCar实例,赋给变量my_teslamy_tesla=ElectricCar(\'tesla\',\'model s\',2020)my_tesla.describe_battery();

查看结果:

This car has a 75 -kwh battery.

3、重写父类的方法

#Car类新增方法:油箱def fill_gas_tank(self):print(\"This car needs a gas tank!\")

在子类中重新父类的方法,python会执行子类的方法,忽略父类的

#重写父类方法def fill_gas_tank(self):\"\"\"电动车没有油箱\"\"\"print(\"This car doesn\'t need a gas tank!\")

实例化并调用重写的方法

my_tesla=ElectricCar(\'tesla\',\'model s\',2020)my_tesla.fill_gas_tank()

查看结果:

This car doesn\'t need a gas tank!

4、将实例用作属性

from test06_class_car import Carclass ElectricCar(Car):\"\"\"电动汽车\"\"\"def __init__(self,make,model,year):\"\"\"初始化父类方法\"\"\"super().__init__(make,model,year)#设置电瓶容量初始值self.battery=Battery()

class Battery:\"\"\"电动汽车电瓶的信息\"\"\"def __init__(self,battery_size=75):\"\"\"初始化电瓶的属性\"\"\"self.battery_size=battery_sizedef describe_battery(self):\"\"\"打印电瓶容量信息\"\"\"print(f\"This car has a {self.battery_size} -kwh battery.\")def get_range(self):\"\"\"打印电瓶的续航里程\"\"\"if self.battery_size==75:range=260elif self.battery_size==100:range=315print(f\"This car can go about {range} miles on a full charge\")#创建ElecticCar实例,赋给变量my_teslamy_tesla=ElectricCar(\'tesla\',\'model s\',2020)  my_tesla.battery.describe_battery()my_tesla.battery.get_range()

查看结果:

This car has a 75 -kwh battery.This car can go about 260 miles on a full charge

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » python进阶(6)–类