Call an object method

From Rosetta Code
Revision as of 13:13, 18 August 2011 by Rdm (talk | contribs) (→‎{{header|J}})
Call an object method is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

In object-oriented programming a method is a function associated with a particular class. In most forms of object oriented implementations methods can be static, associated with the class itself; or instance, associated with an instance of a class.

Show how to call a static method, and an instance method of a class.

ActionScript

<lang actionscript>// Static MyClass.method(someParamater);

// Instance myInstance.method(someParamater);</lang>

C++

<lang cpp>// Static MyClass::method(someParamater);

// Instance myInstance.method(someParamater);</lang>

E

A method call in E has the syntax recipient.verb(arg1, arg2, ...), where recipient is an expression, verb is an identifier (or a string literal preceded by ::), and argN are expressions.

<lang e>someObject.someMethod(someParameter)</lang>

In E, there are no distinguished “static methods”. Instead, it is idiomatic to place methods on the maker of the object. This is very similar to methods on constructors in JavaScript, or class methods in Objective-C.


This example is in need of improvement:

Add runnable example.

J

All method invocations must contain two underline characters in J:

Static:

<lang j>methodName_className_ parameters</lang>

and, given an object instance reference:

<lang j>objectReference=: conew 'className'</lang>

an instance invocation could be:

<lang j>methodName__objectReference parameters</lang>

Note that J also supports infix notation when using methods. In this case, there will be a second parameter list, on the left of the method reference.

<lang j>parameters methodName_className_ parameters</lang> or <lang j>parameters methodName__objectReference parameters</lang>

These variations might be useful when building combining words that need to refer to two different kinds of things. But mostly it's to be consistent with the rest of the language.

Finally, note that static methods can be referred to using the same notation as instance methods -- in this case you must have a reference to the class name:

<lang j>classReference=: <'className' methodName__classReference parameters</lang>

This might be useful when you are working with a variety of classes which share a common structure.

You can also refer to a dynamic method in the same fashion that you use to refer to a instance method -- in this case you must know the object name (which is a number). For example, to refer to a method on object 123

<lang j>methodName_123_ parameters</lang>

This last case can be useful when debugging.

Java

Static methods in Java are usually called by using the dot operator on a class name: <lang java>ClassWithStaticMethod.staticMethodName(argument1, argument2);//for methods with no arguments, use empty parentheses</lang> Instance methods are called by using the dot operator on an instance: <lang java>ClassWithMethod varName = new ClassWithMethod(); varName.methodName(argument1, argument2); //or (new ClassWithMethod()).methodName(argument1, argument2);</lang> Static methods can also be called using instances, but compilers usually complain about that (with a warning, not a failed compilation). The call looks identical to an instance method call. Methods may not be called on uninitialized objects or objects initialized to null (throws a NullPointerException).

Python

<lang python>>>> class MyClass(): def myMethod(self): return 42


>>> myInstance = MyClass() >>> # Call static method on a separate instance >>> MyClass.myMethod(myInstance) 42 >>> # Call instance method implicitely on its instance >>> myInstance.myMethod() 42 >>> </lang>

Tcl

<lang tcl>package require Tcl 8.6

  1. "Static" (on class object)

MyClass mthd someParameter

  1. Instance

$myInstance mthd someParameter</lang>