英文:
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() <= 0
}
now I have a question, if I have a constuctor like this:
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
}
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 := &LinkedList[T]{size: 0}
headNode := &linkedListNode[T]{}
tailNode := &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
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论