Jump to content

Call an object method: Difference between revisions

no edit summary
No edit summary
Line 171:
Output from object1: Example of calling an instance method
Output from object1: Example of calling an instance method from an alias</pre>
 
 
=={{header|C}}==
C has structures and it also has function pointers, which allows C structures to be associated with any function with the same signature as the pointer. Thus, C structures can also have object methods.
Line 249 ⟶ 251:
*> foo-factory can be treated like a normal object reference.
INVOKE foo-factory "someMethod"</syntaxhighlight>
 
 
=={{header|CoffeeScript}}==
While CoffeeScript does provide a useful class abstraction around its prototype-based inheritance, there aren't any actual classes.
Line 260 ⟶ 264:
foo.instanceMethod() #=> 'Baz'
Foo.staticMethod() #=> 'Bar'</syntaxhighlight>
 
 
=={{header|Common Lisp}}==
In Common Lisp, classmethods are methods that apply to classes, rather than classes that contain methods.
Line 285 ⟶ 291:
Value of x^2: 100
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
This example shows the creation of a simple integer stack object. The object contains "Push" and "Pop" static methods.
 
<syntaxhighlight lang="Delphi">
 
{Simple stack interface}
 
type TSimpleStack = class(TObject)
private
FStack: array of integer;
protected
public
procedure Push(I: integer);
function Pop(var I: integer): boolean;
constructor Create;
end;
 
 
{ TSimpleStack implementation }
 
constructor TSimpleStack.Create;
{Initialize stack by setting size to zero}
begin
SetLength(FStack,0);
end;
 
function TSimpleStack.Pop(var I: integer): boolean;
{Pop top item off stack into "I" returns False if stack empty}
begin
Result:=Length(FStack)>=1;
if Result then
begin
{Get item from top of stack}
I:=FStack[High(FStack)];
{Delete the top item}
SetLength(FStack,Length(FStack)-1);
end;
end;
 
procedure TSimpleStack.Push(I: integer);
{Push item on stack by adding to end of array}
begin
{Increase stack size by one}
SetLength(FStack,Length(FStack)+1);
{Insert item}
FStack[High(FStack)]:=I;
end;
 
 
procedure ShowStaticMethodCall(Memo: TMemo);
var Stack: TSimpleStack; {Declare stack object}
var I: integer;
begin
{Instanciate stack object}
Stack:=TSimpleStack.Create;
{Push items on stack by calling static method "Push"}
for I:=1 to 10 do Stack.Push(I);
{Call static method "Pop" to retrieve and display stack items}
while Stack.Pop(I) do
begin
Memo.Lines.Add(IntToStr(I));
end;
{release stack memory and delete object}
Stack.Free;
end;
 
 
 
</syntaxhighlight>
{{out}}
<pre>
10
9
8
7
6
5
4
3
2
1
Elapsed Time: 10.571 ms.
</pre>
 
 
=={{header|D}}==
<syntaxhighlight lang="d">struct Cat {
Line 323 ⟶ 417:
assert(d.dynamicMethod() == "Woof!");
}</syntaxhighlight>
 
 
 
=={{header|Dragon}}==
Making an object of class and then calling it.
465

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.