Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue