• 技术文章 >Python技术 >Python基础教程

    python3类中的Dog如何用来转化?

    小妮浅浅小妮浅浅2020-11-16 13:49:54原创2692
    在python中我们把相似的一类事物归成一类,这样讲是不是就懂了呢?说到dog这个单词,大家都很熟悉,中文是小狗的意思。所以我们可以把不同种类的狗都归为一类。在python中的dog就类似于这种作用。小编相信这个举例大家都能明白,直接上定义理解了也记不长久,不如找点好玩的乐子博大家一笑。


    Dog类是Animal的一个子类,主要讲解三个装饰器进行方法向属性的转换。

    转换有两个主要的好处:

    一是调用没有参数的方法时不再需要加括号,这样的方法就可以当做属性来看

    二是这样定义的属性赋值时可以进行判断,防止无效属性的产生

    这样的转换有两种方式:

    一种是通过@property装饰器,这个装饰器系列一共三个,如果只是想调用这个方法可以只使用@property这个装饰器

    一种是通过property函数

    下面是一个例子,一些说明可以在最后面定义的print_dog方法中查看

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    class Dog(Animal): # 类的继承

         

        # 只使用@property装饰器与普通函数做对比

        def eating(self):

            print("I am eating")

         

        @property # 用这个装饰器后这个方法调用就可以不加括号,即将其转化为属性

        def running(self):

            if self.age >= 3 and self.age < 130:

                print("I am running")

            elif self.age > 0 and self.age <3:

                print("I can't run")

            else:

                print("please input true age")

                 

        # 三种装饰器,可以获取、设置、删除这样定义的属性

        @property

        def country(self):

            return self._country # 注意这个属性之前从来没有定义过,是在下面的setter中定义的

         

        @country.setter # 用 函数名.setter 的装饰器

        def country(self, value): # 设置这个属性的值

            self._country = value

             

        @country.deleter

        def country(self):

            print("The attr country is deleted")

             

             

        # 用property函数实现和装饰器相同的功能

        def get_city(self):

            return self._city

         

        def set_city(self, value):

            self._city = value

             

        def del_city(self, value):

            del self._city

             

        city = property(get_city, set_city, del_city, "where it is in")

             

        @staticmethod

        def print_dog():

            print("这是Animal的一个子类,主要讲解三个装饰器进行方法向属性的转换")

            print("类继承,创建实例时仍要指定父类的普通属性")

            print("@property装饰器将方法转化为属性方式调用,此时的方法必须只有一个self参数")

            print("使用@property后可以看做一个属性(country),用property函数可以达到相同的效果(city)")

            print("注:city中property第四个参数只是一个说明,用Dog.city.__doc__来调用,即返回 where it is in")


    创建实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    david = Dog("David", 2) # 创建实例

      

    # 只用@property的情形

    david.eating() # 调用普通方法

    # I am eating

    david.running # 用过@property装饰器后不需要加括号

    # I can't run

      

    dean = Dog("Dean", 4)

    dean.running # 在@property的属性中进行判断

    # I am running

      

      

    # @property等三个装饰器

    david.country = "America"

    print(david.country)

    del david.country # 如果这里的不出现_country则这样就可以删除,但是用self.country则真的变成了属性,所以为了区别多定义了一个_country

    del david._country # 如今需要再把这个中间变量删除掉才可以

    # 无法再调用 david.country

      

    # 不用装饰器,用函数的形式

    david.city = "Beijing"

    print(david.city) # Beijing

    在Python3类中dog转化的方法就讲到这里了,一种装饰器,一种函数的方法任由小伙伴们选择,小编在这里只作最常规的讲解,具体的使用还要大家亲自上手。

    专题推荐:python3类
    上一篇:python3类怎么学?可以应用于数学吗? 下一篇:python3类中的Cat变量使用有限制吗?

    相关文章推荐

    • python类可以继承吗• python类对象和实例对象是一样的吗• python类可以传递参数吗

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网