• 面向对象类成员之特性


    1. 类中的特性关键字 @property

    访问特性

    2. 关键字@end.setter

    设置特性

    3.普通方法需要加()来执行方法

    4.特性,不需要加()来执行,相当于通过字段来访问,即将方法伪造成一种字段,但是缺点是不能传参数。

    class Provice:
        # 静态字段,类中
        country = "China"
    
        def __init__(self, name):
            temp = "xxx"
            # 普通字段,对象中(self就是对象)
            self.name = name
    
        # 普通方法,类中
        def show(self):
            print("show")
    
        # 静态方法,()中没有self;但是可以传值
        # 静态方法属于类,通过类调用。
        @staticmethod
        def xo(bk):
            print("xo")
            print(bk)
    
        # 类方法
        @classmethod
        def xxoo(cls):
            print("xxoo", cls)
    
        # 普通方法
        def start(self):
            temp = "%s is sb" % self.name
            return temp
    
        # 特性,将方法伪造成一种字段。
        # 访问特性
        @property
        def end(self):
            temp = "%s is smart" % self.name
            return temp
    
        # 设置特性
        @end.setter
        def end(self, value):
            print(value)
            self.name = value
    
    
    obj = Provice("alex")
    ret1 = obj.start()  # 加()来执行start方法
    ret2 = obj.end  # 没有加(),就可以执行end方法。相当于通过字段来访问了。但是缺点是不能传参数。
    
    # 当需要对特性设置时。需要紧跟着再定义一个end的方法。因为前面没有接受参数的地方。
    # 关键字@end.setter
    obj.end = "xxx" # 在设置特性中打印"xxx"
    print(obj.end)  # 首先执行访问特性(obj.end),得到返回值,temp的值,然后执行(print(obj.end))
  • 相关阅读:
    关于Intent
    k8s常用命令
    kube-ui安装
    配置k8s dns
    centos 7 部署k8s集群
    多进程multiprocessing模块
    queue
    github安装k8s
    错误: No API token found for service account "default",
    线程
  • 原文地址:https://www.cnblogs.com/xuwenwei/p/9784517.html
Copyright © 2020-2023  润新知