mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
fix: coding style and file format for all example.
This commit is contained in:
@@ -1,34 +1,35 @@
|
||||
// mult_inheritance.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Camera struct { }
|
||||
|
||||
func (c *Camera) TakeAPicture() string {
|
||||
return "Click"
|
||||
}
|
||||
|
||||
type Phone struct { }
|
||||
|
||||
func (p *Phone ) Call() string {
|
||||
return "Ring Ring"
|
||||
}
|
||||
|
||||
// multiple inheritance
|
||||
type CameraPhone struct {
|
||||
Camera
|
||||
Phone
|
||||
}
|
||||
|
||||
func main() {
|
||||
cp := new(CameraPhone)
|
||||
fmt.Println("Our new CameraPhone exhibits multiple behaviors ...")
|
||||
fmt.Println("It exhibits behavior of a Camera: ", cp.TakeAPicture())
|
||||
fmt.Println("It works like a Phone too: ", cp.Call())
|
||||
}
|
||||
/* Output:
|
||||
Our new CameraPhone exhibits multiple behaviors ...
|
||||
It exhibits behavior of a Camera: Click
|
||||
It works like a Phone too: Ring Ring
|
||||
*/
|
||||
// mult_inheritance.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Camera struct{}
|
||||
|
||||
func (c *Camera) TakeAPicture() string {
|
||||
return "Click"
|
||||
}
|
||||
|
||||
type Phone struct{}
|
||||
|
||||
func (p *Phone) Call() string {
|
||||
return "Ring Ring"
|
||||
}
|
||||
|
||||
// multiple inheritance
|
||||
type CameraPhone struct {
|
||||
Camera
|
||||
Phone
|
||||
}
|
||||
|
||||
func main() {
|
||||
cp := new(CameraPhone)
|
||||
fmt.Println("Our new CameraPhone exhibits multiple behaviors ...")
|
||||
fmt.Println("It exhibits behavior of a Camera: ", cp.TakeAPicture())
|
||||
fmt.Println("It works like a Phone too: ", cp.Call())
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Our new CameraPhone exhibits multiple behaviors ...
|
||||
It exhibits behavior of a Camera: Click
|
||||
It works like a Phone too: Ring Ring
|
||||
*/
|
||||
|
Reference in New Issue
Block a user