Object Slides - jude-lindale/Wiki GitHub Wiki
Objects
- An object is a structure for representing a concept, thing, or situation
- Attached to an object are definitional and descriptive information and how the object behaves
Classes
- Blueprint or description of each object
- the blueprint holds information that describes the object
- Definitional
- E.G. A square always has 4 sides
- Descriptive but allowed to change.
- E.G. A Swedish person has blue eyes.
- Behavior.
- E.G. A child knows how to walk.
Inheritance
- Key aspect
- Allows you to build a hierarchy of objects
- They inherit descriptions and functions from each other.
- E.G. A dining table inherits properties from table but has extra properties of its own.
- They inherit descriptions and functions from each other.
Instances
- Once you have defined the Class
- You can make instances
- My dining table is an instance of the dining table blueprint or class.
- Instances start with all the defined values and behaviors of the blueprint
Defining a Class
-
When creating a class, consider;
- all of its attributes
- the tasks you will need to do with the class
- the communication that will initiate those tasks
-
You construct a class in two parts
- a declaration section
- implementation section, which contains the functions themselves
-
when creating an object you are creating an instantiation of a class
class Pet: legs = 4 def talk(self): print("I am a virtual pet")
Class Attributes
-
Defined at the top level
-
Variable that is the same value for all instances
-
Can be changed bu it changes for all
legs = 4 Totalnumberofinstancescreated = 0 kind = 'canine'
Instance Methods
def talk(self):
print("I am a virtual pet")
- regurlar instance method
- takes one parameter,
self
, (at least) - Using
self
methods access attributes and other methods - They modify an object's state
- Can also access the class itself through the
self.__class__
attribute
Class Methods
@classmethod
def hello(cls):
print("Hello")
@classmethod
decorator flag- Can modify class but not instance state
Static Methods
@staticmetho
def staticmethod():
return 'static method called'
- called on a class
- do not have an associated object
- can neither modify object state nor class state
Instance Definition in Python
Having defined the pet
class Pet:
def talk(self):
print("I am a virtual pet")
pet1 = Pet() #instantiation
pet1.talk() #method invocation
Constructors
-
Special method that all classes have that is called on instance creation
-
usually called with new
-
in python
_init_(self)
-
there are several built in python methods
- start and end with _
- E.g. str to print something about an object
Example
class Critter(object):
def __init__(self, name):
print "A new critter has been born!"
self.name = name
def __str__(self):
rep = "Critter object\n"
rep += "name: " + self.name + "\n"
return rep
def talk(self):
print "Hi. I'm", self.name, "\n"
Data Hiding and Encapsulation
-
A major asset of object-oriented programming is that information hiding is more complete than with the procedures used in procedural programming
- You employ a primitive form of information hiding when you write a program using functions to which you know only the interface
-
private
- Inaccessible to functions not part of the class
-
Once a class is created, no outside function over which you have no control can ever modify or use private member data of any object in the class in an erroneous manner
-
When you create and test a class, and store its definition in a file, programs using that definition can be prevented from using member data incorrectly
-
Usually, you want at least some functions to be public
- Accessible by both member and nonmember functions
- Clearly defined Interface Python and privacy
-
Private attributes/methods start with __
-
(double underscore)
Self.name=name #public __Self._mood=mood #private Def __privatemethod(self)
get-set methods
def get_name(self):
return self.__name
def set_name(self, new_name):
if new_name == "":
print "A critter's name can't be the empty string."
else:
self.__name = new_name
print "Name change successful."