dongwen5336 2017-08-29 16:25
浏览 23
已采纳

一个接口,多种实现

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条回答 默认 最新

  • drxdai15012937753 2017-08-29 16:33
    关注

    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
    ", 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了