Go语言中继承结构体的数组

huangapple go评论84阅读模式
英文:

Array of inherited structs in Go Lang

问题

最近我开始用Go语言构建一个国际象棋游戏,我遇到的一个问题是如何将不同的角色(如:兵、马、王)存储在同一个数组中。

在上面的代码中,我们可以将马和王放在同一个数组中吗?因为它们都是从同一个基类继承而来。

像这样:

characters := []character{Knight{}, King{}}
英文:

Recently I have started building a chess game in GoLang and one issue I'm facing is storing different characters (i.e. Pawn, Knight, King) in a single array.

package main

import "fmt"

type character struct {
	currPosition [2]int
}

type Knight struct {
	c character
}

func (K Knight) Move() {
	fmt.Println("Moving Kinght...")
}

type King struct {
	c character
}

func (K King) Move() {
	fmt.Println("Moving King...")
}

In the above case, can we have Knight and King in the same array since they are inherited from the same base class?

Like

characters := []character{Knight{}, King{}}

答案1

得分: 2

Go语言没有类和继承。Go语言不支持编译时多态性(因为不支持方法重载)。它只有运行时多态性。
但是它有一个叫做组合的概念。使用结构体来形成其他对象。

你可以在这里阅读为什么Golang没有像其他编程语言中的面向对象编程概念那样的继承:https://www.geeksforgeeks.org/inheritance-in-golang/

或者

你可以使用单个结构体来表示所有的国际象棋棋子,并为它们的相应属性赋予不同的值。

// Piece表示一个国际象棋棋子
type Piece struct {
    Name   string
    Color  string
    PosX   int
    PosY   int
    Moves  int
    Points int
}

type Board struct {
    Squares [8][8]*Piece
}

func (b *Board) MovePiece(p *Piece, x, y int){
      // 移动棋子的逻辑
}

...
// 在构建棋盘时为Piece结构体创建对象。
king := &Piece{Name: "King", Color: "White", Points: 10}

或者

如果你想要单独实现国际象棋棋子,你必须使用接口。

英文:

Go doesn't have classes and inheritance. Compile-time polymorphism is not possible in Go (since method overloading is not supported). It only has Runtime Polymorphism.
But it has a concept called composition. Where the struct is used to form other objects.

You can read here why Golang doesn't have inheritance like OOP concepts in other programming languages. https://www.geeksforgeeks.org/inheritance-in-golang/

OR

Instead of implementing chess pieces separately, you can have single struct for all of them with different values of respective attributes.

// Piece represents a chess piece
type Piece struct {
	Name   string
	Color  string
	PosX   int
	PosY   int
	Moves  int
	Points int
}

type Board struct {
	Squares [8][8]*Piece
}

func (b *Board) MovePiece(p *Piece, x, y int){
      // Your logic to move a chess piece.
}

...
// and make objects for Piece struct as you build the board.
king := &Piece{Name: "King", Color: "White", Points: 10}

OR

You must use interface if you want to implement chess pieces separately.

答案2

得分: 2

使用基本接口来实现多态性

type character interface {
    Move()
    Pos() [2]int
}

type Knight struct {
    pos [2]int
}

func (k *Knight) Move() {
    fmt.Println("Moving Knight...")
}

func (k *Knight) Pos() [2]int { return k.pos }

type King struct {
    pos [2]int
}

func (k *King) Move() {
    fmt.Println("Moving King...")
}

func (k *King) Pos() [2]int { return k.pos }

使用这些更改,以下语句可以编译通过:

characters := []character{&Knight{}, &King{}}

此外,你可能希望在这个示例中使用指针接收器。

英文:

Use basic interfaces for polymorphism.

type character interface {
	Move()
	Pos() [2]int
}

type Knight struct {
	pos [2]int
}

func (K *Knight) Move() {
	fmt.Println("Moving Kinght...")
}

func (k *Knight) Pos() [2]int { return k.pos }

type King struct {
	pos [2]int
}

func (k *King) Move() {
	fmt.Println("Moving King...")
}

func (k *King) Pos() [2]int { return k.pos }

The following statement compiles with this changes:

characters := []character{&Knight{}, &King{}}

Also, you probably want pointer receivers as in this example.

huangapple
  • 本文由 发表于 2023年1月7日 12:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75038099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定