110 likes | 233 Vues
Methods, also known as functions, are blocks of code in programming that perform specific operations on objects. They begin with a lowercase letter and follow camelCase for multiple words (e.g., `setColor()`, `openWindow()`). Methods are declared within a class and can have modifiers like public, private, or protected, determining their accessibility. Their return types and parameters specify input and output data. The distinction between actual and formal parameters highlights how data is transferred into methods, crucial for understanding method execution outcomes.
E N D
Methods Creation Parameters Return values
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
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
Methods • Executes algorithm of some operation • Works with class attributes and parameters input values (data) • Can return a value output data
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)
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); }
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
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();
Calling methods (example) int a = 10, c; c = maximum (a,200); drawRectangle(230,222); drawCircle(100,maximum(a,200)); out.print(“Hello”);
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
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