Week 4 Study Materials
# Creates class Car
class Car:
# create class attributes
name = "c200"
make = "mercedez"
model = 2008
# create class methods
def start(self):
print ("Engine started")
def stop(self):
print ("Engine switched off")
Review Questions
- How is object-oriented programming related to prodedural programming?
- What is a class?
Is like an object constructor, or a "blueprint" for creating objects.
Example : - What is an attribute?
Describe the class ex: name, make, model - What is a method?
A method is like a function but it uses objects and Classes used to call an object - What is a constructor? is the method we use to initiialize the class attributes
- What is an object?
- What is the syntax for defining a class?
- What is the syntax for defining an attribute?
- What is the syntax for defining a method?
- What is the syntax for creating an object?
- What is the syntax for invoking a method?
Questions:
defining a class
class ClassName:
Syntax for attribute
# create class attributes
name = "c200"
make = "mercedez"
model = 2008
syntax for defining a method
# create class methods
def start(self):
print ("Engine started")
def stop(self):
print ("Engine switched off")
create an object
# # create new instance rect1 and rect2 or objects
# rect1 = Rectangle()
# rect2 = Rectangle()
Invoke a method
expr.methodname(additional parameter values)