Call an object method: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
(5 intermediate revisions by 3 users not shown)
Line 238:
(. 1 (equals 2)) ; alternative style</syntaxhighlight>
=={{header|COBOL}}==
{{Works with|COBOL 2002}}
COBOLThere hasare two ways to invoke a method: the <code>INVOKE</code> statement and inline method invocation.
<syntaxhighlight lang="cobolcobolfree">*> INVOKE statements.
INVOKE FooClass "someMethod" RETURNING bar *> Factory object
INVOKE foomy-instanceclass "anotherMethodsome-method" RETURNING bar *> InstanceFactory object
USING BY REFERENCE some-parameter
RETURNING foo
INVOKE my-instance "another-method" *> Instance object
USING BY REFERENCE some-parameter
RETURNING foo
 
*> Inline method invocation.
MOVE FooClassmy-class::"someMethodsome-method"(some-parameter) TO bar foo *> Factory object
MOVE foomy-instance::"anotherMethodanother-method"(some-parameter) TO barfoo *> Instance object</syntaxhighlight>
 
To call factory methods of objects of an unknown type (such as when you may have a subclass of the class wanted), it is necessary to get a reference to the class's factory object by calling the <code>"FactoryObject"</code> method.
<syntaxhighlight lang="cobolcobolfree">INVOKE foosome-instance "FactoryObject" RETURNING foo-factory
*> foo-factory can be treated like a normal object reference.
INVOKE foo-factory "someMethod"</syntaxhighlight>
 
 
=={{header|CoffeeScript}}==
Line 734 ⟶ 738:
Hello static world!
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn FBClassDemo
// Class
ClassRef class = fn MutableStringClass
// Cocoa base class name as human-readable string
print fn StringFromClass( class )
// Instantiate
CFMutableStringRef mutStr = fn MutableStringNew
// Method with single argument
MutableStringSetString( mutStr, @"Hello, World!" )
print mutStr
// Method with multiple arguments
MutableStringReplaceAllOccurrencesOfString( mutStr, @"World", @"Rosetta Code" )
print mutStr
end fn
 
fn FBClassDemo
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
NSMutableString
Hello, World!
Hello, Rosetta Code!
</pre>
 
=={{header|Go}}==
Go distances itself from the word "object" and from many object oriented concepts. It does however have methods. Any user-defined type in Go can have methods and these work very much like "instance methods" of object oriented languages. The examples below illustrate details of Go methods and thus represent the concept of instance methods.
Line 2,262 ⟶ 2,298:
=={{header|Wren}}==
Note that it's possible in Wren for instance and static methods in the same class to share the same name. This is because static methods are considered to belong to a separate meta-class.
<syntaxhighlight lang="ecmascript"wren>class MyClass {
construct new() {}
method() { System.print("instance method called") }
Line 2,277 ⟶ 2,313:
static method called
</pre>
 
=={{header|XBS}}==
You can call object methods using two types of structures. Classes and Objects.
Line 2,341 ⟶ 2,378:
Zig does not have classes nor objects. Zig's structs, however, can have methods; but they are not special. They are only namespaced functions that can be called with dot syntax.
 
<syntaxhighlight lang="zig">const asserttesting = @import("std").debug.asserttesting;
 
pub const ID = struct {
Line 2,371 ⟶ 2,408:
};
 
assert(person1.// test getAge() ==method 18);call
asserttry testing.expectEqual(ID@as(u7, 18), person1.getAge(person2) == 20);
try testing.expectEqual(@as(u7, 20), ID.getAge(person2));
}</syntaxhighlight>
 
=={{header|zkl}}==
In zkl, a class can be static (there will only exist one instance of the class). A function is always a member of some class but will only be static if it does not refer to instance data.
9,485

edits