1 / 11

Methods

Methods. Creation Parameters Return values. Methods definition. Sometimes called functions Methods implements some operation of objects Identifier begins with a small letter. If consists of more than one word everynext word starts with capital letter

mai
Download Presentation

Methods

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. Methods Creation Parameters Return values

  2. Methods definition • Sometimes calledfunctions • Methods implements some operation of objects • Identifier begins with a small letter. If consists of more than one word everynext word starts with capital letter • Examples:setColor(), openWindow(), readData() • Methods definition belongs into class declaration • It is not possible to put one method into another

  3. Methods definition Method’s head modifiertypeidentifier(formal_parametrs) { local_variables; statements; return ret_value; } • Modifier defines access level – public, private, protected (see later) • Typespecifies the type of the return value • Parameters specifies type and identifier of input values body If type is other then void

  4. Methods • Executes algorithm of some operation • Works with class attributes and parameters input values (data) • Can return a value output data

  5. Access methods • Selectors • Do not modify values only get access to particular attributes in so called read-only mode • The value can be computed based on the values of class attributes • They are called get methods - getColor() • Modifiers • Change the values of attributes • Transforms input data (parameters) into values of attributes • They are called set methods - setColor(value)

  6. Methods without return value public voiddrawSomething() { graph.drawRect(10,10,200,110); } Methods with parameters public void kresliObdelnik(int sirka,int vyska) { graph.drawRect (10,10,sirka,vyska); }

  7. Methods with return value public int maximum(int a, int b) { return (a>b)? a : b; } • If the there is a given type in the method’s head (i.e. type is other thenvoid), there has to be also return in the body of the method • returndiscontinues the execution of the method

  8. Calling (using) methods • Call statement If methods returns a value: variable = method_identifier(actual_parameters); If not: method_identifier(actual_parameters); • Calling methods of other objects object_identifier.method_identifier();

  9. Calling methods (example) int a = 10, c; c = maximum (a,200); drawRectangle(230,222); drawCircle(100,maximum(a,200)); out.print(“Hello”);

  10. Actual and formal parameters • Formal parameters • Only in the methods definition • Becomes local variable • Value is given by copying from an actual parameter • Actual parameters • When calling a method • Data (constants, attributes, variables), that are transferred into methods • Are copied into formal params

  11. Actual and formal parameters • Params of primitive data types(int, float, char, also String ...) • Changes to the formal parameter inside a method doesn’t effect actual parameters • Params of reference data types (arrays, instances, interfaces) • Changes to the formal parameter inside a method does effect actual parameters

More Related