methods methods are how we implement actions – actions that objects can do, or actions that can be...

21
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move, turn, moveTowards, and rotate.

Upload: silas-tyler

Post on 19-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Methods

Methods are how we implement actions – actions

that objects can do, or actions that can be done to

objects. In Alice, we have methods such as move,

turn, moveTowards, and rotate.

Page 2: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Method Syntax

In Java, a method consists of a method header and a

method body. The header says who can call the

method, the type of the value that is returned by the

method, the name of the method, and the

parameters that are passed into the method.

The body gives the code for the method – that is

what is actually done.

Page 3: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Example

public Rational add(Rational b) { Rational c = new Rational(); c.n = n * b.d + b.n * d; c.d = d * b.d; return c;}

Page 4: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Syntax

<accessor> <return type> <name> (<parameter list>)

{ <body> }

Page 5: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Parameters

Parameters are how we pass information into a

method. The method header contains a parameter

list – a comma separated list of parameters. Each

parameter consists of a type and a variable.

Parameters are similar to variable declarations,

except that they are initialized to values given in

the call to the method.

Page 6: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Calling a method

Most methods are associated with an object

(although some methods may be associated with

the entire class). A method is called by apply the

method to an object of the class. The call may

occur in an expression. The syntax is:

<object>.<method>(<argument list>). For example:

Rational rat3 = rat1.add(rat2);

Page 7: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

ArgumentsIn the call to the method, values are specified for the

initial values of the parameters. In the previous

example, the parameter b would be initialized with

the value rat2.

The list of arguments must match in number and type

with the list of parameters. Sometimes, one type

may be automatically converted to another. For

example, if the parameter is of type double, and the

argument is an int, the argument will be converted.

Page 8: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

More on Methods

Methods are like miniature programs. They have

input (through the parameters), output (via the

return statement) and processing steps (the method

body). Methods may have their own local

variables.

Page 9: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Return Value

A method which returns a value does so by using the

return statement. The syntax is just:

return <value>

The value must be of the same type as specified in

the method header.

Page 10: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Void Methods

A method which does not return a value should be of

return type void. You can return from the method

before reaching the end of it, by using a return

statement without a value, e.g.

return;

The call to a void method may not be in an

expression, but should be a separate statement.

Page 11: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Constructors

Constructors are similar to methods, but instead of

returning a value, they return a newly created

object. Basically, constructors are used to initialize

the fields of the new object, often given

information from the caller, or using default value.

Page 12: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Syntax

Constructors look very similar to methods, but there

are two differences: Constructors do not return a value, but rather the

newly created object. Thus constructors do not

have a return type (not even void) Constructors always have the same name as the

class.

Page 13: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Example

A constructor for the Rational class could be:

public Rational(int n0, int d0) { n = n0; d = d0;}

Constructors are almost always public.

Page 14: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Default Constructor

If you do not declare any constructors, there is

always one created by default. This is a zero-

argument constructor. It takes no arguments and

just creates the object. If fields are initialized, those

values are used. If not, numbers are set to 0, and

chars to null characters.

Page 15: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

More on Constructors

If you do declare any constructors and you use the

zero-argument constructor, you must declare it

yourself:

public Rational() {}

Page 16: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Multiple Constructors

Often classes contain more than one constructor.

This is useful if the user may omit values and

default values are to be used. For example, the

Rational class may have a one-place constructor

which is given the numerator, and the denominator

is assumed to be 1. This converts ints to Rationals.public Rational(int n0){ n = n0; d = 1; }

Page 17: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Multiple Constructors (cont'd)

Similarly, the zero-argument constructor should

probably set the values to something reasonable,

such as 0/1:

public Rational() { n = 0; d = 1; }

Page 18: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Disambiguation

So as not to confuse the compiler, constructors (like

methods) must be distinguishable when called.

That means that they must differ in the number or

types of the arguments.

Page 19: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Nested Constructors

One constructor may invoke another constructor. In

Java convention, this is commonly done to set

default arguments. For example, the one-place

constructor would invoke the two-place

constructor, by passing the value of n0, and

passing 1 for d:public Rational(int n0) { this(n0,1); }

The keyword this is used to invoke another

constructor of the class.

Page 20: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Nested Constructors (cont'd)

Similarly, the zero-argument constructor would

invoke the one-argument constructor (which in

turns invokes the two-place constructor).

public Rational() { this(0); }

This makes more sense when the code for the

constructor is complicated, and we want to avoid

duplication of code in all the constructors. For

example, if we need to change it, we need only

make the change once.

Page 21: Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,

Example

In our example of the Rational constructors, we

forgot that Rational numbers are to be stored in

lowest terms. The constructor should reduce the

rational number.public Rational(int n0; int d0) { n = n0; d = d0; reduce();}

This one change effects all the constructors as they

call this one (directly or indirectly).