Call an object method: Difference between revisions

From Rosetta Code
Content added Content deleted
(added go)
(added smalltalk)
Line 123: Line 123:


// Instance
// Instance
[myInstance method:someParameter];</lang>
[myInstance method:someParameter];

// Method with multiple arguments
[myInstance methodWithRed:arg1 green:arg2 blue:arg3];

// Method with no arguments
[myInstance method];</lang>


=={{header|Perl}}==
=={{header|Perl}}==
Line 206: Line 212:
# Calling a method with no parameters
# Calling a method with no parameters
myInstance.anotherMethod</lang>
myInstance.anotherMethod</lang>

=={{header|Smalltalk}}==
In Smalltalk, calling an instance method is sending a message to an instance, and calling a class method is sending a message to a class object. Class methods are inherited through inheritance of classes. All messages (whether sent to a normal object or class object) are resolved dynamically at runtime, hence there are no "static" methods.
<lang smalltalk>" Class "
MyClass method:someParameter .
" or equivalently "
foo := MyClass .
foo method:someParameter .

" Instance "
myInstance method:someParameter .

" Method with multiple arguments "
myInstance methodWithRed:arg1 green:arg2 blue:arg3 .

" Method with no arguments "
myInstance method .

" Operator method "
myInstance + somethingElse .</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 20:57, 20 September 2011

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 or object. 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 or class method, and an instance method of a class.

ActionScript

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

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

C++

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

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

E

This example is in need of improvement:

Add runnable example.

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.

Go

<lang go>type Foo int // some custom type

// method on the type itself; can be called on that type or its pointer func (Foo) ValueMethod(x int) { }

// method on the pointer to the type; can be called on pointers func (*Foo) PointerMethod(x int) { }


var myValue Foo var myPointer *Foo = new(Foo)

// Calling value method on value myValue.ValueMethod(someParameter) // Calling pointer method on pointer myPointer.PointerMethod(someParameter)

// Value methods can always be called on pointers // equivalent to (*myPointer).ValueMethod(someParameter) myPointer.ValueMethod(someParameter)

// In a special case, pointer methods can be called on values that are addressable (i.e. lvalues) // equivalent to (&myValue).PointerMethod(someParameter) myValue.PointerMethod(someParameter)

// You can get the method out of the type as a function, and then explicitly call it on the object Foo.ValueMethod(myValue, someParameter) (*Foo).PointerMethod(myPointer, someParameter) (*Foo).ValueMethod(myPointer, someParameter)</lang>

J

Note: In some languages "everything is an object". This is not the case in J. In J, non-trivial objects will contain arrays. This is a relatively deep issue, beyond the scope of this page, except that: method invocations are relatively rare, in 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>

Instance methods may not be called on references whose value is null (throws a NullPointerException).

Note: Static methods can also be called using the dot syntax on a reference to an instance, looking identical to an instance method call, but this is highly discouraged, and compilers usually complain about that (with a warning, not a failed compilation). The reason for this is that such a call is actually equivalent to a static method call on the static (compile-time) type of the reference in the source code. That means the runtime value of that reference is actually irrelevant, and the reference may be null or be a reference to an instance of a subclass, the compiler will still make it a call to the static method based on the class of the value at compile-time (i.e. inheritance does not apply; the method call is not resolved at runtime).

This means that there is no benefit to making a static method call this way, because one can make the call based on the static type that is already known from looking at the code when it is written. Furthermore, such a call may lead to serious pitfalls, leading one to believe that changing the object that it is called on could change the behavior. For example, a common snippet to sleep the thread for 1 second is the following: Thread.currentThread().sleep(1000);. However, since .sleep() is actually a static method, the compiler replaces it with Thread.sleep(1000); (since Thread.currentThread() has a return type of Thread). This makes it clear that the thread that is put to sleep is not under the user's control. However, written as Thread.currentThread().sleep(1000);, it may lead one to falsely believe it to be possible to sleep another thread by doing someOtherThread.sleep(1000); when in fact such a statement would also sleep the current thread.

Objective-C

In Objective-C, calling an instance method is sending a message to an instance, and calling a class method is sending a message to a class object. Class methods are inherited through inheritance of classes. All messages (whether sent to a normal object or class object) are resolved dynamically at runtime, hence there are no "static" methods. <lang objc>// Class [MyClass method:someParameter]; // or equivalently: id foo = [MyClass class]; [foo method:someParameter];

// Instance [myInstance method:someParameter];

// Method with multiple arguments [myInstance methodWithRed:arg1 green:arg2 blue:arg3];

// Method with no arguments [myInstance method];</lang>

Perl

<lang perl># Class method MyClass->classMethod($someParameter);

  1. Equivalently using a class name

my $foo = 'MyClass'; $foo->classMethod($someParameter);


  1. Instance method

$myInstance->method($someParameter);

  1. Calling a method with no parameters

$myInstance->anotherMethod;

  1. Class and instance method calls are made behind the scenes by getting the function from
  2. the package and calling it on the class name or object reference explicitly

MyClass::classMethod('MyClass', $someParameter); MyClass::method($myInstance, $someParameter);</lang>

Perl 6

<lang perl6>ClassName.methodname(); # call class method $instance.methodname(); # call instance method</lang> Note that, unlike in Perl 5, class names are type objects, not strings. (Type objects are typed variants of the undefined value in Perl 6.)

PHP

<lang php>// Static method MyClass::method($someParameter); // In PHP 5.3+, static method can be called on a string of the class name $foo = 'MyClass; $foo::method($someParameter);


// Instance method $myInstance->method($someParameter);</lang>

PicoLisp

Method invocation is syntactically equivalent to normal function calls. Method names have a trailing '>' by convention. <lang PicoLisp>(foo> MyClass) (foo> MyObject)</lang>

Python

<lang python>class MyClass(object): @classmethod def myClassMethod(self, x): pass @staticmethod def myStaticMethod(x): pass def myMethod(self, x): return 42 + x

myInstance = MyClass()

  1. Instance method

myInstance.myMethod(someParameter)

  1. A method can also be retrieved as an attribute from the class, and then explicitly called on an instance:

MyClass.myMethod(myInstance, someParameter)


  1. Class or static methods

MyClass.myClassMethod(someParameter) MyClass.myStaticMethod(someParameter)

  1. You can also call class or static methods on an instance, which will simply call it on the instance's class

myInstance.myClassMethod(someParameter) myInstance.myStaticMethod(someParameter)</lang>

Ruby

<lang ruby># Class method MyClass.method(someParameter)

  1. Class may be computed at runtime

foo = MyClass foo.method(someParameter)


  1. Instance method

myInstance.method(someParameter)

  1. The parentheses are optional

myInstance.method someParameter

  1. Calling a method with no parameters

myInstance.anotherMethod</lang>

Smalltalk

In Smalltalk, calling an instance method is sending a message to an instance, and calling a class method is sending a message to a class object. Class methods are inherited through inheritance of classes. All messages (whether sent to a normal object or class object) are resolved dynamically at runtime, hence there are no "static" methods. <lang smalltalk>" Class " MyClass method:someParameter . " or equivalently " foo := MyClass . foo method:someParameter .

" Instance " myInstance method:someParameter .

" Method with multiple arguments " myInstance methodWithRed:arg1 green:arg2 blue:arg3 .

" Method with no arguments " myInstance method .

" Operator method " myInstance + somethingElse .</lang>

Tcl

<lang tcl>package require Tcl 8.6

  1. "Static" (on class object)

MyClass mthd someParameter

  1. Instance

$myInstance mthd someParameter</lang>