Day 20
Download
1 / 12

Day 20 - PowerPoint PPT Presentation


  • 133 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Day 20' - salene


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



  • 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.



  • To make things even more efficient and faster, illustrated by the following example: 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.



Example
Example automatically whenever you create an object using the

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


Different scenarios
Different Scenarios automatically whenever you create an object using the

  • 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 automatically whenever you create an object using the

  • 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) automatically whenever you create an object using the

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) automatically whenever you create an object using the

+ 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 automatically whenever you create an object using the WIki

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


ad