一个接口,多个实现

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

One Interface, Many Implementations

问题

如何将以下Java代码翻译成Go语言?

interface NamePrinter {
    void print();
}

class NamePrinterWithoutGreeting implements NamePrinter {
    private string name;

    public NamePrinterWithoutGreeting(string name) {
        this.name = name;
    }

    public void print() {
        System.out.println(this.name);
    }
}

class NamePrinterWithGreeting implements NamePrinter {
    private string name;

    public NamePrinterWithoutGreeting(string name) {
        this.name = name;
    }

    public void print() {
        System.out.println("Hello, " + this.name);
    }
}

Type `NamePrinter`可以引用`NamePrinterWithoutGreeting``NamePrinterWithGreeting`的实例

void main(String[] args) {
    a NamePrinter = new NamePrinterWithoutGreeting("Joe");
    b NamePrinter = new NamePrinterWithGreeting("Joe");

    a.print(); // 输出 "Joe"
    b.print(); // 输出 "Hello, Joe"
}

回到go... 我想要一个类型为NamePrinter的接口,可以引用多个不同的实现... 但我不知道该如何做。下面是一个实现... 但它只适用于一种情况:

type Person struct {
    name string
}

type NamePrinter interface {
    Create(name string)
    Print()
}

func Create(name string) *Person {
    n := Person{name}
    return &n
}

func (p *Person) print() {
    fmt.Println(p.name)
}

func main() {
    p := Create("joe")
    fmt.Println(p.Print())
}

谢谢。

英文:

How do I translate the following Java code to Go?

interface NamePrinter {
    void print();
}

class NamePrinterWithoutGreeting implements NamePrinter {
    private string name;

    public NamePrinterWithoutGreeting(string name) {
        this.name = name;
    }

    public void print() {
        System.out.println(this.name);
    }
}

class NamePrinterWithGreeting implements NamePrinter {
    private string name;

    public NamePrinterWithoutGreeting(string name) {
        this.name = name;
    }

    public void print() {
        System.out.println("Hello, " + this.name);
    }
}

Type NamePrinter can reference an instance of both NamePrinterWithoutGreeting and NamePrinterWithGreeting:

void main(String[] args) {
    a NamePrinter = new NamePrinterWithoutGreeting("Joe");
    b NamePrinter = new NamePrinterWithGreeting("Joe");

    a.print(); // prints "Joe"
    b.print(); // prints "Hello, Joe"
}

Back to go... I'd like to have an interface of type NamePrinter that could reference many different implementations... but I don't know how to do it. Here below is an implementation... but it is fine for just one case:

type Person struct {
    name string
}

type NamePrinter interface {
    Create(name string)
    Print()
}

func Create(name string) *Person {
    n := Person{name}
    return &n
}

func (p *Person) print() {
    fmt.Println(p.name)
}

func main() {
    p := Create("joe")
    fmt.Println(p.Print())
}

Thank you.

答案1

得分: 3

你定义的任何类型,只要你实现了一组方法,这些方法的签名与接口定义的方法签名相等,那么这个类型就可以在期望接口的地方使用。

type NamePrinter interface {
    print()
}

type NamePrinterWithoutGreeting struct {
    name string
}

func (p *NamePrinterWithoutGreeting) print() {
    fmt.Println(p.name)
}

type NamePrinterWithGreeting struct {
    name string
}

func (p *NamePrinterWithGreeting) print() {
    fmt.Println("Hello, ", p.name)
}

type MyInt int

func (i MyInt) print() {
    fmt.Printf("Hello, %d\n", i)
}

type MyFunc func() string

func (f MyFunc) print() {
    fmt.Println("Hello,", f())
}

func main() {
    var a NamePrinter = &NamePrinterWithoutGreeting{"joe"}
    var b NamePrinter = &NamePrinterWithGreeting{"joe"}
    var i NamePrinter = MyInt(2345)
    var f NamePrinter = MyFunc(func() string { return "funk" })
    a.print()
    b.print()
    i.print()
    f.print()
}

链接:https://play.golang.org/p/hW1q8eMve3

英文:

Any type that you define, and on which you implement a set of methods that are equal in their signatures to those defined by an interface, that type can be used in place where you expect that interface.

type NamePrinter interface {
    print()
}

type NamePrinterWithoutGreeting struct {
    name string
}

func (p *NamePrinterWithoutGreeting) print() {
    fmt.Println(p.name)
}

type NamePrinterWithGreeting struct {
    name string
}

func (p *NamePrinterWithGreeting) print() {
    fmt.Println("Hello, ", p.name)
}

type MyInt int

func (i MyInt) print() {
    fmt.Printf("Hello, %d\n", i)
}

type MyFunc func() string

func (f MyFunc) print() {
    fmt.Println("Hello,", f())
}
func main() {
    var a NamePrinter = &NamePrinterWithoutGreeting{"joe"}
    var b NamePrinter = &NamePrinterWithGreeting{"joe"}
    var i NamePrinter = MyInt(2345)
    var f NamePrinter = MyFunc(func() string { return "funk" })
    a.print()
    b.print()
    i.print()
    f.print()
}

https://play.golang.org/p/hW1q8eMve3

huangapple
  • 本文由 发表于 2017年8月30日 00:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/45944052.html
匿名

发表评论

匿名网友

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

确定