Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
IE (3799-017)
(4) Nov. 25, 2008
Lecturer
Ying CHEN
ying@sys.t.u-tokyo.ac.jp
Department of Systems Innovation
Teaching Assistant
Jeffrey TAN
jeffrey@robot.t.u-tokyo.ac.jp
Department of Precision Engineering
1 CP-IE (4), Nov. 25, 2008
1
Content of Java Programming
2. Language Basics
3. Control Flow Statement
4. Basic of Object-Oriented Programming
(4) 11/25
…
2
4. Basic of Object-Oriented Programming (OOP)
Basic concepts
Procedural-Oriented programming:
algorithm + data structure → code
(functions)
Object-Oriented programming:
data structure + algorithm → code
(methods)
3
4. Basic of Object-Oriented Programming (OOP)
Basic Concept
Instance
Class
Real
describes theWorld
Software Object
Object
generality of objects
State Behavior
↓ ↓
Variables Methods
4
4. Basic of Object-Oriented Programming (OOP)
Bird Class
State → Color, Weight, Size ... → Variables
Behaviors → Fly( ), Walk( ), Eat( ) ... → Methods
Object
Bird A Bird B
Color = gray Color = yellow
Weight = 89g Weight = 61g
Fly( ) Eat( )
5
4. Basic of Object-Oriented Programming (OOP)
Basic Concept
Message
6
4. Basic of Object-Oriented Programming (OOP)
Basic Concept
Characteristics of
Object-Oriented programming
• Encapsulation
• Inheritance
• Polymophism
7
4. Basic of Object-Oriented Programming (OOP)
8
4. Basic of OOP Method
9
4. Basic of OOP Method
Definition of a Method
methodDeclaration {
local variables
method body
}
public,
…
Declaration of a Method
accessLevel modifier returnType methodName
(type name [type name[…] ] ) { double,
…… static, int,
} … void,
…
10 CP-IE (4), Nov. 25, 2008
10
4. Basic of OOP Method
eg. 1
int summation(int a, int b ) {
return(a+b);
}
eg. 2
public static void main (String arg[ ] ) {
System.out.println(“My first java!");
}
11
4. Basic of OOP Class
Structure of a Class
ClassDeclaration
member variables
Constructor
method1Declaration
local variables
method body
method2Declaration
local variables
method body
…
12
4. Basic of OOP Class
Declaration of a Class
13
Practice 5-1 Creating a class
/* Prac5-1, bird class, 2008. 11. 25. */
class Bird {
String color ;
int weight;
boolean hungry ;
Bird.java
14
Practice 5-1 Creating a class
15
4. Basic of OOP Object
A typical Java program creates many objects, which interact with one another
by sending each other messages. Through these object interactions, a Java
program can implement a GUI, run an animation, or send and receive
information over a network. Once an object has completed the work for which
it was created, it is garbage-collected and its resources are recycled for use by
other objects.
This section uses this example to describe the life cycle of an object within a
program. From this, you can learn how to write code that creates and uses an
object and how the system cleans it up.
16
4. Basic of OOP Object
17
4. Basic of OOP Object
Creating Objects from a class
•Initialization:
- objectName.variableName;
- objectName.methodName( );
objectName.methodName(arg);
- constructor
18
4. Basic of OOP Object
Using Objects
19
4. Basic of OOP Object
• Finalization
Some object-oriented languages require that you keep track of all the objects
you create and that you explicitly destroy them when they are no longer needed.
Managing memory explicitly is tedious and error prone. The Java platform
allows you to create as many objects as you want (limited, of course, by what
your system can handle), and you don't have to worry about destroying them.
The Java runtime environment deletes objects when it determines that they are
no longer being used. This process is called garbage collection.
(An object is eligible for garbage collection when there are no more references
to that object. References that are held in a variable are usually dropped when
the variable goes out of scope. Or, you can explicitly drop an object reference
by setting the variable to the special value null. Remember that a program can
have multiple references to the same object; all references to an object must be
dropped before the object is eligible for garbage collection. )
The Java runtime environment has a garbage collector that periodically frees
the memory used by objects that are no longer referenced. The garbage
collector does its job automatically. in rare case, you might want to run the
finalize to release resource soon after a section of code that creates a large
amount of garbage or before a section of code that needs a lot of memory.
(finalize: before being collected, object has chance to clean itself by finalize)
20
Practice 5-2 Creating objects
/* Prac5-2, object/instance, 2008. 11. 25. */
public class Pract52 {
public static void main (String [ ] args ) {
Bird A = new Bird( );
pract52.java
21
Practice 5-2 (continue)
• Keep “Pract52.java” in a new directory:
To compile the “pract52.java
→ To see what happens?
tying@as301> javac pract52.java
Birdsample.java:3: cannot resolve symbol
symbol : class Bird
location: class Birdsample
Bird A; ….
22
Practice 5-3 Combining 2 classes
23