Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers,

Similar presentations


Presentation on theme: "Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers,"— Presentation transcript:

1 Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers, each with their own myName field In Java, the transform method for NameDropper is:In Java, the transform method for NameDropper is: –this.myName means this instance’s myName field Hello Rumi says Hello Maria says Hello Rumi Maria String transform(String thePhrase) { return this.myName + " says " + thePhrase; return this.myName + " says " + thePhrase;}

2 Fundamentals of Software Development 1Slide 2 Fields and Constructors A NameDropper must know its name from the very beginningA NameDropper must know its name from the very beginning –We use a constructor to set the field called myName : –Then, when we invoke NameDropper’s constructor, we give it an argument: NameDropper(String whatMyNameIs) { this.myName = whatMyNameIs; } new NameDropper("Rumi"); new NameDropper("Maria");

3 Fundamentals of Software Development 1Slide 3 Class: Fields, Constructors, Methods public class NameDropper extends StringTransformer implements StringTransformable { private String name; // field: persistent storage, a permanent part // of each NameDropper private String name; // field: persistent storage, a permanent part // of each NameDropper public NameDropper(String whatMyNameIs) { // Constructor public NameDropper(String whatMyNameIs) { // Constructor this.name = whatMyNameIs; this.name = whatMyNameIs; } public String transform(String whatToSay) { // Method public String transform(String whatToSay) { // Method return this.name + " says " + whatToSay; 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.


Download ppt "Fundamentals of Software Development 1Slide 1 One class can describe many different instancesOne class can describe many different instances –Two NameDroppers,"

Similar presentations


Ads by Google