英文:
Reuse function with return type always equal to the struct that is calling that function in golang
问题
我正在尝试在Golang中做一些在Java中使用Type
可以轻松完成的事情。例如:
public class MyClass<Type extends MyClass<Type>> {
public Type myMethod() {
// Do something
return (Type) this;
}
}
public class Test1 extends MyClass<Test1> {
public String A;
Test1(String A) {
this.A = A;
}
}
public class Test2 extends MyClass<Test2> {
public String B;
Test2(String B) {
this.B = B;
}
}
因此,在主函数中,我可以这样做:
Test1 t1 = new Test1("hello");
Test2 t2 = new Test2("world");
System.out.println(t1.myMethod().A); // 输出 "hello"
System.out.println(t2.myMethod().B); // 输出 "world"
我正在尝试弄清楚在Go中是否有一种类似的方法。也许可以这样做:
type test1 struct {
A string
}
type test2 struct {
B string
}
func (val Type) MyMethod() Type {
// Do something
return val
}
func main() {
t1 := test1{A: "hello"}
t2 := test2{B: "world"}
fmt.Println(t1.MyMethod().A) // 输出 "hello"
fmt.Println(t2.MyMethod().B) // 输出 "world"
}
英文:
I'm trying to do in Golang something that would be easily done in Java with Type
. For example:
public class MyClass<Type extends MyClass<Type>> {
public Type myMethod() {
// Do something
return (Type) this;
}
}
public class Test1 extends MyClass<Test1> {
public String A;
Test1(String A) {
this.A = A;
}
}
public class Test2 extends MyClass<Test2> {
public String B;
Test2(String B) {
this.B = B;
}
}
So, in main I can do
Test1 t1 = new Test1("hello");
Test2 t2 = new Test2("world");
System.out.println(t1.myMethod().A); // Prints "hello"
System.out.println(t2.myMethod().B); // Prints "world"
I'm trying to figure out if there is a way to do somethig similar in Go.
Maybe something like:
type test1 struct {
A string
}
type test2 struct {
B string
}
func (val Type) MyMethod() Type {
// Do something
return val
}
func main() {
t1 := test1{A: "hello"}
t2 := test2{B: "world"}
fmt.Println(t1.MyMethod().A) // Prints "hello"
fmt.Println(t2.MyMethod().B) // Prints "world"
}
答案1
得分: 0
感谢JimB,我找到了一种方法。
从Go 1.18开始,支持泛型类型。
因此,在Go中,Java的MyClass
将类似于以下代码:
type MyGeneric[T any] struct {
self *T
}
func (val MyGeneric[T]) MyMethod() *T {
// 做一些操作
return val.self
}
Java类Test1
将是:
type test1 struct {
MyGeneric[test1]
A string
}
func MakeTest1(A string) test1 {
answer := test1{A: A}
answer.self = &answer
return answer
}
类似地,Java类Test2
将是:
type test2 struct {
MyGeneric[test2]
B string
}
func MakeTest2(B string) test2 {
answer := test2{B: B}
answer.self = &answer
return answer
}
所以,主函数如我们所想:
t1 := MakeTest1("hello")
t2 := MakeTest2("world")
fmt.Println(t1.MyMethod().A) // 输出 "hello"
fmt.Println(t2.MyMethod().B) // 输出 "world"
英文:
Thanks to JimB I found a way.
From Go 1.18 Generic Types are sypported.
So the Java MyClass
will look something like this in Go:
type MyGeneric[T any] struct {
self *T
}
func (val MyGeneric[T]) MyMethod() *T {
// Do something
return val.self
}
The Java class Test1
will be:
type test1 struct {
MyGeneric[test1]
A string
}
func MakeTest1(A string) test1 {
answer := test1{A: A}
answer.self = &answer
return answer
}
And similarly the Java class Test2
:
type test2 struct {
MyGeneric[test2]
B string
}
func MakeTest2(B string) test2 {
answer := test2{B: B}
answer.self = &answer
return answer
}
So the main is as we wanted
t1 := MakeTest1("hello")
t2 := MakeTest2("world")
fmt.Println(t1.MyMethod().A) // Prints "hello"
fmt.Println(t2.MyMethod().B) // Prints "world"
专注分享java语言的经验与见解,让所有开发者获益!
评论