自动将子类实例转换为接口

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

Auto cast subclass instance to interface

问题

我有一个接口,像这样:

type Collections[T any] interface {
	Add(element T) bool

	Get(index uint64) T

	Length() uint64
}

我有一个实现该接口的子类:

type LinkedList[T any] struct {
	size uint64
	head *linkedListNode[T]
	tail *linkedListNode[T]
}

我定义了一个包函数:

func IsEmpty[T any](collections Collections[T]) bool {
	if collections == nil {
		return true
	}

	return collections.Length() <= 0
}

现在我有一个问题,如果我有一个像这样的构造函数:

func NewLinkedList[T any]() Collections[T] {
	linkList := &LinkedList[T]{size: 0}

	headNode := &linkedListNode[T]{}
	tailNode := &linkedListNode[T]{}

	headNode.next = tailNode
	tailNode.pre = headNode

	linkList.head = headNode
	linkList.tail = tailNode

	return linkList
}

那么我可以像这样使用包函数:

func TestLinkedListCreate(t *testing.T) {
	var obj = datastruct.NewLinkedList[string]()
	empty := datastruct.IsEmpty(obj)
	println(empty)
}

但是如果我有一个像这样的构造函数:

func NewLinkedList[T any]() *LinkedList[T] {
	linkList := &LinkedList[T]{size: 0}

	headNode := &linkedListNode[T]{}
	tailNode := &linkedListNode[T]{}

	headNode.next = tailNode
	tailNode.pre = headNode

	linkList.head = headNode
	linkList.tail = tailNode

	return linkList
}

我必须像这样使用包函数:

func TestLinkedListCreate(t *testing.T) {
	var obj datastruct.Collections[string] = datastruct.NewLinkedList[string]()
	empty := datastruct.IsEmpty(obj)
	println(empty)
}

那么为什么在函数中,Golang可以自动将子类转换为接口,但在声明中不能将子类转换为接口?

我使用的是Golang 1.19版本。

英文:

I have a interface, like this:

type Collections[T any] interface {
	Add(element T) bool

	Get(index uint64) T

	Length() uint64
}

and I have a subclass implement the interface:

type LinkedList[T any] struct {
	size uint64
	head *linkedListNode[T]
	tail *linkedListNode[T]
}

and I define a package function:

func IsEmpty[T any](collections Collections[T]) bool {
	if collections == nil {
		return true
	}

	return collections.Length() &lt;= 0
}

now I have a question, if I have a constuctor like this:

func NewLinkedList[T any]() Collections[T] {
	linkList := &amp;LinkedList[T]{size: 0}

	headNode := &amp;linkedListNode[T]{}
	tailNode := &amp;linkedListNode[T]{}

	headNode.next = tailNode
	tailNode.pre = headNode

	linkList.head = headNode
	linkList.tail = tailNode

	return linkList
}

then I can use the package function like this:

func TestLinkedListCreate(t *testing.T) {
	var obj = datastruct.NewLinkedList[string]()
	empty := datastruct.IsEmpty(obj)
	println(empty)
}

but if I have a constuctor like this:

func NewLinkedList[T any]() *LinkedList[T] {
	linkList := &amp;LinkedList[T]{size: 0}

	headNode := &amp;linkedListNode[T]{}
	tailNode := &amp;linkedListNode[T]{}

	headNode.next = tailNode
	tailNode.pre = headNode

	linkList.head = headNode
	linkList.tail = tailNode

	return linkList
}

I have to use package function like this:

func TestLinkedListCreate(t *testing.T) {
	var obj datastruct.Collections[string] = datastruct.NewLinkedList[string]()
	empty := datastruct.IsEmpty(obj)
	println(empty)
}

so why golang can auto cast subclass to interface in function, but can't cast subclass to interface in declare?

I use golang 1.19

答案1

得分: 1

Volker评论中说道:

> Go语言没有类型转换、类、继承和子类。你的问题没有意义。在Go中无法进行传统的面向对象编程。重新设计。

英文:

Volker said in a comment:

> Go has no casts, no classes, no inheritance and no subclasses. Your question makes no sense. You cannot do traditional OOP in Go. Redesign

huangapple
  • 本文由 发表于 2022年8月14日 11:48:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/73349010.html
匿名

发表评论

匿名网友

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

确定