From a890aca01670deeed15ed7cbfc8cc71c209c253d Mon Sep 17 00:00:00 2001 From: Maxthrew Date: Tue, 26 Dec 2023 16:00:17 +0300 Subject: [PATCH] laba_5 --- golang/internal/cat/cat.go | 53 ++++++++++++++++++++++++++++++++++++++ golang/main.go | 19 ++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 golang/internal/cat/cat.go diff --git a/golang/internal/cat/cat.go b/golang/internal/cat/cat.go new file mode 100644 index 00000000..378c618d --- /dev/null +++ b/golang/internal/cat/cat.go @@ -0,0 +1,53 @@ +package cat + +import ( + "fmt" +) + +type cat struct { + name string + age uint8 + color string +} + +func NewCat(name_cat string, age_cat uint8, color string) (cat, error) { + var c cat = cat{ + name: name_cat, + } + c.SetColor(color) + var err = c.SetAge(age_cat) + return c, err +} + +func (c *cat) Meow() { + fmt.Printf("%s мяукнул", c.GetName()) +} + +func (c *cat) SetName(name string) { + c.name = name +} + +func (c *cat) SetColor(color string) { + c.color = color +} + +func (c *cat) SetAge(age uint8) error { + if age <= 50 { + c.age = age + return nil + } else { + return fmt.Errorf("Кошки не живут больше 50 лет!") + } +} + +func (c *cat) GetName() string { + return c.name +} + +func (c *cat) GetAge() uint8 { + return c.age +} + +func (c *cat) GetColor() string { + return c.color +} diff --git a/golang/main.go b/golang/main.go index 234a526d..c40b4fb0 100644 --- a/golang/main.go +++ b/golang/main.go @@ -4,10 +4,29 @@ import ( "fmt" "isuct.ru/informatics2022/internal" + "isuct.ru/informatics2022/internal/cat" ) func main() { fmt.Println("Алексеев Максим Алексеевич | Вариант 1") fmt.Println(internal.Zadacha_A(1.28, 3.28, 0.4)) fmt.Println(internal.Zadacha_B([]float64{1.1, 2.4, 3.6, 1.7, 3.9})) + + cat1, err := cat.NewCat("Барсик", 10, "black") + if err != nil { + fmt.Printf("%v", err) + } + cat1.Meow() + fmt.Printf("\n%v\n", cat1.GetColor()) + cat1.SetColor("white") + fmt.Printf("%v\n", cat1.GetColor()) + fmt.Printf("%v\n", cat1.GetAge()) + cat1.SetAge(30) + fmt.Printf("%v\n", cat1.GetAge()) + cat2, err := cat.NewCat("Кузя", 77, "orange") + if err != nil { + fmt.Printf("%v\n", err) + } + fmt.Println(cat2.GetName()) + fmt.Println(cat2.GetAge()) }