One class can describe many different instances - PowerPoint PPT Presentation

1 / 3
About This Presentation
Title:

One class can describe many different instances

Description:

Rumi. Maria. String transform(String thePhrase) { return this. ... new NameDropper('Rumi'); new NameDropper('Maria'); Fundamentals of Software Development 1 ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 4
Provided by: davidmutch8
Category:

less

Transcript and Presenter's Notes

Title: One class can describe many different instances


1
Maria
Hello
Rumi
Rumi says Hello
Maria says Hello
  • One class can describe many different instances
  • Two NameDroppers, each with their own myName
    field
  • In Java, the transform method for NameDropper is
  • this.myName means this instances myName
    field

String transform(String thePhrase) return
this.myName " says " thePhrase
2
Fields and Constructors
  • A NameDropper must know its name from the very
    beginning
  • We use a constructor to set the field called
    myName
  • Then, when we invoke NameDroppers constructor,
    we give it an argument

NameDropper(String whatMyNameIs)
this.myName whatMyNameIs
new NameDropper("Rumi") new NameDropper("Maria")
3
ClassFields, Constructors, Methods
  • public class NameDropper extends
    StringTransformer
    implements StringTransformable
  • private String name // field persistent
    storage, a permanent part
    // of each
    NameDropper
  • public NameDropper(String whatMyNameIs) //
    Constructor
  • this.name whatMyNameIs
  • public String transform(String whatToSay) //
    Method
  • return this.name " says " whatToSay

Questions?
Private means private to the class. The standard
is that fields are generally private and all else
is public (for now) more on this later.
Write a Comment
User Comments (0)
About PowerShow.com