Python is an object-oriented programing language, which means that it provides features that support object-oriented programming. It is easy to define object-oriented programming, but we have already seen some of its characteristics:

  • Programs are made up of object definitions and function definitions, and most of the computation is expressed in terms of operations on objects.
  • Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.

For example, the Time class defined to the way people record the time of day, and the functions we defined correspond to the kinds of things people do with times. Similarly, the Point and Rectangle classes correspond to the mathematical concepts of a point and a rectangle.

So far, we have not taken advantage of the features Python provides to support object-oriented programming. Strictly speaking, these features are not necessary. For the most part, they provide an alternative syntax for things we have already done, but in many cases, the alternative is more concise and more accurately conveys the structure of the program.

For example, in the Time program, there is no obvious connection between the class definition and the function definitions that follow. With some examination, it is apparent that every function takes at least one Time object as an argument. This observation is the motivation for methods; a method is a function that is associated with a particular class. For example, we have seen methods for strings, lists, dictionaries and tuples. We will define methods for user-defined types.

Methods are semantically the same as functions, but there are two syntactic differences:

  • Methods are defined inside a class definition in order to make the relationship between the class and the method explicit.
  • The syntax for invoking a method is different from the syntax for calling a function.

Example:

class Time:
""" represents the time of day
attributes: hour, minute, second
method: print_time, increment, int_to_time
time_to_int, after """
def print_time(self):
print('%.2d:%.2d:%.2d' % (self.hour,self.minute,self.second)) @staticmethod
def int_to_time(seconds):
time = Time()
minute,time.second = divmod(seconds,60)
time.hour,time.minute = divmod(minute,60)
return time def time_to_int(self):
return self.second + self.minute*60 + self.hour*3600 def increment(self,t):
t1 = self.time_to_int()
t2 = t.time_to_int()
time = Time()
time = Time.int_to_time(t1+t2)
return time def after(self,t):
return self.time_to_int() > t.time_to_int() time = Time()
time.hour = 9
time.minute = 4
time.second = 0

from Thinking in Python

Object-oriented features的更多相关文章

  1. Object Oriented Programming python

    Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...

  2. What is Object Oriented Design? (OOD)

    Object Oriented Design is the concept that forces programmers to plan out their code in order to hav ...

  3. CSharpGL - Object Oriented OpenGL in C#

    Object Oriented OpenGL in C#

  4. Java - 面向对象(object oriented)计划 详细解释

    面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...

  5. OO开发思想:面向对象的开发方法(Object oriented,OO)

    面向对象的开发方法(Object oriented,OO)认为是好文章吧,拿来分享一下(转载) 面向对象的开发方法(Object oriented,OO) 从事软件开发的工程 师们常常有这样 的体会: ...

  6. JavaScript: Constructor and Object Oriented Programming

    Constructor :  Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...

  7. 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性

    一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...

  8. Python学习札记(三十) 面向对象编程 Object Oriented Program 1

    参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...

  9. 使用一个数组存储一个英文句子"java is an object oriented programing language"

    class fun { public static void main(String[] args) { String str="java is an object oriented pro ...

  10. 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记

    Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...

随机推荐

  1. (C#) System.BadImageFormatException: An attempt was made to load a program with an incorrect format.

    ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect for ...

  2. [jQuery].scrollTop() 函数详解

    scrollTop()函数用于设置或返回当前匹配元素相对于垂直滚动条顶部的偏移. 当一个元素的实际高度超过其显示区域的高度时,在一定的设置下,浏览器会为该元素显示相应的垂直滚动条.此时,scrollT ...

  3. [HTMLDOM]删除已有的 HTML 元素

    摘自www.w3school.com:http://www.w3school.com.cn/htmldom/dom_elements.asp如需删除 HTML 元素,您必须清楚该元素的父元素: < ...

  4. PO_PO系列 - 采购单管理分析(案例)

    2014-07-01 Created By BaoXinjian

  5. HDU 1532 (Dinic算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络 ...

  6. 别去研究C++

    转载 YH,今天早晨起来.回想昨天,虽然吐槽了 C++ 的各种问题,但给别人打工,还是要靠 C++ 干活吃饭.我对待 C++ 的态度和云风不同,虽然他所说的 C++ 技术的事情我都懂都理解,而我感受到 ...

  7. 30天轻松学习javaweb_http头信息实例

    package com.wzh.test.http; import java.io.ByteArrayOutputStream;import java.io.IOException;import ja ...

  8. wcf使用task实现异步调用

    private async void btnGetEmployees_Click(object sender, RoutedEventArgs e) { txtInfo.Text = "Da ...

  9. PHP 时区设置

    有时候使用date("Y-m-d h:i:s")时发现时间相差8小时,修改“/etc/php5/apache2/php.ini”: date.timezone = "As ...

  10. NodeJS常用工具

    一.NodeJS版本管理器n或者nvm npm install -g n npm install n -g --registry=https://registry.npm.taobao.org --v ...