Day 7 python 从入门到实践——类

  • 类——一大类对象的通用行为或属性,根据实际对象可以调整

  • 创建Dog 类

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

-根据类创建实例

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实例
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 = Dog("willie",6)
print("My dog's name is "+my_dog.name.title()+" !")
print("My dog is "+str(my_dog.age)+" years old !")

-Car类实例——指定属性默认值,修改属性值

#Car类——为属性指定默认值,修改属性的值
class Car():
    def __init__(self,make,model,year):
        """初始化汽车属性"""
        self.year = year 
        self.model = model
        self.make = make
        self.odometer_reading = 0
    def get_descriptive_name(self):
        """返回整洁的汽车信息"""
        long_name = str(self.year)+" "+self.make.title()+" "+self.model.title()
        return long_name
    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):
        """将里程表读数增加指定的值"""
        if miles >= 0:
            self.odometer_reading+= miles
        else:
            print("You can't give a minus increment")
my_new_car = Car("audi","a4","2017")
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()

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(200)#通过方法递增属性的值
my_used_car.read_odometer()
  • 继承及相关概念
#继承——父类Car,子类Electric car继承Car的通用属性,并可以加入自己的特有属性
class Car():
    def __init__(self,make,model,year):
        """初始化汽车属性"""
        self.year = year 
        self.model = model
        self.make = make
        self.odometer_reading = 0
    def get_descriptive_name(self):
        """返回整洁的汽车信息"""
        long_name = str(self.year)+" "+self.make.title()+" "+self.model.title()
        return long_name
    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):
        """将里程表读数增加指定的值"""
        if miles >= 0:
            self.odometer_reading+= miles
        else:
            print("You can't give a minus increment")
class ElectricCar(Car):
    def __init__(self,make,model,year):
        """
        电动汽车的特殊属性
        """
        super().__init__(make, model, year)#super()特殊函数调用初始化父类中的通用属性
        self.battery_size = 70
    def describe_battery(self):
        """打印一条描述电瓶容量的属性"""
        print("This  car has a "+str(self.battery_size)+"-kWh battery .")

my_tesla = ElectricCar("tesla", "model s", 2016)
print(my_tesla.get_descriptive_name())
my_tesla.describe_battery()

#重写父类的方法——父类方法不符合子类行为时,可以在子类中定义一个同名的方法,调用时将忽略父类方法

#将实例用作属性——大类属性过多,将其划分为几个小的类包含相近属性
class Car():
    def __init__(self,make,model,year):
        """初始化汽车属性"""
        self.year = year 
        self.model = model
        self.make = make
        self.odometer_reading = 0
    def get_descriptive_name(self):
        """返回整洁的汽车信息"""
        long_name = str(self.year)+" "+self.make.title()+" "+self.model.title()
        return long_name
    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):
        """将里程表读数增加指定的值"""
        if miles >= 0:
            self.odometer_reading+= miles
        else:
            print("You can't give a minus increment")
my_new_car = Car("audi","a4","2017")
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()

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(200)#通过方法递增属性的值
my_used_car.read_odometer()

#继承——父类Car,子类Electric car继承Car的通用属性,并可以加入自己的特有属性
class Car():
    def __init__(self,make,model,year):
        """初始化汽车属性"""
        self.year = year 
        self.model = model
        self.make = make
        self.odometer_reading = 0
    def get_descriptive_name(self):
        """返回整洁的汽车信息"""
        long_name = str(self.year)+" "+self.make.title()+" "+self.model.title()
        return long_name
    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):
        """将里程表读数增加指定的值"""
        if miles >= 0:
            self.odometer_reading+= miles
        else:
            print("You can't give a minus increment")
class Battery():
    """模拟汽车电瓶"""
    def __init__(self, battery_size=70):#默认电瓶尺寸为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_size == 80:
            range = 270
        message = "This car can go approximately "+str(range)
        message+= "- miles on 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())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
  • 导入类
#导入单个类——在此之前应该存在一个car.py文件包含定义Car类代码
from car import Car
my_new_car = Car("audi", "a4", 2016)
print(my_new_car.get_descriptive_name())

from car import ElectricCar,Car,Battery
my_tesla = ElectricCar("tesla", "model s", 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

#导入整个模块
#import module_name
#调用 module_name.class_name()
import car
my_beetle = car.Car("volkswagen", "beetle", 2016)
print(my_beetle.get_descriptive_name())

my_tesla = car.Car("tesla", "model s", 2016)
print(my_tesla.get_descriptive_name())

#导入模块中所有类 *不推荐使用* 
#- 应用类名不清晰,不便于理解代码
#- 引起类名重复
#from module_name import *

你可能感兴趣的:(学习记录)