| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package main
- import (
- "fmt"
- "math"
- )
- type Circle struct {
- x, y, r float64
- }
- type Rectangle struct {
- x1, y1, x2, y2 float64
- }
- func (c Circle) area() float64 {
- return math.Pi * c.r * c.r
- }
- func (r Rectangle) area() float64 {
- l := r.x2 - r.x1
- w := r.y2 - r.y1
- return l * w
- }
- type Person struct {
- Name string
- }
- func (p *Person) Talk() {
- fmt.Println("Hi, my name is", p.Name)
- }
- type Android struct {
- Person
- Model string
- }
- type Character struct {
- On bool
- Ammo int
- Power int
- }
- func (c *Character) Shoot() bool {
- if c.On == false {
- return false
- } else if c.Ammo > 0 {
- c.Ammo--
- return true
- } else {
- return false
- }
- }
- func (c *Character) RideBike() bool {
- if c.On == false {
- return false
- } else if c.Power > 0 {
- c.Power--
- return true
- } else {
- return false
- }
- }
- func main() {
- /*
- rect := Rectangle{x1: 0, y1: 0, x2: 10, y2: 10}
- circ := Circle{x: 0, y: 0, r: 5}
- fmt.Println("Rectangle area:", rect.area())
- fmt.Println("Circle area:", circ.area())
- */
- /*
- a := new(Android)
- a.Name = "Fedor"
- a.Person.Talk()
- a.Talk()
- */
- ch := Character{true, 5, 10}
- fmt.Println(ch.Shoot(), ch.Ammo, ch.Power)
- fmt.Println(ch.Shoot(), ch.Ammo, ch.Power)
- }
|