Method invocation versus definition To define write a

  • Slides: 3
Download presentation
Method invocation versus definition • To define (write) a method means to write its

Method invocation versus definition • To define (write) a method means to write its code – The code is a recipe to follow when the method runs – Methods often have parameters – information that comes into the method when it is run • To invoke (call, run) a method means to cause the method’s code to run – Sending it actual values to substitute for the formal parameters of the method • Example (on next slides) Fundamentals of Software Development 1 1

Method invocation versus definition • To define (write) a method means to write its

Method invocation versus definition • To define (write) a method means to write its code • To invoke (call, run) a method means to cause the method’s code to run • Example (on next slide): – The Name. Dropper class in the blue box defines: • • A field called my. Name to hold the Name. Dropper’s name to drop A constructor called Name. Dropper that takes the name to store in the field • A method called transform that takes a phrase to tranform – The statements in the yellow box cause the Name. Dropper constructor and transform method to run • They each run several times, with different actual arguments substituted for the formal parameters in the Name. Dropper definition • Software These statements would themselves appear in some other class Fundamentals of Development 1 2

public class Name. Dropper extends String. Transformer implements String. Transformable { private String my.

public class Name. Dropper extends String. Transformer implements String. Transformable { private String my. Name; Public Name. Dropper(String name. To. Use) { this. my. Name = name. To. Use; } Definition of the Name. Dropper class public transform(String phrase) { return this. my. Name + “says ” + phrase; } Name. Dropper person 1, person 2, person 3; Invoking the Name. Dropper person 1 = new Name. Dropper(“Calvin”); constructor and person 2 = new Name. Dropper(“Hobbes”); transform method person 3 = new Name. Dropper(“lobster”); person 1. transform(“you look funny today, Hobbes. ”); person 2. transform(“you looker even funnier. ”); person 1. transform(“no, YOU look funnier. ”); Fundamentals of Software person 3. transorm(“I amd just a lonely lobster. ”); Development 1 3