zoukankan      html  css  js  c++  java
  • CP学习笔记(7)

    定义实例

    一个实例本质上是一个字典,里面包含的是:“方法名”-“函数”对

    def make_instance(cls):
            """Return a new object instance, which is a dispatch dictionary."""
            def get_value(name):
                if name in attributes:
                    return attributes[name]
                else:
                    value = cls['get'](name)
                    return bind_method(value, instance)
            def set_value(name, value):
                attributes[name] = value
            attributes = {}
            instance = {'get': get_value, 'set': set_value}
            return instance
            
    def bind_method(value, instance):
            """Return a bound method if value is callable, or value otherwise."""
            if callable(value):
                def method(*args):
                    return value(instance, *args)
                return method
            else:
                return value
    

    定义类

    def make_class(attributes, base_class=None):
            """Return a new class, which is a dispatch dictionary."""
            def get_value(name):
                if name in attributes:
                    return attributes[name]
                elif base_class is not None:
                    return base_class['get'](name)
            def set_value(name, value):
                attributes[name] = value
            def new(*args):
                return init_instance(cls, *args)
            cls = {'get': get_value, 'set': set_value, 'new': new}
            return cls
            
            
    def init_instance(cls, *args):
            """Return a new object with type cls, initialized with args."""
            instance = make_instance(cls)
            init = cls['get']('__init__')
            if init:
                init(instance, *args)
            return instance
    

    栗子

    def make_account_class():
            """Return the Account class, which has deposit and withdraw methods."""
            interest = 0.02
            def __init__(self, account_holder):
                self['set']('holder', account_holder)
                self['set']('balance', 0)
            def deposit(self, amount):
                """Increase the account balance by amount and return the new balance."""
                new_balance = self['get']('balance') + amount
                self['set']('balance', new_balance)
                return self['get']('balance')
            def withdraw(self, amount):
                """Decrease the account balance by amount and return the new balance."""
                balance = self['get']('balance')
                if amount > balance:
                    return 'Insufficient funds'
                self['set']('balance', balance - amount)
                return self['get']('balance')
            return make_class(locals()) 
    

    最后一句locals()返回一个当前框架下的字典。

    Account类可使用下句生成:

    >>> Account = make_account_class()
    

    子类关于继承的实现:

    def make_checking_account_class():
            """Return the CheckingAccount class, which imposes a $1 withdrawal fee."""
            interest = 0.01
            withdraw_fee = 1
            def withdraw(self, amount):
                fee = self['get']('withdraw_fee')
                return Account['get']('withdraw')(self, amount + fee)
            return make_class(locals(), Account)
    
    
    >>> CheckingAccount = make_checking_account_class() #子类的生成
    >>> jack_acct = CheckingAccount['new']('Spock') #子类实例的生成
    >>> jack_acct['get']('interest') #子类实例的方法调用
    0.01
    
  • 相关阅读:
    差一个引号的崩溃
    js中.toString()和String()的一丢丢区别
    PC端和手机端页面的一丢丢区别
    LINQ踩坑记录
    C# list group分组扩展,方法来源网络记录备忘
    NPOI分批读取数据
    Xamarin开发登录示例
    动态创建匿名对象利用表达式树动态构建分组条件
    安卓H5互调笔记
    WPF学习6
  • 原文地址:https://www.cnblogs.com/rim99/p/5045566.html
Copyright © 2011-2022 走看看