继承如何将这个Java接口和继承结构转换为Golang?
有没有可能将这种使用接口和继承的Java结构重写为一种惯用的Golang方式
这不是超级复杂的Java代码,但它显示了类继承的优势,但我想尝试在Go中实现同样的结果
Java代码:
首先是一个类接口
public interface WebEntry {
String perform(ConnectionData connectionData, SessionData sessionData) throws Exception;
}
某个地方有一个网络条目列表WebEntry
:
List<WebEntry> webEntries = new ArrayList<>();
webEntries.add( new SavePasswordWebEntry() );
一个抽象类UserLoggedInWebEntry
,它实现了WebEntry
public abstract class UserLoggedInWebEntry implements WebEntry {
@Override
public String perform(ConnectionData connectionData, SessionData sessionData) throws Exception {
// THIS IS THE LOGIC I DO NOT WANT TO DUPLICATE, HENCE ITS USING CLASS INHERITANCE
...
return userPerform(User user);
}
protected abstract String userPerform(User user) throws Exception;
}
通过使用类继承,我们可以创建一个UserLoggedInWebEntry
,它的行为仍然是WebEntry
,但我们不需要在每次需要“用户登录web条目”实例时复制...
逻辑:
public class SavePasswordWebEntry extends UserLoggedInWebEntry {
@Override
protected String userPerform(User user) throws Exception {
...
}
}
所以关键是,由于它使用类继承,开发人员只需要实现String userPerform(User user)
,而不需要在UserLoggedInWebEntry
中重复...
的逻辑
这在歌朗是否可能实现?如果是这样,情况会是怎样的
# 1 楼答案
给你看。我不知道我是否理解正确
Playground
# 2 楼答案
尽管我很喜欢自己得到的“只学一步”的答案,但我还是找到了自己实现同样目标的方法
可以通过创建一个包装函数
userLoggedInWebEntry
来解决这个问题,该函数接受WebEntryUserLoggedIn
函数签名,但返回WebEntry
函数签名它实现了与上面的Java继承代码相同的功能,只允许开发人员实现
func( user User ) string
,并且userLoggedInWebEntry
中的逻辑不重复https://play.golang.org/p/adrk46YFIl8