019: class, objects and instance: property

属性在本质上来讲是一组方法,但是调用的时候却如同字段,换句话说,其实就是对字段的一种封装,在设定和读取的时候,可以很轻易的添加逻辑,而其调用方式其不会改变

在Pyhon中可以用@property来定义:

class Book(object):
    def __init__(self, title, price):
        self._title = title
        self._price = price

    @property
    def price(self):
        return "${}".format(self._price)        

    @price.setter
    def price(self, value):
        self._price = value    

    @price.deleter
    def price(self):
        del self._price    

book = Book("Python Basic", 100)

print(book.price)        

book.price = 200
print(book.price)    

del book.price
print(book.price)
    

运行结果:

$100
$200
Traceback (most recent call last):
  File "C:\Users\Miles\python\class_object\20160125_1.py", line 26, in <module>
    print(book.price)
  File "C:\Users\Miles\python\class_object\20160125_1.py", line 8, in price
    return "${}".format(self._price)
AttributeError: ‘Book‘ object has no attribute ‘_price‘
时间: 2024-08-08 02:09:35

019: class, objects and instance: property的相关文章

017: class, objects and instance: class method

类的方法 所谓类的方法,也就是,这个方法会绑定到一个类上面,实例化一个instance的时候,这个方法不会再重新生成 一份,它只有访问类级别的变量 它用@classmethod标签来标注这是一个class method. class Book(object): num = 10 # instance method, will be bound to an object def __init__(self, title, price): self.title = title self.price

018: class, objects and instance: static method

静态方法 使用@staticmethod来标记,该方法与某一个class或者某一个实例无关系,但可以用类名或者实例来调用,实际上这种写法应该一般只是为了组织代码. 实例方法的调用需要一个实例,声明时需要用self参数,调用时隐式传入该实例 类的方法调用需要一个类,其实该类本质上也是一个对象,声明时需要class参数,调用时隐式传入该类 静态方法调用不需要实实例也不需要类,其实该方法本质上与该类毫无关系,唯一的关系就是名字会该类有些关系, 用途应该只是为了组织一些代码,本质上和面向对象无关 cla

016: class, objects and instance: method

在Python的世界里,实际上class也是对象,object也是对象,因此这里加了一个概念,实例 1. 实例方法 所谓的实例方法,也就是,这个方法会绑定到一个instance上面,这个方法一般是需要访问这个instance的数据. 该实例方法,类是依然存在一份的方法定义的,只是实例化一个类的时候,也会重新生成一个方法定义,和类的方法分别存储在不同的地址. class Book(object): # instance method, will be bound to an object def

021: class, objects and instance: 特殊方法

所有的特殊的方法,都是以如下格式存在: __method_name__ 1. __str__ 如果一个类的定义当中有这样一个方法,则str(instance)的时候,会默认调用该实例的__str__方法 如果没有定义__str__方法,则默认返回该对象的地址: class Employee(object): def __init__(self, name): super(Employee, self).__init__() self.name = name def __str__(self):

020: class, objects and instance: 一个简单的例子,压缩文件中内容的替换

这个例子是对前面学习的知道的一个简单总结. 在设计类的时候,并非所有的类都是埋头干活的类,同时也需要有很多类似于管理的类,这样的类的功能就是调用其他的类来共同的完成任务. import sys import os import shutil import zipfile class ZipReplace(object): def __init__(self, file_name, search_string, replace_string): self.file_name = file_name

python 描述符 上下文管理协议 类装饰器 property metaclass

1.描述符 #!/usr/bin/python env # coding=utf-8 # 数据描述符__get__ __set__ __delete__ ''' 描述符总结 描述符是可以实现大部分python类特性中的底层魔法,包括@classmethod,@staticmethd,@property甚至是__slots__属性 描述符是很多高级库和框架的重要工具之一,描述符通常是使用到装饰器或者元类的大型框架中的一个组件 注意事项: 一 描述符本身应该定义成新式类,被代理的类也应该是新式类 二

进击的雨燕--------------协议

详情转自:http://wiki.jikexueyuan.com/project/swift/chapter2/07_Closures.html 协议定义了一个蓝图,规定了用来实现某一特定工作或者功能所必需的方法和属性.类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能够满足协议要求的类型被称为遵循(conform)这个协议. 除了遵循协议的类型必须实现那些指定的规定以外,还可以对协议进行扩展,实现一些特殊的规定或者一些附加的功能,使得遵循的类型能够收益. 协议

Objective-C Programming The Big Nerd Ranch Guide 笔记 19-37

Properties are either atomic or nonatomic, The difference has to do with multithreading. atomic is the default value. Properties are either readonly or readwrite. readwrite is the default value. Whenever you declare a property that points to an NSStr

[翻译] CNPPopupController

CNPPopupController CNPPopupController is a simple and versatile class for presenting a custom popup in a variety of fashions. It includes a many options for controlling how your popup appears and behaves. Please feel free to contribute to this projec