0% found this document useful (0 votes)
6 views

3 - Object Oriented Programming

This document discusses object-oriented programming concepts including classes, instances, attributes, methods, inheritance, polymorphism, and data encapsulation. It provides examples of defining classes for drawings, walls, and columns to illustrate these concepts.

Uploaded by

Vibhor Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

3 - Object Oriented Programming

This document discusses object-oriented programming concepts including classes, instances, attributes, methods, inheritance, polymorphism, and data encapsulation. It provides examples of defining classes for drawings, walls, and columns to illustrate these concepts.

Uploaded by

Vibhor Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Python Prototyping for Infrastructure Systems

Lesson 3: Object-Oriented Programming

Instructor: Xuesong (Pine) Liu

© 2015 Xuesong (Pine) Liu


Please do not copy or post on the web without permission
Outline
• Object-Oriented Programming
– Why Object-oriented?
– Class and instances
– Attributes and methods
– Inheritance
What is Object-Oriented Programming?

• Procedural • Object-Oriented
programming Programming

– Variable – Class and object


– Procedure/Function/ – Attribute and
Method behavior

– Hard to understand – Easy to understand

– Execution step by – Interactive methods


step call
Procedural programming codes can be
complicated
• Example codes – ProceduralExample.py
Why Object-Oriented?
• Main advantages

– Real-world concept
• Entity, attribute and relationship

– Reusability

– Package and library management


Overview of the Object-oriented Programming
• Object/Instance: the basic run-time units that represent actual
entities in the program
– One drawing
• Class: defines a template of the objects that have similar
attributes and behaviors
– The entity type - drawing
• Attributes and methods: represents the properties and behavior
of objects
– Building name, contractor, etc.
• Inheritance: enables objects to reuse the common attributes and
methods

• Polymorphism: enables operation to have different behaviors in


different instances

• Data Encapsulation: hides the complexity and details for simple


reuse
Class, Attribute and Method
• Class – define a type of concept
– Attribute: Properties and characteristics of the concept
– Method: Behavior of the concept

• Class only defines the characteristics of a type of


things. It is not a real entity.

• Object/Instance
– Represent the actual thing
– Cannot change the type of characteristics
– But can define the actual values
Define a class
• class class_name(parent_class_name):

variable1 = 0
variable2 = “Wall”

def method1(self, parameter1, …):


… Code
Use the class and instance
• Object/instance: An entity that is the type of a class.

• Declaration:
– object_name = class_Name();

• Use attribute and method


– Object_name.attribute_name;

– Object_name.method_name();
Example: Drawing class and instances
• Create a class to represent the attributes of drawings

• Create four instances of this class

• Create two methods for searching for drawings using


instances
Another example: Wall and column
• Attributes
– Material type – String
– Material unit price – number
– Labor unit price – number
– Labor productivity – number

• Method
– Calculate the total cost
Initialization method
• __init__()

• Provide initial values of the attributes to a new


instance of a class

• Make sure the new instance does not contain empty


values
Example
Inheritance!
• Very useful for reusability and managing the code

• Look at the class Wall and Column. See any common


attributes and methods?
Inheritance!
• Very useful for reusability and managing the code

• Look at the class Wall and Column. See any common


attributes and methods?
– Repeating the same/similar code is evil
• Have to change all of them
• Time-consuming
• Making the code seem naïve
– How about getting the common stuff out and put them in the
same place?
Class hierarchy
• Super class and subclass
More examples
• Let’s add a subclass for the wall called curvedWall
Manage multiple Python source code files
• Module and Package
– Module is a single Python file
– Any variable, methods and classes in a module can be
accessed if it’s imported
– Two ways to import a module or its content
• import Module_name: Import the entire module
• from Module_name import method, variable, etc.: Import
indiviudal method, variable or class in the module
– from Module_name import *: Import everything

– Examples
Package
• A mechanism for organizing the modules. Just like
the file folder for classes.

• Access the modules in a package


– Using the dot operator

• Examples
Method override – Polymorphism
• The method from a super class is modified in the
subclasses

• Simply re-write the exact same method but do some


different coding.

• One interface, multiple methods


– Add the equipment cost for the column.

You might also like