zoukankan      html  css  js  c++  java
  • 016: class, objects and instance: instance method

    在Python的世界里,实际上class也是对象,object也是对象,因此这里加了一个概念,实例

    实例方法

    所谓的实例方法,也就是,这个方法会绑定到一个instance上面,这个方法一般是需要访问这个instance的数据。

    该实例方法,类是依然存在一份的方法定义的,只是实例化一个类的时候,也会重新生成一个方法定义,和类的方法分别存储在不同的地址。

    class Book(object):
        # instance method, will be bound to an object
        def __init__(self, title, price):
            self.title = title
            self.price = price
        
        # instance method, will be bound to an object
        def display(self):
    
            print("
    *******************************")
            print("BOOK DETAILS:")
            print("Title is:", self.title)    
            print("Price is:", self.price)
            print("===============================
    ")
    
    book = Book("Python Basic", 25)
    
    book.display()
    Book.display(book)        
    
    print("
    *******************************")
    print("<instance method essence>")
    print(Book.display)
    print(book.display)
    print("===============================
    ")

    运行结果:

    *******************************
    BOOK DETAILS:
    Title is: Python Basic
    Price is: 25
    ===============================
    
    
    *******************************
    BOOK DETAILS:
    Title is: Python Basic
    Price is: 25
    ===============================
    
    
    *******************************
    <instance method essence>
    <function Book.display at 0x02275108>
    <bound method Book.display of <__main__.Book object at 0x022348F0>>
    ===============================
  • 相关阅读:
    《Head First》 MVC运用的设计模式
    unity工具 Animator的使用
    服务器搭建 如果搭建KBE开源服务器
    unity 实战图片挖洞Mask(转载)
    unity博客 推荐(不断补充)
    unity实战 UGUI英雄联盟英雄头顶分段式血条
    unity组成 ToLua
    unity实战 UGUI Text 间距和ContentSizeFitter组件的适配
    unity工具 推荐(不断补充)
    各种单例模式的对比分析
  • 原文地址:https://www.cnblogs.com/jcsz/p/5155041.html
Copyright © 2011-2022 走看看