Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor.

Similar presentations


Presentation on theme: "Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor."— Presentation transcript:

1 Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor –How to use a constructor with the new operator And why to use oneAnd why to use one –What the this object is The next several slides review each of these ideas

2 Fundamentals of Software Development 1Slide 2 Writing Constructors public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} A class can have many constructors, each with its own footprint (order and types of parameters) This constructor can be used to initialize the name field with the given name This constructor can be used to initialize the name field to the default value of “Who knows” This constructor can be used to initialize the name field as “Car number” Questions?

3 Fundamentals of Software Development 1Slide 3 Using Constructors public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} x2 = new NameDropper("Ali"); x2.transform("Hi") yields "Ali says Hi" x1 = new NameDropper(); x1.transform("Hi") yields "Who knows says Hi" x3 = new NameDropper(54); x3.transform("Hi") yields "Car 54 says Hi" NameDropper x1, x2, x3; Write 3 statements that construct x1.. x3 using the 3 constructors, respectively. Questions?

4 Fundamentals of Software Development 1Slide 4 The this object public class NameDropper extends StringTransformer implements StringTransformable { extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper () { public NameDropper () { this.name = "Who knows" ; } public NameDropper(String givenName) { public NameDropper(String givenName) { this.name = givenName; this.name = givenName; } public NameDropper (int givenNumber) { public NameDropper (int givenNumber) { this.name = "Car " + GivenNumber; this.name = "Car " + GivenNumber; } public String transform(String x) { public String transform(String x) { return this.name + " says " + x; }} x1 = new NameDropper(); x2 = new NameDropper("Ali"); x3 = new NameDropper(54); During execution of: x1.transform("Hi") the keyword this in NameDropper refers to the same object to which x1 refers. During execution of: x2.transform("Hi") the keyword this in NameDropper refers to the same object to which x2 refers. And so forth. Questions about what the keyword this means?


Download ppt "Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor."

Similar presentations


Ads by Google