Object-oriented features

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

时间: 2024-10-29 19:08:40

Object-oriented features的相关文章

Java Object Oriented Programming concepts

Introduction This tutorial will help you to understand about Java OOP'S concepts with examples. Let's discuss about what are the features of Object Oriented Programming. Writing object-oriented programs involves creating classes, creating objects fro

Object Oriented Programming python

new concepts of the object oriented programming : class encapsulation inheritance polymorphism the three features of an object are : identity, state and behaviora class is an abstraction which regroup objects who have the same attributes and the same

Go Object Oriented Design

Go hastypes and valuesrather than classes and objects. So can a language without classes or objects be object-oriented? While Go may not fit the typical mold of an OOP language, it does  provide many of the same features, albeit in a slightly differe

面向对象编程OOP Object oriented programing

oop是面向对象编程(设计) 面向对象程序设计(英语:Object Oriented Programming,缩写:OOP),指一种程序设计范型,同时也是一种程序开发的方法论.它将对象作为程序的基本单元,将程序和数据封装其中,以提高软件的重用性.灵活性和扩展性.基本理论 一项由 Deborah J. Armstrong 进行的长达40年之久的计算机著作调查显示出了一系列面向对象程序设计的基本理论.它们是: 类 类(Class)定义了一件事物的抽象特点.通常来说,类定义了事物的属性和它可以做到的(

CSE210 Advanced Object Oriented Programming

CSE210 Advanced Object Oriented ProgrammingCoursework 2019 Release date: 11th, Mar, 2019Deadline: 12:00PM, 23rd, Apr, 2019 1. DescriptionThe objective of the coursework is to develop a practical application for data processing, analysis and content s

MQF Object Oriented Programming

MQF Object Oriented Programming I Fall 2019Hw2 Due 10/1/2019 before midnightSpecificationsRutgers parking garage management system is required to take care Rutgers University Paringneeds. The system can keep track of all the cars parked in your garag

CSC72002 Object Oriented Programming

CSC72002 Object Oriented Programming - Assignment 2Weight: 40% of your final markSpecificationsYour task is to complete various exercises in NetBeans, using the Java language, and to submitthese via the MySCU link created for this purpose.Marking cri

5COSC001W Object Oriented

University of WestminsterSchool of Computer Science & Engineering5COSC001W Object Oriented Programming – Coursework 1 (2019/20)Module leader Barbara VillariniUnit Coursework 1Weighting: 50%Qualifying mark 30%DescriptionObject Oriented Programming and

what's the problem of Object oriented programming

The problem came from the Object itselft.It can divided into two aspect: 1.Everything is an Object: that's not true,lots of so called object is  not object ,it's just a Wrapper.You dont know what it is  ,but you just need one to let  things go on. Sa

面向对象程序设计(Object Oriented Design)

OOD的流程: 需求分析-->系统/程序设计-->实现这个设计-->测试 Class Design 1. Identify classes for the system. 2. Describe attributes and methods in each class. 3. Establish relationships among classes. 4. Create classes. Identity classes and objects A fundamental part o