1
0
mirror of https://github.com/MADTeacher/go_basics.git synced 2025-11-23 21:34:47 +02:00

отредактирована 3 глава

This commit is contained in:
Stanislav Chernyshev
2025-06-04 18:38:52 +03:00
parent 70656cfad8
commit f97178b439
64 changed files with 1751 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
module golang/factory
go 1.24

View 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())
}

View 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,
}
}

View 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
}