英文:
Go and Object Oriented Paradigm
问题
给定以下接口...
type Person struct {
name, gender string
}
type Citizen interface {
Create(name string) Citizen
GetName() string
GetGender() String
SayHello() String
}
... 以及以下的 Male
和 Female
实现:
type Male Person
func (m *Male) Create(name) Citizen {
return &Person{name, "male"}
}
func (m *Male) GetName() string {
return m.name
}
func (m *Male) GetGender() string {
return m.gender
}
func (m *Male) SayHello() string {
return "Hello (male voice)"
}
type Female Person
func (f *Female) Create(name) Citizen {
return &Person{name, "female"}
}
func (f *Female) GetName() string {
return f.name
}
func (f *Female) GetGender() string {
return f.gender
}
func (m *Female) SayHello() string {
return "Hello (female voice)"
}
func main() {
var p1 Person = &Male{}
var p2 Person = &Female{}
p1 = p1.Create("Joe")
p2 = p2.Create("Heidi")
log.Printf("%s is a %s",p1.GetName(), p1.GetGender())
log.Printf("%s says %s",p1.GetName(), p1.SayHello())
log.Printf("%s is a %s",p2.GetName(), p2.GetGender())
log.Printf("%s says %s",p2.GetName(), p2.SayHello())
}
由于只有 Create
方法与性别相关,是否有办法让 Male
和 Female
继承 GetName
和 GetGender
方法的共同实现呢?
英文:
Given the following interface...
type Person struct {
name, gender string
}
type Citizen interface {
Create(name string) Citizen
GetName() string
GetGender() String
SayHello() String
}
... and the following Male
and Female
implementations:
type Male Person
func (m *Male) Create(name) Citizen {
return &Person{name, "male"}
}
func (m *Male) GetName() string {
return m.name
}
func (m *Male) GetGender() string {
return m.gender
}
func (m *Male) SayHello() string {
return "Hello (male voice)"
}
type Female Person
func (f *Female) Create(name) Citizen {
return &Person{name, "female"}
}
func (f *Female) GetName() string {
return f.name
}
func (f *Female) GetGender() string {
return f.gender
}
func (m *Female) SayHello() string {
return "Hello (female voice)"
}
func main() {
var p1 Person = &Male{}
var p2 Person = &Female{}
p1 = p1.Create("Joe")
p2 = p2.Create("Heidi")
log.Printf("%s is a %s",p1.GetName(), p1.GetGender())
log.Printf("%s says %s",p1.GetName(), p1.SayHello())
log.Printf("%s is a %s",p2.GetName(), p2.GetGender())
log.Printf("%s says %s",p2.GetName(), p2.SayHello())
}
As only Create
is gender-specific, is there a way to let Male
and Female
inherit a common implementation of methods GetName
and GetGender
?
答案1
得分: 4
将Person
嵌入到Male
和Female
类型中,并将基本方法提升到Person
中。
type Male struct {
Person
}
type Female struct {
Person
}
func (m *Person) GetName() string {
return m.Name
}
func (m *Person) GetGender() string {
return m.Gender
}
func main() {
var c Citizen
c = CreateMale("Joe")
fmt.Println(c.GetName(), c.GetGender(), "\n", c.SayHello())
c = CreateFemale("Jane")
fmt.Println(c.GetName(), c.GetGender(), "\n", c.SayHello())
}
完整示例请参见:https://play.golang.org/p/GZcG-n_YpM
此示例中使用了接口Citizen
而不是类型Person
进行赋值,如果这不是您要寻找的,请告诉我。
英文:
Embed Person
in the Male
and Female
types and push the base methods up to Person
.
type Male struct {
Person
}
type Female struct {
Person
}
func (m *Person) GetName() string {
return m.Name
}
func (m *Person) GetGender() string {
return m.Gender
}
func main() {
var c Citizen
c = CreateMale("Joe")
fmt.Println(c.GetName(), c.GetGender(), "\n", c.SayHello())
c = CreateFemale("Jane")
fmt.Println(c.GetName(), c.GetGender(), "\n", c.SayHello())
}
See full example here: https://play.golang.org/p/GZcG-n_YpM
This uses the interface Citizen
instead of the type Person
in the assignment, so let me know if that isn't what you are looking for.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论