Oop 反映嵌入结构的类型
我目前正在尝试一些OOP风格的围棋,遵循我在网上找到的教程 到目前为止,它非常迷人(让我想起了试图将OOP强制转换为ANSI-C) 然而,有一件事困扰着我,我似乎无法解决 如何反映嵌入结构的类型名 我在网上找到的所有信息都表明,人们无法反映嵌入结构,因为嵌入结构无法直接访问它 这完全准确吗?如果是这样,解决以下问题的正确方法是什么(代码如下) 基本上,程序会打印出三个单独动物的名称,然后是括号中嵌入结构的类型名称,然后是相应动物的“声音” 对于名为“漫游者”的狗,它将打印“漫游者(动物):树皮” 现在,很明显,“漫游者(动物)”并不是特别有用。 理想情况下,这应该是“Rover(Dog)”(嵌入结构的类型名,而不是嵌入结构的类型名) 这就是我的问题所在。我如何才能反映嵌入结构的类型,使“漫游者(动物)”变成“漫游者(狗)”,“朱利叶斯(动物)”变成“朱利叶斯(猫)”,等等Oop 反映嵌入结构的类型,oop,go,struct,reflection,types,Oop,Go,Struct,Reflection,Types,我目前正在尝试一些OOP风格的围棋,遵循我在网上找到的教程 到目前为止,它非常迷人(让我想起了试图将OOP强制转换为ANSI-C) 然而,有一件事困扰着我,我似乎无法解决 如何反映嵌入结构的类型名 我在网上找到的所有信息都表明,人们无法反映嵌入结构,因为嵌入结构无法直接访问它 这完全准确吗?如果是这样,解决以下问题的正确方法是什么(代码如下) 基本上,程序会打印出三个单独动物的名称,然后是括号中嵌入结构的类型名称,然后是相应动物的“声音” 对于名为“漫游者”的狗,它将打印“漫游者(动物):树皮”
主程序包
进口(
“fmt”
“反映”
)
类型动物结构{
名称字符串
卑鄙
}
类型AnimalSounder接口{
制造噪音()
}
类型狗结构{
动物
树皮强度
}
类型Cat结构{
基础动物
MeowStrength int
}
狮子型结构{
基础动物
咆哮强度整数
}
func(狗*狗)制造噪音(){
狗。性能噪声(狗。吠声强度,“吠声”)
}
func(cat*cat)MakeNoise(){
cat.Basics.PerformNoise(cat.MEOWSTRENGHT,“喵”声)
}
func(lion*lion)MakeNoise(){
lion.Basics.PerformNoise(lion.roarstrong,“咆哮!!”)
}
func MakeSomeNoise(animalSounder animalSounder){
animalSounder.MakeNoise()
}
func main(){
myDog:=&Dog{
畜生{
名称:“漫游者”//Name
mean:false,//mean
},
2、//1的强度
}
myCat:=&Cat{
基础知识:动物{
姓名:“朱利叶斯”,
是的,
},
实力:3,
}
野狮:=&狮子{
基础知识:动物{
名称:“阿斯兰”,
是的,
},
总人数:5人,
}
制造噪音(我的狗)
制造噪音(myCat)
制造噪音(野狮)
}
func(动物*动物)性能噪声(强度整数,声音字符串){
如果animal.mean==真{
强度=强度*5
}
fmt.Printf(“%s(%s):\n”,animal.Name,reflect.ValueOf(animal.Type().Elem().Name())
对于语音:=0;语音<强度;语音++{
fmt.Printf(“%s”,声音)
}
fmt.Println(“\n”)
}
好的
然后回答(或至少尝试回答)我自己的问题,提供我认为可能是解决这个问题的正确方法(至少是最直接的方法)
为了简洁起见,我更改了所有版本的
func (cat/dog/lion *Cat/*Dog/*Lion) MakeNoise(){}
到
据我所知,这不应该真正损害可读性,也不会带来任何副作用
“MakeNoise()”的所有迭代现在只提供第三个参数
这与作为字符串的嵌入结构的类型名相同
“PerformNoise()”接受该参数(“animalType”)并将其简单地附加到输出
我爱你
完整的更新代码:
package main
import (
"fmt"
"reflect"
)
type Animal struct {
Name string
Type string
mean bool
}
type AnimalSounder interface {
MakeNoise()
}
type Dog struct {
Animal
BarkStrength int
}
type Cat struct {
Basics Animal
MeowStrength int
}
type Lion struct {
Basics Animal
RoarStrength int
}
func (animal *Dog) MakeNoise() {
animal.PerformNoise(animal.BarkStrength, "BARK", reflect.ValueOf(animal).Type().Elem().Name())
}
func (animal *Cat) MakeNoise() {
animal.Basics.PerformNoise(animal.MeowStrength, "MEOW", reflect.ValueOf(animal).Type().Elem().Name())
}
func (animal *Lion) MakeNoise() {
animal.Basics.PerformNoise(animal.RoarStrength, "ROAR!! ", reflect.ValueOf(animal).Type().Elem().Name())
}
func MakeSomeNoise(animalSounder AnimalSounder) {
animalSounder.MakeNoise()
}
func main() {
myDog := &Dog{
Animal{
Name: "Rover", // Name
mean: false, // mean
},
2, // BarkStrength
}
myCat := &Cat{
Basics: Animal{
Name: "Julius",
mean: true,
},
MeowStrength: 3,
}
wildLion := &Lion{
Basics: Animal{
Name: "Aslan",
mean: true,
},
RoarStrength: 5,
}
MakeSomeNoise(myDog)
MakeSomeNoise(myCat)
MakeSomeNoise(wildLion)
}
func (animal *Animal) PerformNoise(strength int, sound string, animalType string) {
if animal.mean == true {
strength = strength * 5
}
fmt.Printf("%s (%s): \n", animal.Name, animalType)
for voice := 0; voice < strength; voice++ {
fmt.Printf("%s ", sound)
}
fmt.Println("\n")
}
主程序包
进口(
“fmt”
“反映”
)
类型动物结构{
名称字符串
类型字符串
卑鄙
}
类型AnimalSounder接口{
制造噪音()
}
类型狗结构{
动物
树皮强度
}
类型Cat结构{
基础动物
MeowStrength int
}
狮子型结构{
基础动物
咆哮强度整数
}
func(动物*狗)制造噪音(){
animal.PerformNoise(animal.BarkStrength,“BARK”,reflect.ValueOf(animal).Type().Elem().Name())
}
func(动物*猫)制造噪音(){
animal.Basics.PerformNoise(animal.MeowStrength,“MEOW”,reflect.ValueOf(animal.Type().Elem().Name())
}
func(动物*狮子)制造噪音(){
animal.Basics.PerformNoise(animal.RoarStrength,“ROAR!!”,reflect.ValueOf(animal.Type().Elem().Name())
}
func MakeSomeNoise(animalSounder animalSounder){
animalSounder.MakeNoise()
}
func main(){
myDog:=&Dog{
畜生{
名称:“漫游者”//Name
mean:false,//mean
},
2、//1的强度
}
myCat:=&Cat{
基础知识:动物{
姓名:“朱利叶斯”,
是的,
},
实力:3,
}
野狮:=&狮子{
基础知识:动物{
名称:“阿斯兰”,
是的,
},
总人数:5人,
}
制造噪音(我的狗)
制造噪音(myCat)
制造噪音(野狮)
}
func(动物*动物)性能噪声(强度整数、声音字符串、动物类型字符串){
如果animal.mean==真{
强度=强度*5
}
fmt.Printf(“%s(%s):\n”、animal.Name、animalType)
对于语音:=0;语音<强度;语音++{
fmt.Printf(“%s”,声音)
}
fmt.Println(“\n”)
}
PS:只是重复一下。我不想用这种“假”OOP的方式写我所有的代码
它增加了大量不必要的抽象,并需要对流程进行新的设计考虑
然而,我确实认为这是一个很好的方法来试验该语言的基本功能集。它是完全准确的,Go中没有继承,嵌入式结构
动物不知道嵌入式结构狗。我不认为在一种非设计语言上强制使用经典OOP是特别明智的我唯一能想到的就是扩展“PerformNoise()”,这样它就需要一个“动物类型名称”,然后将它们添加到“MakeNoise()”(“AnimalSounder”接口方法)的所有实现中。所以dog.PerformNoise(dog.Bar
func (animal *Cat/*Dog/*Lion) MakeNoise(){}
fmt.Printf("%s (%s): \n", animal.Name, animalType)
package main
import (
"fmt"
"reflect"
)
type Animal struct {
Name string
Type string
mean bool
}
type AnimalSounder interface {
MakeNoise()
}
type Dog struct {
Animal
BarkStrength int
}
type Cat struct {
Basics Animal
MeowStrength int
}
type Lion struct {
Basics Animal
RoarStrength int
}
func (animal *Dog) MakeNoise() {
animal.PerformNoise(animal.BarkStrength, "BARK", reflect.ValueOf(animal).Type().Elem().Name())
}
func (animal *Cat) MakeNoise() {
animal.Basics.PerformNoise(animal.MeowStrength, "MEOW", reflect.ValueOf(animal).Type().Elem().Name())
}
func (animal *Lion) MakeNoise() {
animal.Basics.PerformNoise(animal.RoarStrength, "ROAR!! ", reflect.ValueOf(animal).Type().Elem().Name())
}
func MakeSomeNoise(animalSounder AnimalSounder) {
animalSounder.MakeNoise()
}
func main() {
myDog := &Dog{
Animal{
Name: "Rover", // Name
mean: false, // mean
},
2, // BarkStrength
}
myCat := &Cat{
Basics: Animal{
Name: "Julius",
mean: true,
},
MeowStrength: 3,
}
wildLion := &Lion{
Basics: Animal{
Name: "Aslan",
mean: true,
},
RoarStrength: 5,
}
MakeSomeNoise(myDog)
MakeSomeNoise(myCat)
MakeSomeNoise(wildLion)
}
func (animal *Animal) PerformNoise(strength int, sound string, animalType string) {
if animal.mean == true {
strength = strength * 5
}
fmt.Printf("%s (%s): \n", animal.Name, animalType)
for voice := 0; voice < strength; voice++ {
fmt.Printf("%s ", sound)
}
fmt.Println("\n")
}