美文网首页
Python-类的创建和使用

Python-类的创建和使用

作者: 小小白的jotter | 来源:发表于2021-12-21 15:30 被阅读0次

创建和使用类

创建一个宠物 Pet 类,根据该类创建的每个实例都将存储名字和物种,赋予每个宠物吃饭(eat())和睡觉(sleep())的能力:

class Pet():

    def __init__(self, name, species):
        self.name = name
        self.species = species
        
    def eat(self):
        print(self.name.title() + ' is now eating')
        
    def sleep(self):
        print(self.name.title() + ' is now sleeping')

__init__()为类中的函数,称为方法,开头和末尾各有两个下划线

形参 self 必不可少,且必须位于其他形参之前,指向创建实例本身。

根据类创建实例

my_pet = Pet('ben', 'cat')
print("My pet's name is " + my_pet.name.title() + ".")
print("My Pet is a " + str(my_pet.species) + ".")
image-20211220143052069

调用方法

my_pet.eat()
my_pet.sleep()
image-20211220143124774

使用类和实例

设置默认值

给上面的例子进行补充,设置一个宠物到家的初始时间默认值为 0,对某个属性设定默认值可以不提供形参。

class Pet():

    def __init__(self, name, species):
        self.name = name
        self.species = species
        self.initial_time = 0
        
    def eat(self):
        print(self.name.title() + ' is now eating')
        
    def sleep(self):
        print(self.name.title() + ' is now sleeping')
        
    def read_time(self):
        print("My pet has come to my home for " + str(self.initial_time) + " years.")

调用一下read_time()

image-20211221094954411

修改属性的值

直接修改属性的值

my_pet = Pet('ben', 'cat')
my_pet.initial_time = 5
my_pet.read_time()
image-20211221095905957

通过方法修改属性的值

增加一个 update_time() 的方法,指定初始时间

class Pet():

    def __init__(self, name, species):
        self.name = name
        self.species = species
        self.initial_time = 0

    def eat(self):
        print(self.name.title() + ' is now eating')

    def sleep(self):
        print(self.name.title() + ' is now sleeping')

    def read_time(self):
        print("My pet has come to my home for " + str(self.initial_time) + " years.")

    def update_time(self, time):
        self.initial_time = time

调用

my_pet = Pet('ben', 'cat')
my_pet.update_time(5)
my_pet.read_time()
image-20211221101734667

通过方法对属性的值进行递增

新增一个 increment_time() 方法,表示较初始值而言增加的时间

class Pet():

    def __init__(self, name, species):
        self.name = name
        self.species = species
        self.initial_time = 0

    def eat(self):
        print(self.name.title() + ' is now eating')

    def sleep(self):
        print(self.name.title() + ' is now sleeping')

    def read_time(self):
        print("My pet has come to my home for " + str(self.initial_time) + " years.")

    def update_time(self, time):
        self.initial_time = time
        
    def increment_time(self,year):
         self.initial_time += year

调用

my_pet = Pet('ben', 'cat')
my_pet.update_time(3) # 指定初始3年
my_pet.read_time()
my_pet.increment_time(2) # 后来又增加了2年
my_pet.read_time()
image-20211221105421293

继承

一个类继承另一个类时,它将自动获得另一个类的所有属性和方法;原有的类称为父类,而新类称为子类。子类继承了其父类的所有属性和方法,同时还可以定义自己的属性和方法。

创建一个 Cat 类,继承 Pet 的属性,同时可以定义 Cat 特有的属性。

class Pet():

    def __init__(self, name, species):
        self.name = name
        self.species = species

    def eat(self):
        print(self.name.title() + ' is now eating')

    def sleep(self):
        print(self.name.title() + ' is now sleeping')

class Cat(Pet):

    def __init__(self, name, species):
        super().__init__(name, species)
        self.favourite_food = 'fish'

    def describe_food(self):
        print("The " + str(self.species) + "'s favourite food is " + str(self.favourite_food) + ".")

调用

my_cat = Cat("ben","cat")
my_cat.describe_food()
image-20211221142525181

如果子类中的某些属性与父类不一样,可以在子类新建方法重新定义来改写父类。

类的导入和函数的导入相同。

参考书目:Python编程从入门到实践 - Eric Matthes 著,袁国忠 译

相关文章

  • Python-类的创建和使用

    创建和使用类 创建一个宠物 Pet 类,根据该类创建的每个实例都将存储名字和物种,赋予每个宠物吃饭(eat())和...

  • 第32课:类

    预习: class、__init__、 9.1 创建和使用类 9.1.1创建Dog类 2,在Python2.7中创...

  • python(17):类(1)

    1.创建和使用类 2.使用类和实例

  • 创建和使用类

  • ExtJS的学习和使用-2

    1类的声明和调用 2创建和使用

  • 共读Python编程-类卡

    创建和使用类 创建类 使用class关键字定义类 类名后使用冒号结束 方法init()init方法是类的构造函数,...

  • 类的创建和使用

    类的创建 lazy property:调用的时候才初始化 Type property:static 关键字 比较两...

  • python 字典

    1. 字典的背景 2. 创建和使用字典 2. 创建和使用字典 2.2.1 使用dict类来创建 2.2.2 字典的...

  • python 收藏链接

    python主题 深刻理解Python中的元类 使用pipenv管理你的项目 Python-基础-数据结构小结 P...

  • 线程

    如何创建和使用线程 继承 Thread 类。 实现 Runnable 接口。 使用 Callable 和 Futu...

网友评论

      本文标题:Python-类的创建和使用

      本文链接:https://www.haomeiwen.com/subject/lgexqrtx.html