|
1
|
- Chapter 2 introduces Object Oriented Programming.
- OOP is a relatively new approach to programming which supports the
creation of new data types and operations to manipulate those types.
- This presentation introduces OOP.
|
|
2
|
- There is no real answer to the question, but we’ll call it a “thinking
cap”.
- The plan is to describe a thinking cap by telling you what actions can
be done to it.
|
|
3
|
- You may put a piece of paper in each of the two slots (green and red),
with a sentence written on each.
- You may push the green button and the thinking cap will speak the
sentence from the green slot’s paper.
- And same for the red button.
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
- We can implement the thinking cap using a data type called a class.
|
|
8
|
- The class will have two components called greenWords and redWords. These compnents are strings which hold
the information that is placed in the two slots.
- Using a class permits two features . . .
|
|
9
|
- The two components will be private instance variables. This ensures that nobody can directly
access this information. The only
access is through methods that we provide for the class.
|
|
10
|
- In a class, the methods which manipulate the class are also listed.
|
|
11
|
|
|
12
|
|
|
13
|
- A program that wants to use the thinking cap can import the ThinkingCap class.
|
|
14
|
- Just for fun, the example program will declare two ThinkingCap variables
named student and fan.
|
|
15
|
- The variables are examples of reference variables, which means that they
have the capability of refering to ThinkingCap objects that we create
with the new operator.
|
|
16
|
- Once the ThinkingCaps are created, we can activate methods such as slot
for the student thinking cap.
|
|
17
|
- Once the ThinkingCaps are created, we can activate methods such as slot
for the student thinking cap.
|
|
18
|
- The method activation consists of four parts, starting with the variable
name.
|
|
19
|
- The variable name is followed by a period.
|
|
20
|
- After the period is the name of the method that you are activating.
|
|
21
|
- Finally, the arguments for the method.
In this example the first argument (newGreen) is
"Hello" and the second argument (newRed) is "Bye".
|
|
22
|
- How would you activate student's
pushGreen method ?
- What would be the output of student's pushGreen method at this point in
the program ?
|
|
23
|
- Notice that the pushGreen method has no arguments.
- At this point, activating student.pushGreen will print the string
- Hello.
|
|
24
|
- Trace through this program, and tell me the complete output.
|
|
25
|
|
|
26
|
- Class = Data + Methods.
- You know how to write a new class type, and place the new class in a
package.
- You know how to import the class into a program that uses class type.
- You know how to activate methods.
- But you still need to learn how to write the implementations of a
class’s methods.
|
|
27
|
|
|
28
|
- There is one feature about a method’s implementation . . .
|
|
29
|
- Within the body of the method, the class’s instance variables and other
methods may all be accessed.
|
|
30
|
- Within the body of the method, the class’s instance variables and other
methods may all be accessed.
|
|
31
|
- Within the body of the method, the class’s instance variables and other
methods may all be accessed.
|
|
32
|
- Within the body of the method, the class’s instance variables and other
methods may all be accessed.
|
|
33
|
- Here is the implementation of the pushGreen method, which prints the
green Words:
|
|
34
|
- Here is the implementation of the pushGreen method, which prints the
green Words:
|
|
35
|
- Often, one or more methods will place data in the instance variables...
|
|
36
|
- Classes have instance variables and methods. An object is a variable
where the data type is a class.
- You should know how to declare a new class type, how to implement its
methods, how to use the class type.
- Frequently, the methods of an class type place information in the
instance variables, or use information that's already in the instance
variables.
- In the future we will see more features of OOP.
|
|
37
|
|