<< Chapter < Page Chapter >> Page >

In order to relate object-oriented programming to the real world, a car radio will be used to illustrate and discuss several aspects of software objects. For example, you will learn that car radios, as well as software objects, have the ability to store data, along with the ability to modify or manipulate that data. You will learn that car radios, as well as software objects, have the ability to accept messages and to perform an action, modify their state, return a value, or some combination of the above.

A simple Python program will be presented and explained to illustrate the definition and use of a Python class. This class simulates the manufacture, programming, and use of a car radio.

You will see the definition of a class named Radio . You will see how to write code that simulates pressing the scan button on the radio to learn about available radio stations in the area. You will see how to write code that simulates the association of a radio button with a particular radio station.You will see how to write code that simulates the pressing of a radio button to play the radio station associated with that button.

You will see how to write code to create new Radio objects. You will also see how to save these object's reference in reference variables and how to use those variablesto exercise the Radio objects.

You will learn some of the jargon used in OOP, including persistence, state, messages, methods, and behaviors. You will learn where objects come from, and you will learn that a class is a set of plans that can be used to construct objects.

You will learn that a Python object is an instance of a class. You will see Python code, used to create two objects from the same class, and then to send messages to each ofthose objects (call methods on objects) .

General background information

A class definition is a blueprint for objects

A class definition is a blueprint (set of plans) from which many individual objects can be constructed, created, produced, instantiated, orwhatever verb you prefer to use for building something from a set of plans.

An object is a programming construct that encapsulates data and the ability to manipulate that data in a single software entity.The blueprint describes the data contained within and the behavior of objects instantiated according to the class definition.

An object's data is contained in variables defined within the class (often called member variables, instance variables, data members, attributes, fields, properties, etc. ) . The terminology used in the literature often depends on the background of the person writing thedocument.

Note that unlike Java and C++, once a Python object is instantiated, it can be modified in ways that no longer follow the blueprint of the class through theaddition of new data members.

An object's behavior is controlled by methods defined within the class. In Python, methods are functions that are defined within a class definition.

An object is said to have state and behavior . At any instant in time, the state of an object is determined by the values stored in its variables and its behavior is determined by its methods.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask