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. MATLAB GUI对话框设计(转载)

    原文地址:http://blog.csdn.net/shuziluoji1988/article/details/8532982 1.公共对话框: 公共对话框是利用windows资源的对话框,包括文件 ...

  2. 【maven】将jar安装到maven本地仓库

    Maven 安装 JAR 包的命令是:  mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的ar ...

  3. 黄聪:主机宝安装wordpress注意事项

    1.web环境安装PHP使用5.4.21-nts-03版本 2.web环境安装Mysql使用5.5.45版本 3.创建好站点后,给站点的public_html目录添加IIS_xxx用户最高权限,添加N ...

  4. 黄聪:如何使用WebKitBrowser调用元素点击事件(C#)

    string s = "var _elm = document.getElementById('loginBtn');var _evt = document.createEvent('Mou ...

  5. PLSQL_查询已执行SQL的绑定参数(案例)

    2014-12-19 Created By BaoXinjian

  6. Report_客制化Excel报表中的XLS标记(案例)

    2014-06-06 Created By BaoXinjian

  7. codeforces 431 D. Random Task 组合数学

    题意: 给定m,k 0 <= m <= 10^18 ,1 <= k <= 64 求一个数n,满足n+1,n+2,...n+n这n个数中,刚好有m个数的2进制表示法刚好有k个1 ...

  8. UITouch 触摸事件处理(实例)

    来源:http://www.open-open.com/lib/view/open1341882439838.html 1. UITouch 的主要方法: - (void)touchesBegan:( ...

  9. rails中两种回滚-reversible和revert区别

    1 通常迁移内容写在change方法中 ,但是有些迁移内容不能自动通过执行rake:rollback回滚, 所以在迁移文件里要使用 reversible 方法,告诉rails如何回滚例如下面 # co ...

  10. 20145305 《Java程序设计》第5周学习总结

    教材学习内容总结 1.设计错误对象都继承自java.lang.Throwable类 2.Throwable有两个子类:java.lang.Error与java.lang.Exception 3.Err ...