Created
November 12, 2009 09:26
-
-
Save fujiwara/232758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt"; | |
type Speaker interface { | |
Speak() string; | |
} | |
type Person struct { | |
name string; | |
} | |
func NewPerson(name string) (self *Person) { | |
self = new(Person); | |
self.name = name; | |
return; | |
} | |
func (self *Person) Speak() string { | |
return fmt.Sprintf("私の名前は%sです", self.name); | |
} | |
func (self *Person) String() string { | |
return self.name; | |
} | |
type Dog struct { | |
name string; | |
} | |
func NewDog(name string) (self *Dog) { | |
self = new(Dog); | |
self.name = name; | |
return; | |
} | |
func (self *Dog) Speak() string { | |
return "ワンワン!"; | |
} | |
func (self *Dog) String() string { | |
return self.name; | |
} | |
type Fish struct { | |
name string; | |
} | |
func NewFish(name string) (self *Fish) { | |
self = new(Fish); | |
self.name = name; | |
return; | |
} | |
func (self *Fish) String() string { | |
return self.name; | |
} | |
func say( speaker Speaker ) { | |
fmt.Printf("%s は言いました「%s」\n", speaker, speaker.Speak() ); | |
} | |
func main() { | |
alice := NewPerson("Alice"); | |
bob := NewDog("Bob"); | |
// carol := NewFish("Carol"); | |
say(alice); | |
say(bob); | |
// say(carol); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment