CS115: Chapter 9
data hiding
Same as data-field encapsulation.
initializer
The initializer is always named __init__ for initializing the object.
What is the name of the initializer method?
The name of the initializer is __init__.
What is the object member access operator for?
The object member access operator is the dot (.)
instantiation
The process of creating an object of a class.
Analyze the following code: class A: def __init__(self, s): self.s = s def print(self): print(self.s) a = A() a.print()
The program has an error because the constructor is invoked without an argument. A() is wrong, because the __init__(self, s) requires an argument for s.
Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() print(a.__y)
The program has an error because y is private and cannot be access outside of the class.
Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() print(a.x)
The program runs fine and prints 1.
Analyze the following code: class A: def __init__(self): self.x = 1 self.__y = 1 def getY(self): return self.__y a = A() a.x = 45 print(a.x)
The program runs fine and prints 45.
Analyze the following code: class A: def __init__(self, s = "Welcome"): self.s = s def print(self): print(self.s) a = A() a.print()
The program runs fine and prints Welcome.
client
The program that uses the class is often referred to as the client for the class.
Analyze the following code: class A: def __init__(self, s): self.s = s def print(self): print(s) a = A("Welcome") a.print()
The program will be correct if you change print(s) to print(self.s). s in print(s) is not defined. You have use self.s to reference it.
The first parameter in the initializer method is named self by convention. What is the role of self?
The self refers to the object itself. Through self, the members of the object can be accessed.
What is the syntax for constructing an object? What does Python do when constructing an object?
The syntax for constructing an object is ClassName(arguments) The arguments of the constructor match the parameters in the __init__ method without self. The constructor first creates an object in the memory and then invokes the initializer.
What are the benefits of data hiding? How is it done in Python?
Two benefits: (1) for protecting data and (2) for easy to maintain the class.In Python, private data fields are defined with two leading underscores.
How do you create an object?
Use a constructor.
Variable: references to an object. UML: stands for Unified Modeling Language, which uses graphical notations todescribe classes and objects. Dot Operator: is the . symbol for accessing data fields and invoking methods. Instance Method: must be invoked from an object. Instance Variable: is a data field in an object.
Variable: references to an object. UML: stands for Unified Modeling Language, which uses graphical notations todescribe classes and objects. Dot Operator: is the . symbol for accessing data fields and invoking methods. Instance Method: must be invoked from an object. Instance Variable: is a data field in an object.
How do you create a datetime for the current time? How do you obtain the current year, month, and day from a datetime object d? How do you obtain the timestamp from a datetime object d?
You can construct a datetime object with specified year, month, day, hour, minute, second, and microsecond. The hour, minute, second, and microsecond can be omitted with default value 0. You can also create a datetime object for the current time now() method or the fromtimestamp(timestamp) method. You can obtain the current year, month, and day using d.year, d.month, and d.day from a datetime object d. You can obtain a timestamp from a datatime object d using d.timestamp().
What problem arises in running the following program? How do you fix it? class A: def __init__(self, i): self.i = i def main(): a = A() print(a.i) main() # Call the main function
You need to pass an argument in the constructor A() to invoke the class A's initializer.
What problem arises in running the following program? How do you fix it? class A: def __init__(self, i): self.__i = i def main(): a = A(5) print(a.__i) main() # Call the main function
__i is a private data field and cannot be accessed from outside of the class.
Is the following code correct? If not, fix the error. class A: def __init__(self, on): self.__on = not on def main(): a = A(False) print(a.on) main() # Call the main function
__on is a private data field and cannot be accessed outside the class. The way to fix it is to add a getter method for the Boolean property as follows: class A: def __init__(self, on): self.__on = not on def isOn(self): return self.__on def main(): a = A(False) print(a.isOn()) main() # Call the main function
_______ is a template, blueprint, or contract that defines objects of the same type.
a class
________ is used to create an object.
a constructor
self
a parameter that references the object itself
instance variable
also called fields
__________ represents an entity in the real world that can be distinctly identified.
an object
Write an expression that returns the area of a Circle object c by invoking its getArea() method.
c.getArea()
What is wrong with the following programs? (a) 1 class A: 2 # Construct an object of the class 3 def A(self): 4 radius = 3 (b) 1 class A: 2 # Construct an object of the class 3 def __init__(self): 4 radius = 3 5 6 def setRadius(radius): 7 self.radius = radius
(a) The constructor should be defined as __init__(self). (b) radius = 3 should be self.radius = 3
constructor does two things:
- it creates an object in the memory for the class. - It invokes the class's __init__ method to initialize the object.
What is the output of the following code? class A: # Construct a circle object def __init__(self, k = 1): self.k = k def increment(self): self.k += 1 a = A() a.increment() print(a.k)
2
Show the output of the following code: (a) c1 = Circle(2) c2 = Circle(3) c1 = c2 c2.radius = 4 print(c1.radius, c2.radius) (b) c1 = Circle(2) c2 = Circle(3) c1.radius = c2.radius c2.radius = 4 print(c1.radius, c2.radius)
4 4 3 4
For the Circle class in Listing 9.1, what is the output of the following code? c1 = Circle(6) c2 = Circle(7) c1 = c2 c2.setRadius(8) print(c1.radius, c2.radius)
8 8
An object is an instance of a __________.
class
The keyword __________ is required to define a class.
class
Show the output of the following program: class Count: def __init__(self, count = 0): self.count = count def main(): c = Count() n = 1 m(c, n) print("count is", c.count) print("n is", n) def m(c, n): c = Count(5) n = 3 main() # Call the main function
count is 0 n is 1
Show the output of the following program: class Count: def __init__(self, count = 0): self.count = count def main(): c = Count() times = 0 for i in range(100): increment(c, times) print("count is", c.count) print("times is", times) def increment(c, times): c.count += 1 times += 1 main() # Call the main function
count is 100 times is 0
Unified Modeling Language (UML)
A graphical notation for describing classes and their relationships.
mutator (setter)
A method that changes the value of a private field in an object.
identifier
A name of a variable, function, class.
constructor
A special method for initializing objects when creating objects. The constructor is defined using __init__ in the class.
How do you define a private method?
Add two underscores as the prefix for the method name
anonymous object
An anonymous object is created without a name. The object is used once in the program.
object-oriented programming (OOP)
An approach to programming that involves organizing objects and their behavior into classes of reusable components.
class
An encapsulated collection of data and methods that operate on data. A class may be instantiated to create an object that is an instance of the class. - it's also a contract
dot operator (.)
An operator used to access members of an object. If the member is static, it can be accessed through the class name using the dot operator.
private data field
Cannot be accessed from the outside of the class.
private method
Cannot be invoked from the outside of the class.
__radius__
not a private data field
__radius
private data field
behavior
same as action
state
same as property or attributes
The constructor creates an object in the memory and invokes __________.
the __init__ method
accessor (getter)
the method for retrieving a private field in an object.
Write a statement that invokes the turnOff() method on TV object named tv.
tv.turnOff()
UML class diagrams
use graphical notation to describe classes.
Write a statement to create a Circle object with its default radius and assign the object to a reference variable x.
x = Circle()
Write a statement to create a Circle object with radius 5 and assign it to a reference variable named x.
x = Circle(5)
Given the declaration x = Circle(), which of the following statement is most accurate.
x contains a reference to a Circle object.
Is the following code correct? If so, what is the printout? 1 def main(): 2 a = A() 3 a.print() 4 5 class A: 6 def __init__(self, newS = "Welcome"): 7 self.__s = newS 8 9 def print(self): 10 print(self.__s) 11 12 main() # Call the main function
Correct. The printout is Welcome.
How do you define a class?
Define the initializer, create data fields, and define methods.
Getter: is a method for returning a property value. Setter: is a method for changing a property value. Immutable Object: is a object whose data fields cannot be changed. Data Field Encapsulation: is to declare the data fields using the private modifier.
Getter: is a method for returning a property value. Setter: is a method for changing a property value. Immutable Object: is a object whose data fields cannot be changed. Data Field Encapsulation: is to declare the data fields using the private modifier.
What are the differences between an initializer and a method?
Initializer is a special method that is called when creating an object.
Object: is an instance of a class. Class: is a template, blueprint, or contract that defines what an object's data fields and methods will be. State: also known as property or attribute, are represented by data fields with their current values in an object. Behavior: also known as actions, is defined by methods. Instantiation: is to create an instance from a class. Constructor: is a special kind of method that is invoked to create an object.
Object: is an instance of a class. Class: is a template, blueprint, or contract that defines what an object's data fields and methods will be. State: also known as property or attribute, are represented by data fields with their current values in an object. Behavior: also known as actions, is defined by methods. Instantiation: is to create an instance from a class. Constructor: is a special kind of method that is invoked to create an object.
