This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View 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.

View file

@ -0,0 +1,5 @@
---
category:
- Object oriented
- Encyclopedia
note: Basic language learning

View file

@ -0,0 +1,5 @@
// Static
MyClass.method(someParameter);
// Instance
myInstance.method(someParameter);

View file

@ -0,0 +1,10 @@
class myClass
{
Method(someParameter){
MsgBox % SomeParameter
}
}
myClass.method("hi")
myInstance := new myClass
myInstance.Method("bye")

View file

@ -0,0 +1,5 @@
// Static
MyClass::method(someParameter);
// Instance
myInstance.method(someParameter);

View 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!");
}

View file

@ -0,0 +1 @@
someObject.someMethod(someParameter)

View 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

View 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)

View 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)
}

View 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()
}

View file

@ -0,0 +1,23 @@
procedure main()
foo_m1() # equivalent of class method, not normally used
bar := foo() # create instance
bar.m2() # call method m2 with self=bar
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

View file

@ -0,0 +1 @@
ClassWithStaticMethod.staticMethodName(argument1, argument2);//for methods with no arguments, use empty parentheses

View file

@ -0,0 +1,4 @@
ClassWithMethod varName = new ClassWithMethod();
varName.methodName(argument1, argument2);
//or
new ClassWithMethod().methodName(argument1, argument2);

View 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);

View 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);

View file

@ -0,0 +1,2 @@
(foo> MyClass)
(foo> MyObject)

View 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)

View file

@ -0,0 +1,4 @@
#lang racket/gui
(define timer (new timer%))
(send timer start 100)

View file

@ -0,0 +1,16 @@
# Class method
MyClass.some_method(some_parameter)
# Class may be computed at runtime
foo = MyClass
foo.some_method(some_parameter)
# Instance method
my_instance.method(some_parameter)
# The parentheses are optional
my_instance.method some_parameter
# Calling a method with no parameters
my_instance.another_method

View file

@ -0,0 +1,17 @@
" Class "
MyClass selector: someArgument .
" or equivalently "
foo := MyClass .
foo selector: someArgument.
" Instance "
myInstance selector: someArgument.
" Message with multiple arguments "
myInstance fooWithRed:arg1 green:arg2 blue:arg3 .
" Message with no arguments "
myInstance selector.
" Binary (operator) message"
myInstance + argument .

View file

@ -0,0 +1 @@
theCar := (someCondition ifTrue:[ Ford ] ifFalse: [ Jaguar ]) new.

View file

@ -0,0 +1,2 @@
whichMessage := #( #'red' #'green' #'blue') at: computedIndex.
foo perform: whichMessage

View file

@ -0,0 +1,2 @@
theMessage := ('handleFileType' , suffix) asSymbol.
foo perform: theMessage.

View file

@ -0,0 +1,5 @@
[
foo perform: theMessage
] on: MessageNotUnderstood do:[
Dialog information: 'sorry'
]

View file

@ -0,0 +1,6 @@
package require Tcl 8.6
# "Static" (on class object)
MyClass mthd someParameter
# Instance
$myInstance mthd someParameter