Bài giảng Phát triển phần mềm phân tán - Bài 5: Object initialization and usage - Vũ Thị Hương Giang
LESSON V.
Object Initialization and Usage
Vu Thi Huong Giang
SoICT (School of Information and
Communication Technology)
HUST (Hanoi University of Science
and Technology)
Objectives
• Acquaint how to initialize and use objects
Content
• Data initialization and constructor
• Object declaration and initialization
• Object usage
I. Data initialization
• Needs of initialize data before using
• Primitive data type: initialize by assignment
operator
• Object (reference data type): initialize by using
constructor.
Constructor
• Objects are created through constructors
– instance variables of an object are initialized in the
scope of constructors.
• A class may declare its own constructor or rely on
the default constructor provided by the Java
environment.
– The name of constructor is the name of class
– Constructor is written without return type; the default
return type of a class constructor is the same class
5
Constructor’s definition:
without parameter
•
•
Fixing the initial values of attributes
Syntax:
•
Example
public class Account {
private String owner;
private long balance;
public Account() {
owner = “Noname”;
balance = 100;
}
modifier class-name() {
// constructor body
}
•
Default constructor:
– constructor without argument.
– It is automatically provided in the
}
case of no explicit declaration
– Initialize attributes with the default
values of the corresponding data
types
•
If this constructor is not
implemented, the java default
constructor will initiate attributes
with the following values:
– owner = null (Default value of String
type)
– balance = 0 (Default value of long
type)
Constructor’s definition:
with parameter
• Parameterizing the initial values of attributes
modifier class-name(parameter-list) {
// constructor body
}
• Example
public class Account {
private String owner;
private long balance;
public Account(String name, long money) {
owner = name;
balance = money;
}
}
II. Object declaration and initialization
datatype instance-variable;
instance-variable = new datatype();
or
datatype instance-variable = new datatype();
• Declaration: declare a reference variable
– Associate a variable name with a datatype object
• Instantiation: new is a Java operator that creates the
object (i.e. creates an instance of class datatype)
– Allocate memory for a datatype object
– Return its address
• Initialization: initialize the new object
– Call to a constructor
8
Object initialization
• Syntax
datatype instance-variable = new datatype(…);
• When the object is created, the member variable
is assigned to the memory area, and initialized at
the same time.
• Implicit initialization:
– number data type 0;
– reference type null
– boolean false
(see the default values of data types, lecture 2)
III. Object usage
• Using an object implies
– Accessing (taking or changing) the value of one of its
variables
– Calling one of its methods to perform an action.
• Objects communicates through message passing
receiver.message
– The dot operator (“.”) is used to send a message to
receiver object
10
1. Accessing and changing the value of
a variable
receiver.member-variable-name;
– The message is the variable name
– No parameter is used
– No need to use the dot operator inside a class
public class Account {
// Class that uses
String name; //Account name // the variable balance
long balance; //Balance
Account obj = new Account();
obj.balance = 100;
void display(){
System.out.println(…);
}
void deposit (long money){
owner: Vu Thi Huong Giang
balance: 2.000.000 VND
balance += money;
}
interest 0.05f 0.04f 0.03f
}
obj
Account object
(Caller)
2. Calling methods
• A method is called through an object
– The object is its default target
receiver.method-name(list-of-parameters)
– The message is the method name.
– The parameters respect the signature of method.
public class Account {
// Class that uses
String name; //Account name // methods of Account object
long balance; //Balance
Account obj = new Account();
obj.display();
void display(){
obj.deposit(1000);
System.out.println(…);
}
void deposit (long money){
balance += money;
}
owner: Vu Thi Huong Giang
balance: 2.000.000 VND
interest 0.05f 0.04f 0.03f
}
obj
(Caller)
Account object
3. Calling constructors
• A constructor can not be called directly
• It can only be called by using the new operator
during class instantiation.
public class Account {
// Account name
public class AccountUsage {
public static void main(String[] args) {
private String owner;
// Account name
// Object creation
Account account1 =
private long balance;
new Account("Giang", 2000);
}
}
public Account
(String name, long money ) {
owner = name;
balance = money;
owner: Giang
}
balance: 2.000
}
account1
(Caller)
Account object
Example
public class Track {
public Track(int length, int format, int size) {
title = "Notitle";
// attributes
private String title;
time_length = length;
data_format = format;
buffer_size = size;
// title of the track
private int time_length;
// length of time for playback
private int data_format;
// encoding format
}
public Track(String name, int length, int format,
int size) {
title = name;
private int buffer_size;
// size of the buffer where data is
// read from for playback
time_length = length;
data_format = format;
buffer_size = size;
}
// constructor without parameter
public void setLength(int length) {
public Track() {
time_length = length;
title = "Notitle";
}
}
public void setTitle(String name) {
title = name;
}
// ....
}
14
Example
public class TrackClassUsage {
• Objects are used usually
public static void main(String[] args)
{
through variables
Track track1 = new Track();
track1.setTitle(“One name");
track1.setLength(45);
Track track2 = new Track(
"Four seasons", 43, 1, 1000);
Track track3 = new Track(
“One name", 45, 0, 0);
Track track4 = track3;
track2 = new Track(
• Track1 and track3 have
the same initial values,
but they are not the
same object
• Track3 and track4 refer
to the same object
• The object ("Four
seasons", 43, 1, 1000)
is no longer accessible
“Another name", 0, 0, 1000);
}
}
4. Keyword this
• Used inside a method or a constructor to refer to
the current object
– Specify member or method of current object
– Distinguish the data member with the parameters of
member functions (which have the same name)
public class Account {
// instance variable
private String owner; // Account name
private long balance; // Balance
//...
// value setting method
public void setAccountInfo(String owner, long balance) {
this.owner = owner;
this.balance = balance;
}
//...
}
16
Keyword this
• Used to call another constructor of own class
public class Track {
private String title; // title of the track
private int time_length; // the length of time to playback
private int data_format;
// the encoding format in which the data (audio, video, text...) is represented
private int buffer_size;
// the total size of the buffer where data is read from for playback
public Track() {
‘this’(argument list) calls
another constructor
this(0,0,0);
h, int format, int size) {
;
gth;
data_format = format;
buffer_size = size;
}
}
17
Keyword this
• Used to pass the current object’s reference to
other objects
public class Track {
private String title; // title of the track
private int time_length; // the length of time to playback
private int data_format;
// the encoding format in which the data (audio, video, text...) is represented
private int buffer_size;
// the total size of the buffer where data is read from for playback
public Track() {
this(0,0,0);
}
public Track(int length, int format, int size) {
title = "Notitle";
time_length = length;
data_format = format;
buffer_size = size;
RecordException re = new RecordException(this);
}
}
18
5. Argument-passing
• Parameter: a variable receiving value at the time
the method is called
• Argument: a value passed to the method when it
is called
• Two ways of how the arguments are passed to
methods:
– Parameters of primitive type: by value
• a method receives a copie of the original value;
– Parameters of reference type: by reference
• a method receives the memory address of the original
value, not the value itself
Passing arguments by value
public class PassingByValueUsage {
arg1
10
static int integer;
static float floating_point;
integer
char character;
boolean logic;
10
public static void
arg2
TestPrimitiveParam(int arg1,
float arg2, char arg3,
0.06
0.06
boolean arg4) {
//passing by value
//new values can be seen
//outside the enclosed method
integer = arg1;
floating_point
arg1
10
floating_point = arg2;
// legal assignment
// new values can not be seen
// outside the enclosed method
arg1 += arg1;
integer
10+1
arg4
true
arg4 = false;
}
false
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển phần mềm phân tán - Bài 5: Object initialization and usage - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_phat_trien_phan_mem_phan_tan_bai_5_object_initiali.pdf

