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. 同名域中计算机之间RDP问题

    今天遇到一个奇葩问题 server1 在domain1中 server2 在domain2中 domain1 和domain2的名字一样,然后从server1去RDP到server2,你是无论如何都无 ...

  2. android学习笔记一——简介

    android 是由Andy Rubin创立的一个手机操作系统,后被google收购. google希望同各方共同建立一个标准化.开放式的移动电话软件平台,从而在移动产业内形成了一个开放式的操作平台. ...

  3. golang rbac框架

    在 https://github.com/mikespook/gorbac/tree/v1.0 github上新的版本是开发板,得用这里的老版 demo package main import ( & ...

  4. NodeJS的异步编程风格

    NodeJS的异步编程风格 http://www.infoq.com/cn/news/2011/09/nodejs-async-code NodeJS运行环境因其支持Javascript语言和异步编程 ...

  5. 如何在VBA窗体中使用 DataGrid 控件?

    1.下载MSDATGRD.cab,下载路径如下:http://www.findthatzipfile.com/search-10500733-hZIP/winrar-winzip-download-m ...

  6. flash读取XML节点内容以及节点属性

    原文地址:http://hi.baidu.com/yqzdm/item/f95fd9d24679d916d90e44c9 一.xml的写法: 这里的xml只是在有限范围内的了解,限于写一些简单的用于f ...

  7. flash builder 启动ios模拟器失败是什么原因?

    参考知乎:http://www.zhihu.com/question/22537362 在mac os设置-安全性与隐私-隐私-辅助功能 找到flash bulder 打上前面的勾,如下图:

  8. IOS中UIWebView停止加载

    方法一: [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank" ...

  9. 黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性

    <? /* * 参考资料: * http://www.phpddt.com/manual/simplehtmldom_1_5/manual_api.htm * http://www.phpddt ...

  10. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...