1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Initialization ppt

13 103 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Initialization
Chuyên ngành Computer Science
Thể loại Bài thuyết trình
Định dạng
Số trang 13
Dung lượng 122,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Instance field default values• Instance fields set to default values when object created – 0 for numeric types – false for bool – '\x0000' for char – null for references set to default v

Trang 1

Initialization

Trang 2

• Present field initialization options for fields

– default values

– variable initializers

– constructors

Trang 3

Instance field default values

• Instance fields set to default values when object created

– 0 for numeric types – false for bool – '\x0000' for char – null for references

set to default values Rational a = new Rational();

instance fields

class Rational {

int numerator;

int denominator;

.

}

numerator 0 denominator 0 a

Trang 4

Variable initializer

• Instance fields can be initialized at point of definition

– called variable initializer

– executed each time an object is created

– convenient way to overwrite default values

initialize

class Rational {

int numerator;

int denominator = 1 ; .

}

Trang 5

• Class can supply constructor to do initialization

– automatically invoked when object created – implemented using same name as class with no return type

constructor

class Rational {

int numerator;

int denominator;

public Rational (int numerator, int denominator) {

this.numerator = numerator;

this.denominator = denominator;

} .

}

Trang 6

Invoking constructor

• Constructor automatically invoked when object created

create object,

invoke constructor Rational a = new Rational(2, 5);

numerator 2 denominator 5 a

Trang 7

Multiple constructors

• Class can supply multiple constructors

– parameter lists must be different

constructor

class Rational {

public Rational (int numerator, int denominator) {

this.numerator = numerator;

this.denominator = denominator;

}

public Rational (int numerator) {

this.numerator = numerator;

this.denominator = 1;

} .

constructor

Trang 8

Default constructor

• Can supply constructor that takes no arguments

– often called the default constructor

no argument

constructor

class Rational {

public Rational () {

this.denominator = 1;

} .

}

Trang 9

Selecting constructor to invoke

• Compiler selects constructor version automatically

– based on arguments passed

Rational b = new Rational(6);

Rational c = new Rational();

one int

no arguments

Trang 10

Constructor initializer

• One constructor can invoke another constructor

– use :this( ) syntax before constructor body

– called constructor initializer

– can put common code in constructor that others call

class Rational {

public Rational (int numerator, int denominator)

{ this.numerator = numerator;

this.denominator = denominator;

}

public Rational(int numerator)

:this(numerator, 1)

{

}

call 2 argument

constructor

Trang 11

Compiler generated constructor

• Compiler creates default constructor

– only if no constructors supplied by programmer

• Compiler generated constructor

– takes no arguments

– has empty body

– calls no argument constructor for base class

Trang 12

Initialization order

Initialization options executed in well defined order

1 fields set to default values

2 variable initializers run in textual order top to bottom

3 constructor executed

Trang 13

• Three common options for field initialization

– default values, variable initializers, constructors

– executed in well defined order

Ngày đăng: 25/01/2014, 15:20

w