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>>
    ===============================
  • 相关阅读:
    HTML5与HTML的区别
    0918练习整理
    0904 未来展望
    ajax弹出窗口
    AjAX请求后台,无刷新更新页面
    Jquery通过Ajax方式来提交Form表单
    php的socket通信
    次短路[SPFA]
    [Usaco2008 Open]Roads Around The Farm分岔路口[水题]
    [Usaco2008 Nov]Guarding the Farm 保卫牧场[DFS]
  • 原文地址:https://www.cnblogs.com/jcsz/p/5155041.html
Copyright © 2011-2022 走看看