Python类

大城市里の小女人 提交于 2020-02-20 00:28:25

Python类

1 创建类

class Dog():
    """模拟小狗"""
    
    def __init__(self,name,age):
        """初始化属性"""
        self.name = name
        self.age = age
        
    def sit(self):
        """模拟蹲下"""
        print(self.name + " 蹲下 !")
        
    def roll_over(self):
        """模拟小狗打滚"""
        print(self.name + " 滚一个 !")

2 根据类创建实例

class Dog():
    ...snip...

my_dog = Dog('小白',3)
print("My dog's name is " + my_dog.name)
print("My dog is " + str(my_dog.age) + " years old.")

2.1 访问属性

my_dog.name

class Dog():
    ...snip...

my_dog = Dog('小白',3)
print("My dog's name is " + my_dog.name)

2.2 调用方法

my_dog.sit()
my_dog.roll_over()

class Dog():
    ...snip...

my_dog = Dog('小白',3)
my_dog.sit()
my_dog.roll_over()

2.3 创建多个实例

class Dog():
    ...snip...

my_dog = Dog('小白',3)
your_dog = Dog('小黄',2)

 

3 类与实例

class Car():
    """模拟汽车"""

    def __init__(self,make,model,year):
        """初始化属性"""
        self.make = make
        self.model = model
        self.year = year

    def get_descriptive_name(self):
        """返回描述汽车信息"""
        long_name = self.make + ' ' + self.model + ' ' + self.year
        return  long_name.title()

my_car = Car('tesla','S','2016')
print(my_car.get_descriptive_name())

3.1 给属性指定默认值

self.odometer_reading = 20

class Car():
    """模拟汽车"""

    def __init__(self,make,model,year):
        """初始化属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 20

    def get_descriptive_name(self):
        """返回描述汽车信息"""
        long_name = self.make + ' ' + self.model + ' ' + self.year
        return  long_name.title()

    def read_odometer(self):
        """打印里程"""
        print("This car has " + str(self.odometer_reading) + " miles on it.")

my_car = Car('tesla','S','2016')
print(my_car.get_descriptive_name())
my_car.read_odometer()

3.2 修改属性值

【1】直接修改

my_car.odometer_reading = 90

class Car():
    """模拟汽车"""

    def __init__(self,make,model,year):
        """初始化属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 20

    def get_descriptive_name(self):
        """返回描述汽车信息"""
        long_name = self.make + ' ' + self.model + ' ' + self.year
        return  long_name.title()

    def read_odometer(self):
        """打印里程"""
        print("This car has " + str(self.odometer_reading) + " miles on it.")

my_car = Car('tesla','S','2016')
print(my_car.get_descriptive_name())
my_car.odometer_reading = 90
my_car.read_odometer()

【2】通过方法修改

my_car.update_odometer(88)

class Car():
    """模拟汽车"""

    def __init__(self,make,model,year):
        """初始化属性"""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 20

    def get_descriptive_name(self):
        """返回描述汽车信息"""
        long_name = self.make + ' ' + self.model + ' ' + self.year
        return  long_name.title()

    def read_odometer(self):
        """打印里程"""
        print("This car has " + str(self.odometer_reading) + " miles on it.")

    def update_odometer(self,mile):
        self.odometer_reading = mile

my_car = Car('tesla','S','2016')
print(my_car.get_descriptive_name())
my_car.update_odometer(88)
my_car.read_odometer()

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!