Day 20
This presentation is the property of its rightful owner.
Sponsored Links
1 / 12

Day 20 PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on
  • Presentation posted in: General

Day 20. 5.5 Constructors. When you create an object for a class, you often want certain initializing actions performed, such as giving values to the instance variables.

Download Presentation

Day 20

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Day 20

Day 20

5.5 Constructors


Day 20

  • When you create an object for a class, you often want certain initializing actions performed, such as giving values to the instance variables.


Day 20

  • When you create an object for a class, you often want certain initializing actions performed, such as giving values to the instance variables.

  • A constructor is a special kind of method that is designed to perform such initializations.


Day 20

  • Until now, we have created new objects in the manner illustrated by the following example: Pet goodScout = new Pet();

    This creates an object whose initial values have no values at all.


Day 20

  • To make things even more efficient and faster, we use constructors.

  • A constructor is a method that is called when a new object is created. It can perform any action you write into its definition, but a constructor is meant to perform initializing actions.


Day 20

  • Unlike set methods, constructors are called almost automatically whenever you create an object using the new operator.


Example

Example

In the first file, you can write the first method as public PetRecord(String initialName, intinitialAge, double initialWeight){ … }


Different scenarios

Different Scenarios

  • When an object is created, a person may initialize some, all, or none of the instance variables. So we make constructors for reasonable scenarios.


Different scenarios1

Different Scenarios

  • So we make constructors for reasonable scenario:Notice: these are not void methodspublic PetRecord() // default constructorpublic PetRecord(String initialName)public PetRecord(intinitialAge)public PetRecord(double initialWeight)public PetRecord(String initialName, intinitialAge, double initialWeight)


Write code first file

Write Code (first file)

PetRecord- name: String- age: int- weight: double + writeOutput: void+ PetRecord(String iName, intiAge, double iWeight)+ PetRecord(String iName)+ PetRecord(intiAge)+ PetRecord(double iWeight)+ PetRecord()+ set(String nName, intnAge, double nWeight): void+ set(String nName): void+ set(intnAge): void+ set(double nWeight): void


Write code first file1

Write Code (first file)

+ set(String nName, intnAge, double nWeight): void+ set(String nName): void+ set(intnAge): void+ set(double nWeight): void+ getName: String+ getAge: int+ getWeight: double


At home do homework on wiki

At Home, do homework on WIki

  • Right now ... We gotta do some JavaScript Web Development


  • Login