Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
6
Task/Call-an-object-method/00-META.yaml
Normal file
6
Task/Call-an-object-method/00-META.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
category:
|
||||
- Object oriented
|
||||
- Encyclopedia
|
||||
from: http://rosettacode.org/wiki/Call_an_object_method
|
||||
note: Basic language learning
|
||||
3
Task/Call-an-object-method/00-TASK.txt
Normal file
3
Task/Call-an-object-method/00-TASK.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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.
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
BEGIN # demonstrate a possible method of simulating class & instance methods #
|
||||
# declare a "class" #
|
||||
MODE ANIMAL = STRUCT( STRING species
|
||||
, PROC( REF ANIMAL )VOID print # instance method #
|
||||
, PROC VOID cm # class method #
|
||||
);
|
||||
# constructor #
|
||||
PROC new animal = ( STRING species )REF REF ANIMAL:
|
||||
BEGIN
|
||||
HEAP ANIMAL newv := ANIMAL( species
|
||||
, ( REF ANIMAL this )VOID:
|
||||
print( ( "[animal instance[", species OF this, "]]" ) )
|
||||
, VOID: print( ( "[animal class method called]" ) )
|
||||
);
|
||||
HEAP REF ANIMAL newa := newv;
|
||||
newa
|
||||
END # new animal # ;
|
||||
|
||||
REF ANIMAL a
|
||||
:= new animal( "PANTHERA TIGRIS" ); # create an instance of ANIMAL #
|
||||
cm OF a; # call the class method #
|
||||
( print OF a )( a ) # call the instance method #
|
||||
END
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Static
|
||||
MyClass.method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.method(someParameter);
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package My_Class is
|
||||
type Object is tagged private;
|
||||
procedure Primitive(Self: Object); -- primitive subprogram
|
||||
procedure Dynamic(Self: Object'Class);
|
||||
procedure Static;
|
||||
private
|
||||
type Object is tagged null record;
|
||||
end My_Class;
|
||||
18
Task/Call-an-object-method/Ada/call-an-object-method-2.ada
Normal file
18
Task/Call-an-object-method/Ada/call-an-object-method-2.ada
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package body My_Class is
|
||||
procedure Primitive(Self: Object) is
|
||||
begin
|
||||
Put_Line("Hello World!");
|
||||
end Primitive;
|
||||
|
||||
procedure Dynamic(Self: Object'Class) is
|
||||
begin
|
||||
Put("Hi there! ... ");
|
||||
Self.Primitive; -- dispatching call: calls different subprograms,
|
||||
-- depending on the type of Self
|
||||
end Dynamic;
|
||||
|
||||
procedure Static is
|
||||
begin
|
||||
Put_Line("Greetings");
|
||||
end Static;
|
||||
end My_Class;
|
||||
11
Task/Call-an-object-method/Ada/call-an-object-method-3.ada
Normal file
11
Task/Call-an-object-method/Ada/call-an-object-method-3.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package Other_Class is
|
||||
type Object is new My_Class.Object with null record;
|
||||
overriding procedure Primitive(Self: Object);
|
||||
end Other_Class;
|
||||
|
||||
package body Other_Class is
|
||||
procedure Primitive(Self: Object) is
|
||||
begin
|
||||
Put_Line("Hello Universe!");
|
||||
end Primitive;
|
||||
end Other_Class;
|
||||
20
Task/Call-an-object-method/Ada/call-an-object-method-4.ada
Normal file
20
Task/Call-an-object-method/Ada/call-an-object-method-4.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Call_Method is
|
||||
|
||||
package My_Class is ... -- see above
|
||||
package body My_Class is ... -- see above
|
||||
|
||||
package Other_Class is ... -- see above
|
||||
package body Other_Class is ... -- see above
|
||||
|
||||
Ob1: My_Class.Object; -- our "root" type
|
||||
Ob2: Other_Class.Object; -- a type derived from the "root" type
|
||||
|
||||
begin
|
||||
My_Class.Static;
|
||||
Ob1.Primitive;
|
||||
Ob2.Primitive;
|
||||
Ob1.Dynamic;
|
||||
Ob2.Dynamic;
|
||||
end Call_Method;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Static
|
||||
MyClass.method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.method(someParameter);
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
class myClass
|
||||
{
|
||||
Method(someParameter){
|
||||
MsgBox % SomeParameter
|
||||
}
|
||||
}
|
||||
|
||||
myClass.method("hi")
|
||||
myInstance := new myClass
|
||||
myInstance.Method("bye")
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
( ( myClass
|
||||
= (name=aClass)
|
||||
( Method
|
||||
= .out$(str$("Output from " !(its.name) ": " !arg))
|
||||
)
|
||||
(new=.!arg:?(its.name))
|
||||
)
|
||||
& (myClass.Method)$"Example of calling a 'class' method"
|
||||
& new$(myClass,object1):?MyObject
|
||||
& (MyObject..Method)$"Example of calling an instance method"
|
||||
& !MyObject:?Alias
|
||||
& (Alias..Method)$"Example of calling an instance method from an alias"
|
||||
);
|
||||
8
Task/Call-an-object-method/C++/call-an-object-method.cpp
Normal file
8
Task/Call-an-object-method/C++/call-an-object-method.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// Static
|
||||
MyClass::method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.method(someParameter);
|
||||
|
||||
// Pointer
|
||||
MyPointer->method(someParameter);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Static
|
||||
MyClass.Method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.Method(someParameter);
|
||||
27
Task/Call-an-object-method/C/call-an-object-method.c
Normal file
27
Task/Call-an-object-method/C/call-an-object-method.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
|
||||
typedef struct{
|
||||
int x;
|
||||
int (*funcPtr)(int);
|
||||
}functionPair;
|
||||
|
||||
int factorial(int num){
|
||||
if(num==0||num==1)
|
||||
return 1;
|
||||
else
|
||||
return num*factorial(num-1);
|
||||
}
|
||||
|
||||
int main(int argc,char** argv)
|
||||
{
|
||||
functionPair response;
|
||||
|
||||
if(argc!=2)
|
||||
return printf("Usage : %s <non negative integer>",argv[0]);
|
||||
else{
|
||||
response = (functionPair){.x = atoi(argv[1]),.funcPtr=&factorial};
|
||||
printf("\nFactorial of %d is %d\n",response.x,response.funcPtr(response.x));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
*> INVOKE
|
||||
INVOKE FooClass "someMethod" RETURNING bar *> Factory object
|
||||
INVOKE foo-instance "anotherMethod" RETURNING bar *> Instance object
|
||||
|
||||
*> Inline method invocation
|
||||
MOVE FooClass::"someMethod" TO bar *> Factory object
|
||||
MOVE foo-instance::"anotherMethod" TO bar *> Instance object
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
INVOKE foo-instance "FactoryObject" RETURNING foo-factory
|
||||
*> foo-factory can be treated like a normal object reference.
|
||||
INVOKE foo-factory "someMethod"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
MyClass myClassObject;
|
||||
myClassObject.myFunction(some parameter);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(Long/toHexString 15) ; use forward slash for static methods
|
||||
(System/currentTimeMillis)
|
||||
|
||||
(.equals 1 2) ; use dot operator to call instance methods
|
||||
(. 1 (equals 2)) ; alternative style
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
class Foo
|
||||
@staticMethod: -> 'Bar'
|
||||
|
||||
instanceMethod: -> 'Baz'
|
||||
|
||||
foo = new Foo
|
||||
|
||||
foo.instanceMethod() #=> 'Baz'
|
||||
Foo.staticMethod() #=> 'Bar'
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(defclass my-class ()
|
||||
((x
|
||||
:accessor get-x ;; getter function
|
||||
:initarg :x ;; arg name
|
||||
:initform 0))) ;; initial value
|
||||
|
||||
;; declaring a public class method
|
||||
(defmethod square-x ((class-instance my-class))
|
||||
(* (get-x class-instance) (get-x class-instance)))
|
||||
|
||||
;; create an instance of my-class
|
||||
(defvar *instance*
|
||||
(make-instance 'my-class :x 10))
|
||||
|
||||
(format t "Value of x: ~a~%" (get-x *instance*))
|
||||
|
||||
(format t "Value of x^2: ~a~%" (square-x *instance*))
|
||||
37
Task/Call-an-object-method/D/call-an-object-method.d
Normal file
37
Task/Call-an-object-method/D/call-an-object-method.d
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
struct Cat {
|
||||
static int staticMethod() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
string dynamicMethod() { // Never virtual.
|
||||
return "Mew!";
|
||||
}
|
||||
}
|
||||
|
||||
class Dog {
|
||||
static int staticMethod() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
string dynamicMethod() { // Virtual method.
|
||||
return "Woof!";
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
// Static methods calls:
|
||||
assert(Cat.staticMethod() == 2);
|
||||
assert(Dog.staticMethod() == 5);
|
||||
|
||||
Cat c; // This is a value on the stack.
|
||||
Dog d; // This is just a reference, set to null.
|
||||
|
||||
// Other static method calls, discouraged:
|
||||
assert(c.staticMethod() == 2);
|
||||
assert(d.staticMethod() == 5);
|
||||
|
||||
// Instance method calls:
|
||||
assert(c.dynamicMethod() == "Mew!");
|
||||
d = new Dog;
|
||||
assert(d.dynamicMethod() == "Woof!");
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
{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;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
r = new run()
|
||||
r.val()
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
//Static method on a built-in type Integer
|
||||
static func Integer.Div(x, y) {
|
||||
x / y
|
||||
}
|
||||
|
||||
//Instance method
|
||||
func Integer.Div(n) {
|
||||
this / n
|
||||
}
|
||||
|
||||
print(Integer.Div(12, 3))
|
||||
print(12.Div(3))
|
||||
1
Task/Call-an-object-method/E/call-an-object-method.e
Normal file
1
Task/Call-an-object-method/E/call-an-object-method.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
someObject.someMethod(someParameter)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
type MyClass ^|we are defining a new data type and entering in its static context|^
|
||||
fun method = void by block do writeLine("static method called") end
|
||||
model ^|we enter the instance context|^
|
||||
fun method = void by block do writeLine("instance method called") end
|
||||
end
|
||||
type CallAnObjectMethod
|
||||
var myInstance = MyClass() ^|creating an instance|^
|
||||
myInstance.method()
|
||||
MyClass.method()
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
module CallMethod {
|
||||
/**
|
||||
* This is a class with a method and a function.
|
||||
*/
|
||||
const Example(String text) {
|
||||
@Override
|
||||
String toString() { // <-- this is a method
|
||||
return $"This is an example with text={text}";
|
||||
}
|
||||
|
||||
static Int oneMoreThan(Int n) { // <-- this is a function
|
||||
return n+1;
|
||||
}
|
||||
}
|
||||
|
||||
void run() {
|
||||
@Inject Console console;
|
||||
|
||||
Example example = new Example("hello!");
|
||||
String methodResult = example.toString(); // <-- call a method
|
||||
console.print($"Result from calling a method: {methodResult.quoted()}");
|
||||
|
||||
// Int funcResult = example.oneMoreThan(12); // <-- compiler error
|
||||
Int funcResult = Example.oneMoreThan(12); // <-- call a function
|
||||
console.print($"Results from calling a function: {funcResult}");
|
||||
|
||||
// methods and functions are also objects that can be manipulated;
|
||||
// note that "function String()" === "Function<<>, <String>>"
|
||||
Method<Example, <>, <String>> method = Example.toString;
|
||||
function String() func = method.bindTarget(example);
|
||||
console.print($"Calling a bound method: {func().quoted()}");
|
||||
|
||||
// by default, a method with target T converts to a function taking a T;
|
||||
// Ecstasy refers to this as "Bjarning" (because C++ takes "this" as a param)
|
||||
val func2 = Example.toString; // <-- type: function String()
|
||||
console.print($"Calling a Bjarne'd function: {func2(example).quoted()}");
|
||||
|
||||
// the function is just an object, and invocation (and in this case, binding,
|
||||
// as indicated by the '&' operator which requests a reference) is accomplished
|
||||
// using the "()" operator
|
||||
val func3 = Example.oneMoreThan; // <-- type: function Int(Int)
|
||||
val func4 = &func3(13); // <-- type: function Int()
|
||||
console.print($"Calling a fully bound function: {func4()}");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
console.printLine("Hello"," ","World!");
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
defmodule ObjectCall do
|
||||
def new() do
|
||||
spawn_link(fn -> loop end)
|
||||
end
|
||||
|
||||
defp loop do
|
||||
receive do
|
||||
{:concat, {caller, [str1, str2]}} ->
|
||||
result = str1 <> str2
|
||||
send caller, {:ok, result}
|
||||
loop
|
||||
end
|
||||
end
|
||||
|
||||
def concat(obj, str1, str2) do
|
||||
send obj, {:concat, {self(), [str1, str2]}}
|
||||
|
||||
receive do
|
||||
{:ok, result} ->
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
obj = ObjectCall.new()
|
||||
|
||||
IO.puts(obj |> ObjectCall.concat("Hello ", "World!"))
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
USING: accessors io kernel literals math sequences ;
|
||||
IN: rosetta-code.call-a-method
|
||||
|
||||
! Define some classes.
|
||||
SINGLETON: dog
|
||||
TUPLE: cat sassiness ;
|
||||
|
||||
! Define a constructor for cat.
|
||||
C: <cat> cat
|
||||
|
||||
! Define a generic word that dispatches on the object at the top
|
||||
! of the data stack.
|
||||
GENERIC: speak ( obj -- )
|
||||
|
||||
! Define methods in speak which specialize on various classes.
|
||||
M: dog speak drop "Woof!" print ;
|
||||
M: cat speak sassiness>> 0.5 > "Hiss!" "Meow!" ? print ;
|
||||
M: object speak drop "I don't know how to speak!" print ;
|
||||
|
||||
! Call speak on various objects.
|
||||
! Despite being a method, it's called like any other word.
|
||||
dog speak
|
||||
0.75 <cat> speak
|
||||
0.1 <cat> speak
|
||||
"bird" speak
|
||||
40
Task/Call-an-object-method/Forth/call-an-object-method-1.fth
Normal file
40
Task/Call-an-object-method/Forth/call-an-object-method-1.fth
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
include lib/compare.4th
|
||||
include 4pp/lib/foos.4pp
|
||||
|
||||
[ASSERT] \ enable assertions
|
||||
|
||||
:: Cat
|
||||
class
|
||||
method: dynamicCat \ virtual method
|
||||
end-class {
|
||||
|
||||
:static staticCat { 2 } ; \ static method
|
||||
:method { s" Mew!" } ; defines dynamicCat
|
||||
} \ for unrelated classes,
|
||||
; \ method names have to differ
|
||||
|
||||
:: Dog
|
||||
class
|
||||
method: dynamicDog \ virtual method
|
||||
end-class {
|
||||
|
||||
:static staticDog { 5 } ;
|
||||
:method { s" Woof!" } ; defines dynamicDog
|
||||
} \ for unrelated classes,
|
||||
; \ method names have to differ
|
||||
|
||||
static Cat c \ create two static objects
|
||||
static Dog d
|
||||
|
||||
: main
|
||||
assert( class -> staticCat 2 = ) \ check for valid method return
|
||||
assert( class -> staticDog 5 = ) \ of a static method
|
||||
|
||||
assert( c -> staticCat 2 = ) \ check for valid method return
|
||||
assert( d -> staticDog 5 = ) \ of a static method
|
||||
|
||||
assert( c => dynamicCat s" Mew!" compare 0= )
|
||||
assert( d => dynamicDog s" Woof!" compare 0= )
|
||||
; \ same for dynamic methods
|
||||
|
||||
main
|
||||
29
Task/Call-an-object-method/Forth/call-an-object-method-2.fth
Normal file
29
Task/Call-an-object-method/Forth/call-an-object-method-2.fth
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
include FMS-SI.f
|
||||
|
||||
:class animal
|
||||
variable cnt 0 cnt ! \ static instance variable
|
||||
:m init: 1 cnt +! ;m
|
||||
:m cnt: cnt @ . ;m
|
||||
;class
|
||||
|
||||
:class cat <super animal
|
||||
:m speak ." meow" ;m
|
||||
;class
|
||||
|
||||
:class dog <super animal
|
||||
:m speak ." woof" ;m
|
||||
;class
|
||||
|
||||
cat Frisky \ instantiate a cat object named Frisky
|
||||
dog Sparky \ instantiate a dog object named Sparky
|
||||
|
||||
\ The class method cnt: will return the number of animals instantiated
|
||||
\ regardless of which animal object is used.
|
||||
|
||||
\ The instance method speak will respond differently depending
|
||||
\ on the class of the instance object.
|
||||
|
||||
Frisky cnt: \ => 2 ok
|
||||
Sparky cnt: \ => 2 ok
|
||||
Frisky speak \ => meow ok
|
||||
Sparky speak \ => woof ok
|
||||
14
Task/Call-an-object-method/Fortran/call-an-object-method.f
Normal file
14
Task/Call-an-object-method/Fortran/call-an-object-method.f
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
! type declaration
|
||||
type my_type
|
||||
contains
|
||||
procedure, pass :: method1
|
||||
procedure, pass, pointer :: method2
|
||||
end type my_type
|
||||
|
||||
! declare object of type my_type
|
||||
type(my_type) :: mytype_object
|
||||
|
||||
!static call
|
||||
call mytype_object%method1() ! call method1 defined as subroutine
|
||||
!instance?
|
||||
mytype_object%method2() ! call method2 defined as function
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type MyType
|
||||
Public:
|
||||
Declare Sub InstanceMethod(s As String)
|
||||
Declare Static Sub StaticMethod(s As String)
|
||||
Private:
|
||||
dummy_ As Integer ' types cannot be empty in FB
|
||||
End Type
|
||||
|
||||
Sub MyType.InstanceMethod(s As String)
|
||||
Print s
|
||||
End Sub
|
||||
|
||||
Static Sub MyType.StaticMethod(s As String)
|
||||
Print s
|
||||
End Sub
|
||||
|
||||
Dim t As MyType
|
||||
t.InstanceMethod("Hello world!")
|
||||
MyType.Staticmethod("Hello static world!")
|
||||
Print
|
||||
Print "Press any key to quit the program"
|
||||
Sleep
|
||||
29
Task/Call-an-object-method/Go/call-an-object-method-1.go
Normal file
29
Task/Call-an-object-method/Go/call-an-object-method-1.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
type Foo int // some custom type
|
||||
|
||||
// method on the type itself; can be called on that type or its pointer
|
||||
func (self Foo) ValueMethod(x int) { }
|
||||
|
||||
// method on the pointer to the type; can be called on pointers
|
||||
func (self *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)
|
||||
29
Task/Call-an-object-method/Go/call-an-object-method-2.go
Normal file
29
Task/Call-an-object-method/Go/call-an-object-method-2.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package box
|
||||
|
||||
import "sync/atomic"
|
||||
|
||||
var sn uint32
|
||||
|
||||
type box struct {
|
||||
Contents string
|
||||
secret uint32
|
||||
}
|
||||
|
||||
func New() (b *box) {
|
||||
b = &box{secret: atomic.AddUint32(&sn, 1)}
|
||||
switch sn {
|
||||
case 1:
|
||||
b.Contents = "rabbit"
|
||||
case 2:
|
||||
b.Contents = "rock"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (b *box) TellSecret() uint32 {
|
||||
return b.secret
|
||||
}
|
||||
|
||||
func Count() uint32 {
|
||||
return atomic.LoadUint32(&sn)
|
||||
}
|
||||
16
Task/Call-an-object-method/Go/call-an-object-method-3.go
Normal file
16
Task/Call-an-object-method/Go/call-an-object-method-3.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package main
|
||||
|
||||
import "box"
|
||||
|
||||
func main() {
|
||||
// Call constructor. Technically it's just an exported function,
|
||||
// but it's a Go idiom to naming a function New that serves the purpose
|
||||
// of a constructor.
|
||||
b := box.New()
|
||||
|
||||
// Call instance method. In Go terms, simply a method.
|
||||
b.TellSecret()
|
||||
|
||||
// Call class method. In Go terms, another exported function.
|
||||
box.Count()
|
||||
}
|
||||
23
Task/Call-an-object-method/Go/call-an-object-method-4.go
Normal file
23
Task/Call-an-object-method/Go/call-an-object-method-4.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
procedure main()
|
||||
|
||||
bar := foo() # create instance
|
||||
bar.m2() # call method m2 with self=bar, an implicit first parameter
|
||||
|
||||
foo_m1( , "param1", "param2") # equivalent of static class method, first (self) parameter is null
|
||||
end
|
||||
|
||||
class foo(cp1,cp2)
|
||||
method m1(m1p1,m1p2)
|
||||
local ml1
|
||||
static ms1
|
||||
ml1 := m1p1
|
||||
# do something
|
||||
return
|
||||
end
|
||||
method m2(m2p1)
|
||||
# do something else
|
||||
return
|
||||
end
|
||||
initially
|
||||
L := [cp1]
|
||||
end
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
data Obj = Obj { field :: Int, method :: Int -> Int }
|
||||
|
||||
-- smart constructor
|
||||
mkAdder :: Int -> Obj
|
||||
mkAdder x = Obj x (+x)
|
||||
|
||||
-- adding method from a type class
|
||||
instanse Show Obj where
|
||||
show o = "Obj " ++ show (field o)
|
||||
1
Task/Call-an-object-method/J/call-an-object-method-1.j
Normal file
1
Task/Call-an-object-method/J/call-an-object-method-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
methodName_className_ parameters
|
||||
1
Task/Call-an-object-method/J/call-an-object-method-2.j
Normal file
1
Task/Call-an-object-method/J/call-an-object-method-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
objectReference=:'' conew 'className'
|
||||
1
Task/Call-an-object-method/J/call-an-object-method-3.j
Normal file
1
Task/Call-an-object-method/J/call-an-object-method-3.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
methodName__objectReference parameters
|
||||
1
Task/Call-an-object-method/J/call-an-object-method-4.j
Normal file
1
Task/Call-an-object-method/J/call-an-object-method-4.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
parameters methodName_className_ parameters
|
||||
1
Task/Call-an-object-method/J/call-an-object-method-5.j
Normal file
1
Task/Call-an-object-method/J/call-an-object-method-5.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
parameters methodName__objectReference parameters
|
||||
2
Task/Call-an-object-method/J/call-an-object-method-6.j
Normal file
2
Task/Call-an-object-method/J/call-an-object-method-6.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
classReference=: <'className'
|
||||
methodName__classReference parameters
|
||||
1
Task/Call-an-object-method/J/call-an-object-method-7.j
Normal file
1
Task/Call-an-object-method/J/call-an-object-method-7.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
methodName_123_ parameters
|
||||
|
|
@ -0,0 +1 @@
|
|||
ClassWithStaticMethod.staticMethodName(argument1, argument2);//for methods with no arguments, use empty parentheses
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
ClassWithMethod varName = new ClassWithMethod();
|
||||
varName.methodName(argument1, argument2);
|
||||
//or
|
||||
new ClassWithMethod().methodName(argument1, argument2);
|
||||
|
|
@ -0,0 +1 @@
|
|||
x.y()
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
class MyClass {
|
||||
fun instanceMethod(s: String) = println(s)
|
||||
|
||||
companion object {
|
||||
fun staticMethod(s: String) = println(s)
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val mc = MyClass()
|
||||
mc.instanceMethod("Hello instance world!")
|
||||
MyClass.staticMethod("Hello static world!")
|
||||
}
|
||||
93
Task/Call-an-object-method/LFE/call-an-object-method-1.lfe
Normal file
93
Task/Call-an-object-method/LFE/call-an-object-method-1.lfe
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
(defmodule aquarium
|
||||
(export all))
|
||||
|
||||
(defun fish-class (species)
|
||||
"
|
||||
This is the constructor that will be used most often, only requiring that
|
||||
one pass a 'species' string.
|
||||
|
||||
When the children are not defined, simply use an empty list.
|
||||
"
|
||||
(fish-class species ()))
|
||||
|
||||
(defun fish-class (species children)
|
||||
"
|
||||
This contructor is mostly useful as a way of abstracting out the id
|
||||
generation from the larger constructor. Nothing else uses fish-class/2
|
||||
besides fish-class/1, so it's not strictly necessary.
|
||||
|
||||
When the id isn't know, generate one.
|
||||
"
|
||||
(let* (((binary (id (size 128))) (: crypto rand_bytes 16))
|
||||
(formatted-id (car
|
||||
(: io_lib format
|
||||
'"~32.16.0b" (list id)))))
|
||||
(fish-class species children formatted-id)))
|
||||
|
||||
(defun fish-class (species children id)
|
||||
"
|
||||
This is the constructor used internally, once the children and fish id are
|
||||
known.
|
||||
"
|
||||
(let ((move-verb '"swam"))
|
||||
(lambda (method-name)
|
||||
(case method-name
|
||||
('id
|
||||
(lambda (self) id))
|
||||
('species
|
||||
(lambda (self) species))
|
||||
('children
|
||||
(lambda (self) children))
|
||||
('info
|
||||
(lambda (self)
|
||||
(: io format
|
||||
'"id: ~p~nspecies: ~p~nchildren: ~p~n"
|
||||
(list (get-id self)
|
||||
(get-species self)
|
||||
(get-children self)))))
|
||||
('move
|
||||
(lambda (self distance)
|
||||
(: io format
|
||||
'"The ~s ~s ~p feet!~n"
|
||||
(list species move-verb distance))))
|
||||
('reproduce
|
||||
(lambda (self)
|
||||
(let* ((child (fish-class species))
|
||||
(child-id (get-id child))
|
||||
(children-ids (: lists append
|
||||
(list children (list child-id))))
|
||||
(parent-id (get-id self))
|
||||
(parent (fish-class species children-ids parent-id)))
|
||||
(list parent child))))
|
||||
('children-count
|
||||
(lambda (self)
|
||||
(: erlang length children)))))))
|
||||
|
||||
(defun get-method (object method-name)
|
||||
"
|
||||
This is a generic function, used to call into the given object (class
|
||||
instance).
|
||||
"
|
||||
(funcall object method-name))
|
||||
|
||||
; define object methods
|
||||
(defun get-id (object)
|
||||
(funcall (get-method object 'id) object))
|
||||
|
||||
(defun get-species (object)
|
||||
(funcall (get-method object 'species) object))
|
||||
|
||||
(defun get-info (object)
|
||||
(funcall (get-method object 'info) object))
|
||||
|
||||
(defun move (object distance)
|
||||
(funcall (get-method object 'move) object distance))
|
||||
|
||||
(defun reproduce (object)
|
||||
(funcall (get-method object 'reproduce) object))
|
||||
|
||||
(defun get-children (object)
|
||||
(funcall (get-method object 'children) object))
|
||||
|
||||
(defun get-children-count (object)
|
||||
(funcall (get-method object 'children-count) object))
|
||||
45
Task/Call-an-object-method/LFE/call-an-object-method-2.lfe
Normal file
45
Task/Call-an-object-method/LFE/call-an-object-method-2.lfe
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
; Load the file and create a fish-class instance:
|
||||
|
||||
> (slurp '"object.lfe")
|
||||
#(ok object)
|
||||
> (set mommy-fish (fish-class '"Carp"))
|
||||
#Fun<lfe_eval.10.91765564>
|
||||
|
||||
; Execute some of the basic methods:
|
||||
|
||||
> (get-species mommy-fish)
|
||||
"Carp"
|
||||
> (move mommy-fish 17)
|
||||
The Carp swam 17 feet!
|
||||
ok
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
|
||||
; Now let's look at "modifying" state data (e.g., children counts):
|
||||
|
||||
> (get-children mommy-fish)
|
||||
()
|
||||
> (get-children-count mommy-fish)
|
||||
0
|
||||
> (set (mommy-fish baby-fish-1) (reproduce mommy-fish))
|
||||
(#Fun<lfe_eval.10.91765564> #Fun<lfe_eval.10.91765564>)
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-1)
|
||||
"fdcf35983bb496650e558a82e34c9935"
|
||||
> (get-children-count mommy-fish)
|
||||
1
|
||||
> (set (mommy-fish baby-fish-2) (reproduce mommy-fish))
|
||||
(#Fun<lfe_eval.10.91765564> #Fun<lfe_eval.10.91765564>)
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-2)
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"
|
||||
> (get-children-count mommy-fish)
|
||||
2
|
||||
> (get-info mommy-fish)
|
||||
id: "47eebe91a648f042fc3fb278df663de5"
|
||||
species: "Carp"
|
||||
children: ["fdcf35983bb496650e558a82e34c9935",
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"]
|
||||
ok
|
||||
101
Task/Call-an-object-method/LFE/call-an-object-method-3.lfe
Normal file
101
Task/Call-an-object-method/LFE/call-an-object-method-3.lfe
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
(defmodule object
|
||||
(export all))
|
||||
|
||||
(defun fish-class (species)
|
||||
"
|
||||
This is the constructor that will be used most often, only requiring that
|
||||
one pass a 'species' string.
|
||||
|
||||
When the children are not defined, simply use an empty list.
|
||||
"
|
||||
(fish-class species ()))
|
||||
|
||||
(defun fish-class (species children)
|
||||
"
|
||||
This constructor is useful for two reasons:
|
||||
1) as a way of abstracting out the id generation from the
|
||||
larger constructor, and
|
||||
2) spawning the 'object loop' code (fish-class/3).
|
||||
"
|
||||
(let* (((binary (id (size 128))) (: crypto rand_bytes 16))
|
||||
(formatted-id (car
|
||||
(: io_lib format
|
||||
'"~32.16.0b" (list id)))))
|
||||
(spawn 'object
|
||||
'fish-class
|
||||
(list species children formatted-id))))
|
||||
|
||||
(defun fish-class (species children id)
|
||||
"
|
||||
This function is intended to be spawned as a separate process which is
|
||||
used to track the state of a fish. In particular, fish-class/2 spawns
|
||||
this function (which acts as a loop, pattern matching for messages).
|
||||
"
|
||||
(let ((move-verb '"swam"))
|
||||
(receive
|
||||
((tuple caller 'move distance)
|
||||
(! caller (list species move-verb distance))
|
||||
(fish-class species children id))
|
||||
((tuple caller 'species)
|
||||
(! caller species)
|
||||
(fish-class species children id))
|
||||
((tuple caller 'children)
|
||||
(! caller children)
|
||||
(fish-class species children id))
|
||||
((tuple caller 'children-count)
|
||||
(! caller (length children))
|
||||
(fish-class species children id))
|
||||
((tuple caller 'id)
|
||||
(! caller id)
|
||||
(fish-class species children id))
|
||||
((tuple caller 'info)
|
||||
(! caller (list id species children))
|
||||
(fish-class species children id))
|
||||
((tuple caller 'reproduce)
|
||||
(let* ((child (fish-class species))
|
||||
(child-id (get-id child))
|
||||
(children-ids (: lists append
|
||||
(list children (list child-id)))))
|
||||
(! caller child)
|
||||
(fish-class species children-ids id))))))
|
||||
|
||||
(defun call-method (object method-name)
|
||||
"
|
||||
This is a generic function, used to call into the given object (class
|
||||
instance).
|
||||
"
|
||||
(! object (tuple (self) method-name))
|
||||
(receive
|
||||
(data data)))
|
||||
|
||||
(defun call-method (object method-name arg)
|
||||
"
|
||||
Same as above, but with an additional argument.
|
||||
"
|
||||
(! object (tuple (self) method-name arg))
|
||||
(receive
|
||||
(data data)))
|
||||
|
||||
; define object methods
|
||||
(defun get-id (object)
|
||||
(call-method object 'id))
|
||||
|
||||
(defun get-species (object)
|
||||
(call-method object 'species))
|
||||
|
||||
(defun get-info (object)
|
||||
(let ((data (call-method object 'info)))
|
||||
(: io format '"id: ~s~nspecies: ~s~nchildren: ~p~n" data)))
|
||||
|
||||
(defun move (object distance)
|
||||
(let ((data (call-method object 'move distance)))
|
||||
(: io format '"The ~s ~s ~p feet!~n" data)))
|
||||
|
||||
(defun reproduce (object)
|
||||
(call-method object 'reproduce))
|
||||
|
||||
(defun get-children (object)
|
||||
(call-method object 'children))
|
||||
|
||||
(defun get-children-count (object)
|
||||
(call-method object 'children-count))
|
||||
45
Task/Call-an-object-method/LFE/call-an-object-method-4.lfe
Normal file
45
Task/Call-an-object-method/LFE/call-an-object-method-4.lfe
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
; Load the file and create a fish-class instance:
|
||||
|
||||
> (slurp '"object.lfe")
|
||||
#(ok object)
|
||||
> (set mommy-fish (fish-class '"Carp"))
|
||||
<0.33.0>
|
||||
|
||||
; Execute some of the basic methods:
|
||||
|
||||
> (get-species mommy-fish)
|
||||
"Carp"
|
||||
> (move mommy-fish 17)
|
||||
The Carp swam 17 feet!
|
||||
ok
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
|
||||
; Now let's look at modifying state data:
|
||||
|
||||
> (get-children mommy-fish)
|
||||
()
|
||||
> (get-children-count mommy-fish)
|
||||
0
|
||||
> (set baby-fish-1 (reproduce mommy-fish))
|
||||
<0.34.0>
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-1)
|
||||
"fdcf35983bb496650e558a82e34c9935"
|
||||
> (get-children-count mommy-fish)
|
||||
1
|
||||
> (set baby-fish-2 (reproduce mommy-fish))
|
||||
<0.35.0>
|
||||
> (get-id mommy-fish)
|
||||
"47eebe91a648f042fc3fb278df663de5"
|
||||
> (get-id baby-fish-2)
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"
|
||||
> (get-children-count mommy-fish)
|
||||
2
|
||||
> (get-info mommy-fish)
|
||||
id: 47eebe91a648f042fc3fb278df663de5
|
||||
species: Carp
|
||||
children: ["fdcf35983bb496650e558a82e34c9935",
|
||||
"3e64e5c20fb742dd88dac1032749c2fd"]
|
||||
ok
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
myObject someMethod (arg1, arg2, arg3).
|
||||
MyClass someMethod (arg1, arg2, arg3).
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
myObject someMethod "string constant argument".
|
||||
myObject someMethod (argument). ;; Parentheses are necessary here
|
||||
myObject someMethod. ;; No arguments
|
||||
|
|
@ -0,0 +1 @@
|
|||
myObject someMethod: arg1, arg2, arg3.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
-- call static method
|
||||
script("MyClass").foo()
|
||||
|
||||
-- call instance method
|
||||
obj = script("MyClass").new()
|
||||
obj.foo()
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
% avoid infinite metaclass regression by
|
||||
% making the metaclass an instance of itself
|
||||
:- object(metaclass,
|
||||
instantiates(metaclass)).
|
||||
|
||||
:- public(me/1).
|
||||
me(Me) :-
|
||||
self(Me).
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
:- object(class,
|
||||
instantiates(metaclass)).
|
||||
|
||||
:- public(my_class/1).
|
||||
my_class(Class) :-
|
||||
self(Self),
|
||||
instantiates_class(Self, Class).
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
:- object(instance,
|
||||
instantiates(class)).
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
| ?- class::me(Me).
|
||||
Me = class
|
||||
yes
|
||||
|
||||
| ?- instance::my_class(Class).
|
||||
Class = class
|
||||
yes
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
local object = { name = "foo", func = function (self) print(self.name) end }
|
||||
|
||||
object:func() -- with : sugar
|
||||
object.func(object) -- without : sugar
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
local methods = { }
|
||||
function methods:func () -- if a function is declared using :, it is given an implicit 'self' parameter
|
||||
print(self.name)
|
||||
end
|
||||
|
||||
local object = setmetatable({ name = "foo" }, { __index = methods })
|
||||
|
||||
object:func() -- with : sugar
|
||||
methods.func(object) -- without : sugar
|
||||
16
Task/Call-an-object-method/Lua/call-an-object-method-3.lua
Normal file
16
Task/Call-an-object-method/Lua/call-an-object-method-3.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
local count = 0
|
||||
local box = { }
|
||||
local boxmt = { __index = box }
|
||||
function box:tellSecret ()
|
||||
return self.secret
|
||||
end
|
||||
|
||||
local M = { }
|
||||
function M.new ()
|
||||
count = count + 1
|
||||
return setmetatable({ secret = count, contents = count % 2 == 0 and "rabbit" or "rock" }, boxmt)
|
||||
end
|
||||
function M.count ()
|
||||
return count
|
||||
end
|
||||
return M
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
local box = require 'box'
|
||||
|
||||
local b = box.new()
|
||||
|
||||
print(b:tellSecret())
|
||||
print(box.count())
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
Module CheckIt {
|
||||
\\ A class definition is a function which return a Group
|
||||
\\ We can make groups and we can alter them using Group statement
|
||||
\\ Groups may have other groups inside
|
||||
|
||||
Group Alfa {
|
||||
Private:
|
||||
myvalue=100
|
||||
Public:
|
||||
Group SetValue {
|
||||
Set (x) {
|
||||
Link parent myvalue to m
|
||||
m<=x
|
||||
}
|
||||
}
|
||||
Module MyMethod {
|
||||
Read x
|
||||
Print x*.myvalue
|
||||
}
|
||||
}
|
||||
|
||||
Alfa.MyMethod 5 '500
|
||||
Alfa.MyMethod %x=200 ' 20000
|
||||
\\ we can copy Alfa to Z
|
||||
Z=Alfa
|
||||
Z.MyMethod 5
|
||||
Z.SetValue=300
|
||||
Z.MyMethod 5 ' 1500
|
||||
Alfa.MyMethod 5 ' 500
|
||||
Dim A(10)
|
||||
A(3)=Z
|
||||
A(3).MyMethod 5 '1500
|
||||
A(3).SetValue=200
|
||||
A(3).MyMethod 5 '1000
|
||||
\\ get a pointer of group in A(3)
|
||||
k->A(3)
|
||||
k=>SetValue=100
|
||||
A(3).MyMethod 5 '500
|
||||
\\ k get pointer to Alfa
|
||||
k->Alfa
|
||||
k=>SetValue=500
|
||||
Alfa.MyMethod 5 '2500
|
||||
k->Z
|
||||
k=>MyMethod 5 ' 1500
|
||||
Z.SetValue=100
|
||||
k=>MyMethod 5 ' 500
|
||||
}
|
||||
Checkit
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
# Static
|
||||
Method( obj, other, arg );
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
# Instance
|
||||
Method( obj, other, arg );
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
Dog = {}
|
||||
Dog.name = ""
|
||||
Dog.help = function()
|
||||
print "This class represents dogs."
|
||||
end function
|
||||
Dog.speak = function()
|
||||
print self.name + " says Woof!"
|
||||
end function
|
||||
|
||||
fido = new Dog
|
||||
fido.name = "Fido"
|
||||
|
||||
Dog.help // calling a "class method"
|
||||
fido.speak // calling an "instance method"
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
class MyClass
|
||||
declare static id = 5
|
||||
declare MyName
|
||||
|
||||
// constructor
|
||||
def MyClass(MyName)
|
||||
this.MyName = MyName
|
||||
end
|
||||
|
||||
// class method
|
||||
def getName()
|
||||
return this.MyName
|
||||
end
|
||||
|
||||
// static method
|
||||
def static getID()
|
||||
return id
|
||||
end
|
||||
end
|
||||
|
||||
// call the static method
|
||||
println MyClass.getID()
|
||||
|
||||
// instantiate a new MyClass object with the name "test"
|
||||
// and call the class method
|
||||
myclass = new(MyClass, "test")
|
||||
println myclass.getName()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Static
|
||||
MyClass.Method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.Method(someParameter);
|
||||
|
|
@ -0,0 +1 @@
|
|||
SomeClass.staticMethod()
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
objectInstance = SomeClass() -- create a new instance of the class
|
||||
objectInstance.instanceMethod() -- call the instance method
|
||||
|
||||
SomeClass().instanceMethod() -- same as above; create a new instance of the class and call the instance method immediately
|
||||
3
Task/Call-an-object-method/Nim/call-an-object-method.nim
Normal file
3
Task/Call-an-object-method/Nim/call-an-object-method.nim
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
var x = @[1, 2, 3]
|
||||
add(x, 4)
|
||||
x.add(5)
|
||||
|
|
@ -0,0 +1 @@
|
|||
+&GO
|
||||
|
|
@ -0,0 +1 @@
|
|||
my_obj#my_meth params
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
ClassName->some_function(); # call class function
|
||||
instance->some_method(); # call instance method
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
// Static (known in Pascal as class method)
|
||||
MyClass.method(someParameter);
|
||||
|
||||
// Instance
|
||||
myInstance.method(someParameter);
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
// 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];
|
||||
|
|
@ -0,0 +1 @@
|
|||
1.2 sqrt
|
||||
|
|
@ -0,0 +1 @@
|
|||
Date now
|
||||
30
Task/Call-an-object-method/OoRexx/call-an-object-method.rexx
Normal file
30
Task/Call-an-object-method/OoRexx/call-an-object-method.rexx
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
say "pi:" .circle~pi
|
||||
c=.circle~new(1)
|
||||
say "c~area:" c~area
|
||||
Do r=2 To 10
|
||||
c.r=.circle~new(r)
|
||||
End
|
||||
say .circle~instances('') 'circles were created'
|
||||
|
||||
::class circle
|
||||
::method pi class -- a class method
|
||||
return 3.14159265358979323
|
||||
|
||||
::method instances class -- another class method
|
||||
expose in
|
||||
use arg a
|
||||
If datatype(in)<>'NUM' Then in=0
|
||||
If a<>'' Then
|
||||
in+=1
|
||||
Return in
|
||||
|
||||
::method init
|
||||
expose radius
|
||||
use arg radius
|
||||
self~class~instances('x')
|
||||
|
||||
::method area -- an instance method
|
||||
expose radius
|
||||
Say self~class
|
||||
Say self
|
||||
return self~class~pi * radius * radius
|
||||
9
Task/Call-an-object-method/PHP/call-an-object-method.php
Normal file
9
Task/Call-an-object-method/PHP/call-an-object-method.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// 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);
|
||||
27
Task/Call-an-object-method/PL-SQL/call-an-object-method.sql
Normal file
27
Task/Call-an-object-method/PL-SQL/call-an-object-method.sql
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
create or replace TYPE myClass AS OBJECT (
|
||||
-- A class needs at least one member even though we don't use it
|
||||
dummy NUMBER,
|
||||
STATIC FUNCTION static_method RETURN VARCHAR2,
|
||||
MEMBER FUNCTION instance_method RETURN VARCHAR2
|
||||
);
|
||||
/
|
||||
CREATE OR REPLACE TYPE BODY myClass AS
|
||||
STATIC FUNCTION static_method RETURN VARCHAR2 IS
|
||||
BEGIN
|
||||
RETURN 'Called myClass.static_method';
|
||||
END static_method;
|
||||
|
||||
MEMBER FUNCTION instance_method RETURN VARCHAR2 IS
|
||||
BEGIN
|
||||
RETURN 'Called myClass.instance_method';
|
||||
END instance_method;
|
||||
END;
|
||||
/
|
||||
|
||||
DECLARE
|
||||
myInstance myClass;
|
||||
BEGIN
|
||||
myInstance := myClass(null);
|
||||
DBMS_OUTPUT.put_line( myClass.static_method() );
|
||||
DBMS_OUTPUT.put_line( myInstance.instance_method() );
|
||||
END;/
|
||||
17
Task/Call-an-object-method/Perl/call-an-object-method.pl
Normal file
17
Task/Call-an-object-method/Perl/call-an-object-method.pl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# Class method
|
||||
MyClass->classMethod($someParameter);
|
||||
# Equivalently using a class name
|
||||
my $foo = 'MyClass';
|
||||
$foo->classMethod($someParameter);
|
||||
|
||||
|
||||
# Instance method
|
||||
$myInstance->method($someParameter);
|
||||
|
||||
# Calling a method with no parameters
|
||||
$myInstance->anotherMethod;
|
||||
|
||||
# Class and instance method calls are made behind the scenes by getting the function from
|
||||
# the package and calling it on the class name or object reference explicitly
|
||||
MyClass::classMethod('MyClass', $someParameter);
|
||||
MyClass::method($myInstance, $someParameter);
|
||||
13
Task/Call-an-object-method/Phix/call-an-object-method.phix
Normal file
13
Task/Call-an-object-method/Phix/call-an-object-method.phix
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(notonline)-->
|
||||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no class in p2js)</span>
|
||||
<span style="color: #008080;">class</span> <span style="color: #000000;">test</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">msg</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this is a test"</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">()</span> <span style="color: #0000FF;">?</span><span style="color: #7060A8;">this</span><span style="color: #0000FF;">.</span><span style="color: #000000;">msg</span> <span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">inst</span><span style="color: #0000FF;">()</span> <span style="color: #0000FF;">?</span><span style="color: #008000;">"this is dynamic"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">class</span>
|
||||
<span style="color: #000000;">test</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">show</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- prints "this is a test"</span>
|
||||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">inst</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- prints "this is dynamic"</span>
|
||||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">inst</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">show</span>
|
||||
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">inst</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- prints "this is a test"</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(foo> MyClass)
|
||||
(foo> MyObject)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
obj->method();
|
||||
obj["method"]();
|
||||
call_function(obj->method);
|
||||
call_function(obj["method"]);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
function func = obj->method;
|
||||
func();
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
module.func();
|
||||
module["func"]();
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$Date = Get-Date
|
||||
$Date.AddDays( 1 )
|
||||
[System.Math]::Sqrt( 2 )
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
// define a rudimentary class
|
||||
class HelloWorld
|
||||
{
|
||||
public static void sayHello()
|
||||
{
|
||||
println("Hello, world!");
|
||||
}
|
||||
public void sayGoodbye()
|
||||
{
|
||||
println("Goodbye, cruel world!");
|
||||
}
|
||||
}
|
||||
|
||||
// call the class method
|
||||
HelloWorld.sayHello();
|
||||
|
||||
// create an instance of the class
|
||||
HelloWorld hello = new HelloWorld();
|
||||
|
||||
// and call the instance method
|
||||
hello.sayGoodbye();
|
||||
24
Task/Call-an-object-method/Python/call-an-object-method.py
Normal file
24
Task/Call-an-object-method/Python/call-an-object-method.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class MyClass(object):
|
||||
@classmethod
|
||||
def myClassMethod(self, x):
|
||||
pass
|
||||
@staticmethod
|
||||
def myStaticMethod(x):
|
||||
pass
|
||||
def myMethod(self, x):
|
||||
return 42 + x
|
||||
|
||||
myInstance = MyClass()
|
||||
|
||||
# Instance method
|
||||
myInstance.myMethod(someParameter)
|
||||
# A method can also be retrieved as an attribute from the class, and then explicitly called on an instance:
|
||||
MyClass.myMethod(myInstance, someParameter)
|
||||
|
||||
|
||||
# Class or static methods
|
||||
MyClass.myClassMethod(someParameter)
|
||||
MyClass.myStaticMethod(someParameter)
|
||||
# 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)
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
( ---------------- zen object orientation -------------- )
|
||||
|
||||
[ immovable
|
||||
]this[ swap do ]done[ ] is object ( --> )
|
||||
|
||||
[ ]'[ ] is method ( --> [ )
|
||||
|
||||
[ method
|
||||
[ dup share
|
||||
swap put ] ] is localise ( --> [ )
|
||||
|
||||
[ method [ release ] ] is delocalise ( --> [ )
|
||||
|
||||
|
||||
( -------------- example: counter methods -------------- )
|
||||
|
||||
( to create a counter object, use:
|
||||
"[ object 0 ] is 'name' ( [ --> )" )
|
||||
|
||||
[ method
|
||||
[ 0 swap replace ] ] is reset-counter ( --> [ )
|
||||
|
||||
[ method
|
||||
[ 1 swap tally ] ] is increment-counter ( --> [ )
|
||||
|
||||
[ method [ share ] ] is report-counter ( --> [ )
|
||||
|
||||
|
||||
( -------------------- demonstration ------------------- )
|
||||
|
||||
say 'Creating counter object: "mycounter".' cr cr
|
||||
[ object 0 ] is mycounter ( [ --> )
|
||||
|
||||
say "Initial value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
||||
say "Incrementing mycounter three times." cr
|
||||
3 times [ increment-counter mycounter ]
|
||||
|
||||
say "Current value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
||||
say "Localising mycounter." cr cr
|
||||
localise mycounter
|
||||
|
||||
say " Current value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
||||
say " Resetting mycounter." cr
|
||||
reset-counter mycounter
|
||||
|
||||
say " Current value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
||||
say " Incrementing mycounter six times." cr
|
||||
6 times [ increment-counter mycounter ]
|
||||
|
||||
say " Current value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
||||
say "Delocalising mycounter." cr cr
|
||||
delocalise mycounter
|
||||
|
||||
say "Current value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
||||
say "Resetting mycounter." cr
|
||||
reset-counter mycounter
|
||||
|
||||
say "Current value of mycounter: "
|
||||
report-counter mycounter echo cr cr
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
#lang racket/gui
|
||||
|
||||
(define timer (new timer%))
|
||||
(send timer start 100)
|
||||
26
Task/Call-an-object-method/Raku/call-an-object-method-1.raku
Normal file
26
Task/Call-an-object-method/Raku/call-an-object-method-1.raku
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
class Thing {
|
||||
method regular-example() { say 'I haz a method' }
|
||||
|
||||
multi method multi-example() { say 'No arguments given' }
|
||||
multi method multi-example(Str $foo) { say 'String given' }
|
||||
multi method multi-example(Int $foo) { say 'Integer given' }
|
||||
};
|
||||
|
||||
# 'new' is actually a method, not a special keyword:
|
||||
my $thing = Thing.new;
|
||||
|
||||
# No arguments: parentheses are optional
|
||||
$thing.regular-example;
|
||||
$thing.regular-example();
|
||||
$thing.multi-example;
|
||||
$thing.multi-example();
|
||||
|
||||
# Arguments: parentheses or colon required
|
||||
$thing.multi-example("This is a string");
|
||||
$thing.multi-example: "This is a string";
|
||||
$thing.multi-example(42);
|
||||
$thing.multi-example: 42;
|
||||
|
||||
# Indirect (reverse order) method call syntax: colon required
|
||||
my $foo = new Thing: ;
|
||||
multi-example $thing: 42;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
my @array = <a z c d y>;
|
||||
@array .= sort; # short for @array = @array.sort;
|
||||
|
||||
say @array».uc; # uppercase all the strings: A C D Y Z
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
my $object = "a string"; # Everything is an object.
|
||||
my method example-method {
|
||||
return "This is { self }.";
|
||||
}
|
||||
|
||||
say $object.&example-method; # Outputs "This is a string."
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
new point { print() }
|
||||
Class Point
|
||||
x = 10 y = 20 z = 30
|
||||
func print see x + nl + y + nl + z + nl
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue