Download presentation source

Download Report

Transcript Download presentation source

CS100A, Fall 1998, 8 September
Concepts for this lecture:
• Class
• Instance of a class, (an object)
• Fields (instance variables)
• Declaration of class variables
• Operator new
• Class methods
Reading in Holmes
6.1-6.4 (191-197)
6.7 (202-204)
CS100A, 8 Sept. 1998.
Lecture 3
1
Variable: a variable is a named
box into which one can place a
value of some type.
int
x
-49
We want boxes (variables) that
can contain more than one value,
e.g.
x 25
Coordinate c
y 20
CS100A, 8 Sept. 1998.
Lecture 3
2
Coordinate c1
Coordinate c5
x 25
y 25
x 6
y 21
Coordinate is a class. A class is
like a type, except that YOU
can declare it.
c1 and c5 are instances of the
class; they are objects.
c1.x and c1.y are fields of object
c1.
CS100A, 8 Sept. 1998.
Lecture 3
3
Declaration in Java of class Coordinate:
public class Coordinate {
public int x;
public int y;
}
• keywords public and class
• name of the class. Convention:
capitalize all words in a class
names. E.g. StringBuffer,
• body of the class, delimited by { }
– field declarations. Just normal
type and class declarations
– methods and constructors
(discussed later)
CS100A, 8 Sept. 1998.
Lecture 3
4
Type declaration and class declarations
int x;
0
// x
Coordinate w;
// w
null
null is a Java constant; it denotes the
absence of an object.
CS100A, 8 Sept. 1998.
Lecture 3
5
Creating a new instance of a class: use
new:
Coordinate w;
// w
null
w= new Coordinate();
x ?
y ?
// w
CS100A, 8 Sept. 1998.
Lecture 3
6
Referencing a field of an instance.
x 3
y 5
// w
// x
6
x= w.x;
// w
// x
x 3
y 5
3
CS100A, 8 Sept. 1998.
Lecture 3
7
Assigning to a field of an instance.
(Later, we’ll investigate good
programming practices for referring to
and assigning to fields of an instance.
x 3
y 5
// w
w.x= 7;
x 7
y 5
// w
CS100A, 8 Sept. 1998.
Lecture 3
8
Assigning one instance to another:
creates alias: two names refer to the
same object!
// w
x 3
y 5
// h
x 7
y 1
h= w;
// w
x 3
y 5
//
x 7
y 1
h
CS100A, 8 Sept. 1998.
Lecture 3
9
Classes can have methods that operate on
the fields of the class
public class Coordinate {
public int x;
public int y;
// Set field x to p*p
public void setX(int p) {
x= p*p;
}
// Set field y to q
public void setY(int q) {
y= q;
}
// Return the sum of the squares of the fields
public int sumSquares() {
return x*x + y*y;
}
}
CS100A, 8 Sept. 1998.
Lecture 3
10
Execution of statement
return <expression>
terminates execution of the method (function) in
which it appears and “returns” the value of
<expression> to the place of call.
Example of calls:
c.setX(3);
c.setY(2);
x
x 9
y 2
//Store 85 in s
s= c.sumSquares();
CS100A, 8 Sept. 1998.
Lecture 3
11
We discussed several concepts concerning a classes:
• Definition of a class, including fields and methods,
• Declaration of a class variable,
• Creation of a new instance of a class and storing it in
a class variable.
• Referencing and assigning to a field of a class.
• Assigning a class instance to a class variable, thus
creating an alias.
• Class methods (procedures and functions)
We have looked only at the mechanics; we haven’t
spend much time on using these things. That will
come later. For now, it’s imperative that you learn
the mechanics.
The demonstrations using CodeWarrior on the Mac
should help provide some perspective.
CS100A, 8 Sept. 1998.
Lecture 3
12