mirror of
https://github.com/MADTeacher/go_basics.git
synced 2025-11-23 21:34:47 +02:00
отредактирована 3 глава
This commit is contained in:
3
part_3/3.6/golang/factory/go.mod
Normal file
3
part_3/3.6/golang/factory/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module golang/factory
|
||||
|
||||
go 1.24
|
||||
24
part_3/3.6/golang/factory/main.go
Normal file
24
part_3/3.6/golang/factory/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang/factory/shape"
|
||||
)
|
||||
|
||||
func main() {
|
||||
rectangle := shape.NewRectangle(10, 6, shape.Point{X: 6, Y: -8}, "Gold")
|
||||
|
||||
fmt.Printf("%+v\n", *rectangle)
|
||||
fmt.Printf("%+v\n", rectangle.GetName())
|
||||
fmt.Printf("Perimeter = %+v\n", rectangle.GetPerimeter())
|
||||
fmt.Printf("Area = %+v\n", rectangle.GetArea())
|
||||
|
||||
// меняем значения полей
|
||||
rectangle.SetLength(15)
|
||||
rectangle.SetWidth(7)
|
||||
rectangle.SetColor("Green")
|
||||
|
||||
fmt.Printf("%+v\n", *rectangle)
|
||||
fmt.Printf("New perimeter = %+v\n", rectangle.GetPerimeter())
|
||||
fmt.Printf("New Area = %+v\n", rectangle.GetArea())
|
||||
}
|
||||
47
part_3/3.6/golang/factory/shape/rectangle.go
Normal file
47
part_3/3.6/golang/factory/shape/rectangle.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package shape
|
||||
|
||||
type Rectangle struct {
|
||||
shape // композиция
|
||||
width uint
|
||||
length uint
|
||||
}
|
||||
|
||||
func (r *Rectangle) GetWidth() uint {
|
||||
return r.width
|
||||
}
|
||||
|
||||
func (r *Rectangle) GetLength() uint {
|
||||
return r.length
|
||||
}
|
||||
|
||||
func (r *Rectangle) SetWidth(width uint) {
|
||||
r.width = width
|
||||
}
|
||||
|
||||
func (r *Rectangle) SetLength(length uint) {
|
||||
r.length = length
|
||||
}
|
||||
|
||||
func (r *Rectangle) GetPerimeter() uint {
|
||||
return (r.length + r.width) * 2
|
||||
}
|
||||
|
||||
func (r *Rectangle) GetArea() uint {
|
||||
return r.length * r.width
|
||||
}
|
||||
|
||||
func (r *Rectangle) GetName() string {
|
||||
return "Super-puper " + r.shape.GetName()
|
||||
}
|
||||
|
||||
func NewRectangle(width, length uint, center Point, color string) *Rectangle {
|
||||
return &Rectangle{
|
||||
shape: shape{
|
||||
name: "Rectangle",
|
||||
color: color,
|
||||
center: center,
|
||||
},
|
||||
width: width,
|
||||
length: length,
|
||||
}
|
||||
}
|
||||
32
part_3/3.6/golang/factory/shape/shape.go
Normal file
32
part_3/3.6/golang/factory/shape/shape.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package shape
|
||||
|
||||
type Point struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
type shape struct {
|
||||
name string
|
||||
center Point
|
||||
color string
|
||||
}
|
||||
|
||||
func (s *shape) SetColor(color string) {
|
||||
s.color = color
|
||||
}
|
||||
|
||||
func (s *shape) GetColor() string {
|
||||
return s.color
|
||||
}
|
||||
|
||||
func (s *shape) GetName() string {
|
||||
return s.name
|
||||
}
|
||||
|
||||
func (s *shape) MoveCenter(point Point) {
|
||||
s.center = point
|
||||
}
|
||||
|
||||
func (s *shape) GetCenter() Point {
|
||||
return s.center
|
||||
}
|
||||
Reference in New Issue
Block a user