Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Lectures / lecture3_1

.pdf
Скачиваний:
36
Добавлен:
14.10.2016
Размер:
887.32 Кб
Скачать

APPLICATION DEVELOPMENT

SDP-2

Lecture 4.

Brodyagina Mariya

Quiz

Which statement is true?

a.There are eight primitive types built in to the Java programming language.

b.byte, short, char, and long are the four integral primitive data types in the Java programming language.

c.A boolean type variable holds true, false, and nil.

d.long=10; is a valid variable name that adheres to the variable declaration and initialization syntax.

Working with Objects: Introduction

Objects are accessed via references.

Objects are instantiated versions of their class.

Objects consist of attributes and operations:

In Java, these are fields and methods.

Accessing Objects by Using a

Reference

The camera is like the object that is accessed via the reference (remote).

The remote is like the reference used to access the camera (object).

Working with Object Reference Variables

• Declaration:

Classname identifier;

This code fragment

Instantiation:

creates the object.

 

new Classname();

Assignment:

Object reference = new Classname();

The identifier

The

from the

assignment

Declaration

operator

step

 

To assign to a reference, creation and assignment must be in same statement.

Declaring and Initializing: Example

1 Declare a

 

reference for

2

the object.

 

Create the object

 

instance.

Shirt myShirt;

myShirt = new Shirt();

3 Assign the object to the reference variable.

Working with Object References

Declare and initialize reference.

Shirt myShirt = new Shirt();

 

int shirtId = myShirt.shirtId;

Get the value of

the shirtId

 

 

field of the

myShirt.display();

object.

Call the display() method of the object.

Working with Object References

1 Create a Shirt object and get a reference to it.

1

Pick up remote to gain access to camera.

2

Press remote controls to have camera do something.

Shirt myShirt = new Shirt();

myShirt.display();

2

Call a method to have Shirt object do something.

Working with Object References

 

There is

 

only one

remote2

Camera

object.

 

Camera remote1 = new Camera();

Camera remote2 = remote1;

remote1.play();

remote2.stop();

remote1

References to Different Objects

Television

Television remote

Camcorder

Camcorder remote

Соседние файлы в папке Lectures