Initialization

Chia sẻ: Shy Monk | Ngày: | Loại File: PPT | Số trang:0

0
79
lượt xem
5
download

Initialization

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Present field initialization options for fields – default values – variable initializers – constructors

Chủ đề:
Lưu

Nội dung Text: Initialization

  1. Initialization
  2. Objectives • Present field initialization options for fields – default values – variable initializers – constructors 2
  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 class Rational { instance fields int numerator; a int denominator; numerator 0 ... denominator 0 } set to default values Rational a = new Rational(); 3
  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 class Rational { int numerator; initialize int denominator = 1; ... } 4
  5. Constructor • Class can supply constructor to do initialization – automatically invoked when object created – implemented using same name as class with no return type class Rational { int numerator; int denominator; constructor public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } ... } 5
  6. Invoking constructor • Constructor automatically invoked when object created create object, Rational a = new Rational(2, 5); invoke constructor a numerator 2 denominator 5 6
  7. Multiple constructors • Class can supply multiple constructors – parameter lists must be different class Rational { constructor public Rational(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } constructor public Rational(int numerator) { this.numerator = numerator; this.denominator = 1; } ... } 7
  8. Default constructor • Can supply constructor that takes no arguments – often called the default constructor class Rational { no argument public Rational() constructor { this.denominator = 1; } ... } 8
  9. Selecting constructor to invoke • Compiler selects constructor version automatically – based on arguments passed two ints Rational a = new Rational(2, 5); one int Rational b = new Rational(6); no arguments Rational c = new Rational(); 9
  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) call 2 argument :this(numerator, 1) constructor { ... } ... } 10
  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 11
  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 12
  13. Summary • Three common options for field initialization – default values, variable initializers, constructors – executed in well defined order 13
Đồng bộ tài khoản