Download presentation source

Download Report

Transcript Download presentation source

CS100A, Fall 1997, 9 September
Concepts for this lecture:
• Class
• Instance of a class, (an object)
• Fields (instance variables),
methods, and constructors
• Declaration of class variables
• operator new
Reading in Lewis/Loftus
4.1 (121-124)
4.5 (140-143)
4.6 (143-146)
CS100A, Fall 1997.
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, Fall 1997.
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, Fall 1997.
Lecture 3
3
Declaration in Java of class Coordinate: The
constructors are used (later) to create instance
of class Coordinate.
public class Coordinate
{public int x;
public int y;
// Constructor: an instance with x=0, y=0
public Coordinate()
{x= 0; y= 0;}
// Constructor: an instance with x= xp, y=yp
public Coordinate(int xp, int yp)
{x= xp; y= yp}
}
CS100A, Fall 1997.
Lecture 3
4
Note syntax of the class:
• 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
(discussed later)
– constructor declarations.
• public
• <name of class>
• sequence of statements,
delimited by { }
– (later, method declarations)
CS100A, Fall 1997.
Lecture 3
5
Unless you know the syntax of classes
perfectly, often, the best way to write your
own class declaration is to copy another one
and modify the copy.
Constructor: used to create an instance of the
class and assign initial values to the fields of
the instance.
Therefore, in general, it will assign to all
fields.
The comment for a constructor should describe
what its parameters are for. See the second
constructor in class Coordinate, given
earlier.
CS100A, Fall 1997.
Lecture 3
6
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, Fall 1997.
Lecture 3
7
Creating a new instance of a class: use a
call on a constructor of the class.
Coordinate w;
// w
null
w= new Coordinate();
x 0
y 0
// w
w= new Coordinate(3,5);
x 3
y 5
// w
CS100A, Fall 1997.
Lecture 3
8
Referencing a field of an instance.
x 3
y 5
// w
// x
6
x= w.x;
// w
// x
x 3
y 5
6
CS100A, Fall 1997.
Lecture 3
9
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, Fall 1997.
Lecture 3
10
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, Fall 1997.
Lecture 3
11
We discussed several concepts concerning a classes:
• Definition of a class, including fields and
constructors,
• 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.
We have looked only at the mechanics; we haven’t
spend much time on using these things. That will
come. For now, it’s imperative that you learn the
mechanics.
And, the demonstrations using CodeWarrior on the
Mac should help provide some perspective.
CS100A, Fall 1997.
Lecture 3
12