• Tidak ada hasil yang ditemukan

In the real world ( § 14.1.1 )

N/A
N/A
Protected

Academic year: 2023

Membagikan "In the real world ( § 14.1.1 )"

Copied!
11
0
0

Teks penuh

(1)

CompSci 101 - Lecture 15

Required Coursebook Reading

Chapter 14 – Classes

(sections 14.1, 14.2 and 14.3)

Classes ( § 14.1 )

Our programs consist of collections of things called objects which perform useful tasks. There are two fundamental aspects to an object:

state – the data or information that the object stores

behaviour – the operations that the object can perform

In the real world ( § 14.1.1 )

Your car is just one of many cars in the world. Using object-oriented terminology, we would say that your car is an instance of the class of objects known as cars. Cars have:

state: colour, current speed, current gear, etc.

behaviour: accelerate, brake, change gear, etc.

Although all cars share the same behaviour, each car’s state is independent of another car’s state:

colour: red speed: 50

colour: white speed: 5

Car A Car B

(2)

However, both cars can accelerate and brake and change gear, etc.

For example, if car B accelerates, its state will be modified – its current speed may become, say, 10 km/h.

In a program ( § 14.1.2 )

Our programs can also consist of many different objects. Two

objects of the same kind are capable of the same kinds of behaviour, but have independent state information.

For an object in our program:

• the state information is defined by what are called instance variables.

• the possible behaviour is defined by what are called instance methods.

The definition of a particular kind of object (which must specify the state information and the behaviours for objects of that kind) is called a class. Once created, an object is also called an instance of a class.

accelerate

colour: white speed: 10 gear: 1st

Car B

(3)

Defining a Class ( § 14.1.3 )

The general structure of a class definition in Java is as follows:

public class NameOfClass { constant1;

constant2; ...

instanceVariable1;

instanceVariable2; ...

constructorMethod1() { ...

}

constructorMethod2() { ...

}

instanceMethod1() { ...

}

instanceMethod2() { ...

} ...

}

(4)

• This class definition must be stored in a file called

NameOfClass.java, and compiled in the usual way to generate the file NameOfClass.class.

• By convention, the name of the class always starts with a capital letter.

Visibility modifiers ( § 14.1.4 )

The fields (constants and instance variables) and the methods can be either public or private.

public: the field or method can be accessed from any class

private: the field or method can only be accessed inside the class in which it is defined

For classes that we define in our programs, we will follow these rules:

• our constants will (almost) always be public (and static)

• our instance variables will always be private

• our constructor methods will always be public

• our instance methods will (almost) always be public

Fields ( § 14.2 )

Constants ( § 14.2.1 )

• If the class needs to store some constant information which will not change:

public static final type identifier = expression;

(5)

Instance variables ( § 14.2.2 )

• Instance variables are used to define the state, or information, that each instance of the class (ie. each object) will store.

• Defined under the class header, outside of any method.

• The scope of an instance variable is the whole class and it exists for as long as the instance is around.

• All instance variables will have the modifier private.

For example, if we were defining a class to represent circle objects, it may begin:

public class Circle { private int radius;

private int xCentre;

private int yCentre;

...

}

• This class would be stored in a file called Circle.java, and compiled to a file called Circle.class.

• From this class definition, we could create many objects of type Circle.

For example, we may create 3 circle objects:

radius 12 xCentre 50 yCentre 10

radius 7 xCentre 3 yCentre -6

radius 80 xCentre 99 yCentre 200

(6)

Default values for instance variables ( § 14.2.3 )

The primitive types:

int default value 0

double default value 0.0

boolean default value false.

Instance variables of any object type:

• have the default value null. This means that the variable does not point to an object yet.

The keyword null

Any reference variable has the value null before an object is assigned to it. For example, when we declare a variable of some reference type:

Circle c;

we can visualise this as:

where c has the value null. When we assign an object to c:

c = new Circle(8,9,10);

we visualise this as:

c

(7)

We can also assign the value null to any reference variable. For example, if we assign:

c = null;

then c will no longer point to the Circle object, but will again have the value null:

Beware null pointers

You must be careful to never call a method (using dot notation) on a reference variable which has the value null. If you do, a:

java.lang.NullPointerException

will occur, which will cause your program to stop working.

Creating objects

How do we create objects from a class definition?

1) use the keyword new and

2) call the constructor method in the class definition

c

radius 8 xCentre 9 yCentre c

10

(8)

Constructor methods ( § 14.3 )

A constructor is a special method which is called when we want to create a new instance of a class::

String s1 = new String( hello );

When we are designing our own classes, the general form of a constructor method is as follows:

public NameOfClass(parameters) { ...

}

A constructor method has one purpose:

to initialise the instance variables for the new object being created

For example, returning to the example of our Circle class, we could define the following constructor:

public class Circle { private int radius;

private int xCentre;

private int yCentre;

public Circle() { radius = 10;

xCentre = 100;

yCentre = 100;

}

(9)

Now that our class definition contains a constructor method, in our program we can declare variables of type Circle:

Circle c1, c2;

And we can create objects by calling the constructor method:

c1 = new Circle();

c2 = new Circle();

We would visualise this as follows:

What if we want the instance variables of the objects we create to initially have different values?

We could define a constructor which is passed parameters, the values of which can be assigned to the instance variables, for example:

public Circle(int initRadius, int initX, int initY) { radius = initRadius;

xCentre = initX;

yCentre = initY;

}

radius 10 xCentre 100 yCentre c1

100

radius 10 xCentre 100 yCentre c2

100

(10)

With this new constructor method, we could create objects as follows:

c1 = new Circle(20, 10, 5);

c2 = new Circle(50, 666, 999);

In fact, our classes can have more than one constructor defined.

radius 20 xCentre 10 yCentre c1

5

radius 50 xCentre 666 yCentre c2

999

(11)

So our Circle class could look like:

public class Circle { private int radius;

private int xCentre;

private int yCentre;

public Circle() { radius = 10;

xCentre = 100;

yCentre = 100;

}

public Circle(int initRadius,

int initX, int initY) { radius = initRadius;

xCentre = initX;

yCentre = initY;

} ...

}

For example:

c1 = new Circle(8, 9, 10);

c2 = new Circle();

Here is a useful hint for when you are defining a constructor:

radius 8 xCentre 9 yCentre c1

10

radius 10 xCentre 100 yCentre c2

100

Referensi

Dokumen terkait

The role of tourism in facing the digital era can be seen in marketing activities that can be easily accessed through internet media such as websites, social media,