Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何翻译这个Java接口&;戈朗的继承结构?_Java_Inheritance_Go - Fatal编程技术网

如何翻译这个Java接口&;戈朗的继承结构?

如何翻译这个Java接口&;戈朗的继承结构?,java,inheritance,go,Java,Inheritance,Go,是否有可能将这个使用接口和继承的Java结构重写为一种惯用的Golang方式 这不是超级复杂的Java代码,但它显示了类继承的优势,但我想尝试在Go中实现同样的结果 Java代码: 首先是一个类接口 公共界面WebEntry{ 字符串执行(ConnectionData ConnectionData,SessionData SessionData)抛出异常; } 某个地方有一个web条目列表WebEntry: List webEntries=new ArrayList(); 添加(新的SaveP

是否有可能将这个使用接口和继承的Java结构重写为一种惯用的Golang方式

这不是超级复杂的Java代码,但它显示了类继承的优势,但我想尝试在Go中实现同样的结果

Java代码:

首先是一个类接口

公共界面WebEntry{
字符串执行(ConnectionData ConnectionData,SessionData SessionData)抛出异常;
}
某个地方有一个web条目列表
WebEntry

List webEntries=new ArrayList();
添加(新的SavePasswordWebEntry());
一个抽象类
UserLoggedInWebEntry
,它实现了
WebEntry

公共抽象类UserLoggedInWebEntry实现WebEntry{
@凌驾
公共字符串执行(ConnectionData ConnectionData,SessionData SessionData)引发异常{
//这是我不想重复的逻辑,因此它使用类继承
...
返回userPerform(用户);
}
受保护的抽象字符串userPerform(用户用户)引发异常;
}
通过使用类继承,我们可以创建一个
UserLoggedInWebEntry
,它仍然作为
WebEntry
运行,但我们不需要在每次需要“用户登录web entry”实例时复制
逻辑:

公共类SavePasswordWebEntry扩展了UserLoggedInWebEntry{
@凌驾
受保护字符串userPerform(用户用户)引发异常{
...
}
}
因此,整个要点是,由于它使用类继承,开发人员只需要实现
stringuserperform(User-User)
,而不需要复制
UserLoggedInWebEntry
中的
逻辑

这是否可能在戈朗实现?如果是这样,它会是什么样子?

可能是这样

type WebEntry interface {
    Perform() string
}

type SomeWebEntry struct {
}

func (_ *SomeWebEntry) Perform() string {
    return "SomeWebEntry"
}

type OtherWebEntry struct {
    SomeWebEntry
}

func (this *OtherWebEntry) OtherPerform() string {
    return this.Perform() + "OtherWebEntry"
}

func main() {
    webEntries := []WebEntry{&SomeWebEntry{},&OtherWebEntry{}}
    for _, v := range webEntries {
        fmt.Println(v.Perform())
    }
    fmt.Println("-")
    fmt.Println(webEntries[1].(*OtherWebEntry).OtherPerform())
}

您可以使用一个名为。

的功能。看这里。我不知道我是否理解正确

package main

import (
    "fmt"
)

type ConnectionData struct{}
type SessionData struct{}

type WebEntry interface {
    Perform(c ConnectionData, s SessionData) (string, error)
}

type UserLoggedInWebEntry struct {
}

func (u *UserLoggedInWebEntry) Perform(c ConnectionData, s SessionData) (string, error) {
    return u.UserPerform(c, s)
}

func (u *UserLoggedInWebEntry) UserPerform(c ConnectionData, s SessionData) (string, error) {
    return "UserLoggedInWebEntry UserPerform", nil
}

type SavePasswordWebEntry struct {
    UserLoggedInWebEntry
}

func (sp *SavePasswordWebEntry) Perform(c ConnectionData, s SessionData) (string, error) {
    return "SavePasswordWebEntry Perform", nil
}

func main() {
    webEntries := make([]WebEntry,0)
    webEntries = append(webEntries, new(SavePasswordWebEntry))
    for _, we := range webEntries {
        fmt.Println(we.Perform(ConnectionData{},SessionData{}))
    }
}

尽管我很喜欢自己得到的“只学一步”的答案,但我还是自己想出了如何实现同样的目标

它通过创建一个包装函数
userLoggedInWebEntry
来解决,该函数接受
WebEntryUserLoggedIn
函数签名,但返回
WebEntry
函数签名

它实现了与上面的Java继承代码相同的功能,只允许开发人员实现
func(user-user)字符串
,并且
userLoggedInWebEntry
中的逻辑不重复

主程序包
进口(
“fmt”
)
类型ConnectionData字符串
键入SessionData字符串
输入用户字符串
键入WebEntry func(connectionData connectionData,sessionData sessionData)字符串
键入webentryuserloggedinfunc(用户)字符串
func main(){
var webentries[]WebEntry
webentries=append(webentries,userLoggedInWebEntry(SavePasswordWebEntry))
//测试调用
fmt.Println(网络条目[0](“连接数据”、“会话数据”))
}
func userLoggedInWebEntry(webEntry WebEntryUserLoggedIn)webEntry{
返回func(connectionData connectionData,sessionData sessionData)字符串{
////这是我不想复制的逻辑,因此它使用类继承
var user=“John”
返回webEntry(用户)
} 
}
func SavePasswordWebEntry(用户)字符串{
返回fmt.Sprintf(“用户是%s”,用户)
}

重新创建什么?像任何Java代码一样混乱?是的,你也可以在围棋中做傻事。关于Go代码的实际问题是什么?正如我在问题中所说:>但是,通过创建一个抽象类UserLoggedInWebEntry,开发人员可以轻松创建只实现具有签名保护字符串userPerform(User User User)的方法的web条目,而无需复制。。。UserLoggedInWebEntry.perform(…)中的逻辑。通过在这里使用类继承,开发不需要复制在
UserLoggedInWebEntry
中发生的“…”逻辑,这是我的问题。我如何在Golang实现同样的目标?这可能吗?如果是这样的话,如何“学着去”——那就是。。我正在努力。。问。。这是关于Stackoveflow的问题。如果我知道正确的Go以及如何实现同样的目标,我就不会在这里问了。主要的建议是:停止在Go中重新创建Java模式。你会受伤的。别管了。你做不到,你就不会成功。不要问“如何在Go中实现Java的模式X”。如果我不清楚,很抱歉。不幸的是,这是错误的,因为“SavePasswordWebEntry”应该包含一个“用户”——请注意参数签名是如何更改的。在上述解决方案中,所有函数都采用“ConnectionData”和“SessionData”。