day 20
Download
Skip this Video
Download Presentation
Day 20

Loading in 2 Seconds...

play fullscreen
1 / 12

Day 20 - PowerPoint PPT Presentation


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

slide2

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

slide3

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

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.

slide5

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

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
ad