Go语言和面向对象编程范式

huangapple go评论157阅读模式
英文:

Go and Object Oriented Paradigm

问题

给定以下接口...

type Person struct {
    name, gender string
}

type Citizen interface {
    Create(name string) Citizen
    GetName() string
    GetGender() String
    SayHello() String
}

... 以及以下的 MaleFemale 实现:

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 方法与性别相关,是否有办法让 MaleFemale 继承 GetNameGetGender 方法的共同实现呢?

英文:

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嵌入到MaleFemale类型中,并将基本方法提升到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.

huangapple
  • 本文由 发表于 2017年9月1日 05:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/45990335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定