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.7/golang/factory/go.mod
Normal file
3
part_3/3.7/golang/factory/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module golang/factory
|
||||
|
||||
go 1.24
|
||||
85
part_3/3.7/golang/factory/main.go
Normal file
85
part_3/3.7/golang/factory/main.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang/factory/shape"
|
||||
)
|
||||
|
||||
func printShapeName(name shape.IShapeName) {
|
||||
fmt.Printf("IShapeName: %v\n", name.GetName())
|
||||
}
|
||||
|
||||
func getIShapeFromRectangle(rectangle *shape.Rectangle) shape.IShape {
|
||||
return rectangle // неявное приведение
|
||||
// return shape.IShape(rectangle) // явное приведение
|
||||
}
|
||||
|
||||
func main() {
|
||||
rectangle := shape.NewRectangle(10, 6, shape.Point{X: 6, Y: -8}, "Gold")
|
||||
|
||||
ishape := shape.IShape(rectangle) // приведение Rectangle к интерфейсу
|
||||
// приведение интерфейса IShape к интерфейсу IShapeArea
|
||||
iShapeArea := shape.IShapeArea(ishape)
|
||||
fmt.Printf("IShapeArea: %+v\n", iShapeArea.GetArea())
|
||||
|
||||
// явное обратное приведение интерфейса IShapePerimeter к интерфейсу IShape невозможно
|
||||
// ishape = shape.IShape(iShapePerimeter) //error:
|
||||
// cannot convert iShapePerimeter (variable of type shape.IShapePerimeter) to shape.IShape
|
||||
// (shape.IShapePerimeter does not implement shape.IShape (missing method GetArea))
|
||||
|
||||
// правильное приведение интерфейса IShapeArea к интерфейсу IShape
|
||||
ishape = iShapeArea.(*shape.Rectangle)
|
||||
// аналогично
|
||||
// newIshape := shape.IShape(iShapeArea.(*shape.Rectangle))
|
||||
fmt.Printf("IShape center: %+v\n", ishape.GetCenter())
|
||||
|
||||
// правильное приведение интерфейса IShapeArea к структуре Rectangle
|
||||
rectangle = iShapeArea.(*shape.Rectangle)
|
||||
fmt.Printf("%s: %+v\n", rectangle.GetName(), *rectangle)
|
||||
}
|
||||
|
||||
// func main() {
|
||||
// rectangle1 := shape.NewRectangle(10, 6, shape.Point{X: 6, Y: -8}, "Gold")
|
||||
// triangle1 := shape.NewTriangle(6, 10, 8, shape.Point{X: -85, Y: 15}, "Green")
|
||||
// rectangle2 := shape.NewRectangle(3, 5, shape.Point{X: 46, Y: -48}, "Yellow")
|
||||
// triangle2 := shape.NewTriangle(4, 8, 6, shape.Point{X: 61, Y: 98}, "Orange")
|
||||
|
||||
// // объявляем и инициализируем срез интерфейсного типа
|
||||
// ishapeSlice := []shape.IShape{
|
||||
// rectangle1,
|
||||
// triangle1,
|
||||
// triangle2,
|
||||
// rectangle2,
|
||||
// }
|
||||
|
||||
// for _, it := range ishapeSlice {
|
||||
// fmt.Printf("Shape center coordinate: %+v\n", it.GetCenter())
|
||||
// newCenter := shape.Point{
|
||||
// X: it.GetCenter().X + 33,
|
||||
// Y: it.GetCenter().Y - 20,
|
||||
// }
|
||||
// it.MoveCenter(newCenter)
|
||||
// fmt.Printf("Shape new center coordinate: %+v\n", it.GetCenter())
|
||||
// fmt.Println()
|
||||
// }
|
||||
// }
|
||||
|
||||
// func main() {
|
||||
// rectangle := shape.NewRectangle(10, 6, shape.Point{X: 6, Y: -8}, "Gold")
|
||||
// triangle := shape.NewTriangle(6, 10, 8, shape.Point{X: -85, Y: 15}, "Green")
|
||||
// printShapeName(rectangle) // неявное приведение
|
||||
// //printShapeName(shape.IShapeName(rectangle)) // явное приведение
|
||||
// printShapeName(triangle)
|
||||
// ishape := getIShapeFromRectangle(rectangle)
|
||||
// fmt.Printf("Shape center: %v\n", ishape.GetCenter())
|
||||
// fmt.Printf("Shape perimeter: %v\n", ishape.GetPerimeter())
|
||||
// }
|
||||
|
||||
// func main() {
|
||||
// rectangle := shape.NewRectangle(10, 6, shape.Point{X: 6, Y: -8}, "Gold")
|
||||
// triangle := shape.NewTriangle(6, 10, 8, shape.Point{X: -85, Y: 15}, "Green")
|
||||
// iShapeName := shape.IShapeName(triangle) // приведение Triangle к интерфейсу
|
||||
// fmt.Printf("IShapeName: %v\n", iShapeName.GetName())
|
||||
// iShapeName = shape.IShapeName(rectangle) // приведение Rectangle к интерфейсу
|
||||
// fmt.Printf("IShapeName: %v\n", iShapeName.GetName())
|
||||
// }
|
||||
20
part_3/3.7/golang/factory/shape/ishape.go
Normal file
20
part_3/3.7/golang/factory/shape/ishape.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package shape
|
||||
|
||||
type IShape interface {
|
||||
GetArea() float64
|
||||
GetPerimeter() uint
|
||||
MoveCenter(point Point)
|
||||
GetCenter() Point
|
||||
}
|
||||
|
||||
type IShapeArea interface {
|
||||
GetArea() float64
|
||||
}
|
||||
|
||||
type IShapePerimeter interface {
|
||||
GetPerimeter() uint
|
||||
}
|
||||
|
||||
type IShapeName interface {
|
||||
GetName() string
|
||||
}
|
||||
47
part_3/3.7/golang/factory/shape/rectangle.go
Normal file
47
part_3/3.7/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() float64 {
|
||||
return float64(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.7/golang/factory/shape/shape.go
Normal file
32
part_3/3.7/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
|
||||
}
|
||||
64
part_3/3.7/golang/factory/shape/triangle.go
Normal file
64
part_3/3.7/golang/factory/shape/triangle.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package shape
|
||||
|
||||
import "math"
|
||||
|
||||
type Triangle struct {
|
||||
shape
|
||||
abLength uint
|
||||
acLength uint
|
||||
bcLength uint
|
||||
}
|
||||
|
||||
func (t *Triangle) GetABLength() uint {
|
||||
return t.abLength
|
||||
}
|
||||
|
||||
func (t *Triangle) GetACLength() uint {
|
||||
return t.acLength
|
||||
}
|
||||
|
||||
func (t *Triangle) GetBCLength() uint {
|
||||
return t.bcLength
|
||||
}
|
||||
|
||||
func (t *Triangle) SetABLength(ab uint) {
|
||||
t.abLength = ab
|
||||
}
|
||||
|
||||
func (t *Triangle) SetACLength(ac uint) {
|
||||
t.abLength = ac
|
||||
}
|
||||
|
||||
func (t *Triangle) SetBCLength(bc uint) {
|
||||
t.abLength = bc
|
||||
}
|
||||
|
||||
func (t *Triangle) GetPerimeter() uint {
|
||||
return t.abLength + t.acLength + t.bcLength
|
||||
}
|
||||
|
||||
func (t *Triangle) GetArea() float64 {
|
||||
// расчет площади треугольника
|
||||
// высота рассчитывается по формуле разностороннего треугольника
|
||||
// через длины всех сторон
|
||||
p := float64(t.GetPerimeter() / 2) // полупериметр
|
||||
numerator := p * (p - float64(t.abLength)) *
|
||||
(p - float64(t.acLength)) *
|
||||
(p - float64(t.acLength))
|
||||
numerator = 2 * math.Sqrt(numerator)
|
||||
height := numerator / 2
|
||||
return float64(t.acLength) / 2 * height
|
||||
}
|
||||
|
||||
func NewTriangle(ab, ac, bc uint, center Point, color string) *Triangle {
|
||||
return &Triangle{
|
||||
shape: shape{
|
||||
name: "Triangle",
|
||||
color: color,
|
||||
center: center,
|
||||
},
|
||||
abLength: ab,
|
||||
bcLength: bc,
|
||||
acLength: ac,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user