1 / 12

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.

salene
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 20 5.5 Constructors

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

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

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

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

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

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

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

  9. 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)

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

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

  12. At Home, do homework on WIki • Right now ... We gotta do some JavaScript Web Development

More Related