-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface-with-type-embedding-AND-method-overriding.go
125 lines (109 loc) · 3.08 KB
/
interface-with-type-embedding-AND-method-overriding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Example of Interface with Type Embedding and Method Overriding on GO language
package main
import (
"fmt"
)
/*
The interface type Information is a contract tof creating
various Product types in a catalog. The information interface
provide three behaviours in its contract: General, Attributes, and Inventory.
*/
type Information interface {
General()
Attributes()
Inventory()
}
/*
A struct Product is declared with fields for holding its state and methods
implemented based on the behaviours defined in the Information interface. */
type Product struct {
Name, Description string
Weight, Price float64
Stock int
}
func (prd Product) General() {
fmt.Printf("\n%s", prd.Name)
fmt.Printf("\n%s\n", prd.Description)
fmt.Println(prd.Price)
}
func (prd Product) Attributes() {
fmt.Println(prd.Weight)
}
// A struct Mobile is declared in which the type Product is embedded
type Mobile struct {
Product
DisplayFeatures []string
ProcessorFeatures []string
}
// Override for the Attributes Method for the Mobile struct
func (mob Mobile) Attributes() {
mob.Product.Attributes()
fmt.Println("\nDisplay Features:")
for _, key := range mob.DisplayFeatures {
fmt.Println(key)
}
fmt.Println("\nProcessor Features:")
for _, key := range mob.ProcessorFeatures {
fmt.Println(key)
}
}
// A struct Shirts is declared in which the type Product is embedded.
type Shirts struct {
Product
Size, Pattern, Sleeve, Fabric string
}
// Overrides for the Attributes Method for the Shirts struct
func (shrt Shirts) Attributes() {
fmt.Println("\nSpecification:")
fmt.Println(shrt.Size)
fmt.Println(shrt.Pattern)
fmt.Println(shrt.Sleeve)
fmt.Println(shrt.Fabric)
}
func (prd Product) Inventory() {
fmt.Println(prd.Stock)
}
// A struct named Catalog is declared to represent catalog of various products.
// The type of Details foeld is used a slice of the Information interface
type Catalog struct {
Name string
Details []Information
}
func (c Catalog) CatalogDetails() {
fmt.Println("**************************************************")
fmt.Printf("\nStore : %s \n", c.Name)
for _, key := range c.Details {
fmt.Println("===================Products==================")
key.General()
key.Attributes()
key.Inventory()
fmt.Println("=============================================")
}
}
func main() {
//Create an Instance of Mobile Type and Call Methods
MobilePrd := Mobile{
Product{
"Apple iPhone 6s (Rose Gold, 32 GB) (2 GB RAM)",
"Handset, Apple EarPods with Remote and Mic, Lightning to USB Cable",
40999, 143, 10,
},
[]string{"2 GB RAM", "4.7 inch Retina HD Display", "12MP Primary Camera"},
[]string{"32 GB RAM", "4.7 inch Retina HD Display", "5MP Primary Font"},
}
//Create an Instance of Shirts Type and Call Meethods
shirtPrd := Shirts{
Product{
"Reebok Stripped Men's Round Neck Grey T-Shirt",
"Machine Wash as per Tag, Do Not Dry Clean, Do Not Bleach",
1124, 400, 25,
},
"XL", "Stripped", "Half Sleeve", "Cotton",
}
//Create an Instance of Catalog Type
category := Catalog{
"Amazon",
[]Information{MobilePrd, shirtPrd},
}
category.CatalogDetails()
}