如果类型 T2 基于类型 T1,是否存在任何类型的“继承"?从T1到T2? [英] If type T2 is based on type T1, is there any sort of "inheritance" from T1 to T2?

查看:24
本文介绍了如果类型 T2 基于类型 T1,是否存在任何类型的“继承"?从T1到T2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果type T2是基于type T1,除了共享相同的数据字段,T1有什么关系吗?>T2?

<前>包主导入fmt"类型 T1 结构 { s 字符串 }func (v *T1) F1() string { return v.s }类型 T2 T1func (v *T2) F2() string { return v.s }功能主(){var t1 = T1{xyz"}var t2 = T2{pdq"}s0 := t2.F1()//错误 - 预期正常s1 := ((*T1)(&t2)).F1()//好的 - 预期s2 := ((*T2)(&t1)).F2()//好的 - 不是预期的fmt.Println( s0, s1, s2 )}

我对这里缺乏了解

  1. 希望 T2 能够继承 T1 的方法,但事实并非如此.

  2. 期望 T2 可以被强制转换为 T1,因为它派生自 T1

  3. 很惊讶 T1 可以被强制转换为 T2,但事实确实如此.

  4. 似乎 T1T2 之间的关系是完全对称的——我找不到任何破坏对称性的东西,尽管事实上它是从其他 - 或者这是一种错觉?

解决方案

Go 不支持面向对象的类型继承.

Go 是一种面向对象的语言吗?

为什么没有类型继承?

一个方法绑定到一个特定的类型.

<块引用>

方法声明绑定了一个方法的标识符.方法是据说绑定到基本类型和仅在选择器中可见那种类型.

您可以在 T1 类型之间转换>T2.

<块引用>

x 可以转换以输入T [when] x 的类型和 T 有相同的底层类型.

例如

包主进口 (fmt")类型 T1 结构{ i int }func (t T1) String() string { return "T1" }类型 T2 T1func (t T2) String() string { return "T2" }功能主(){t1 := t1{1}t2 := T2{2}fmt.Println(t1, t2)c1 := T1(t2)c2 := T2(t1)fmt.Println(c1, c2)t1 = T1(c2)t2 = T2(c1)fmt.Println(t1, t2)}输出:T1 T2T1 T2T1 T2

If type T2 is based on type T1, other than sharing the same data fields, is there any relationship between T1 and T2?

package main
import "fmt"

type T1 struct { s string }
func (v *T1) F1() string { return v.s }

type T2 T1
func (v *T2) F2() string { return v.s }

func main() {
        var t1 = T1{ "xyz" }
        var t2 = T2{ "pdq" }
        s0 := t2.F1()                   // error - expected ok
        s1 := ((*T1)(&t2)).F1()         // ok - expected
        s2 := ((*T2)(&t1)).F2()         // ok - not expected
        fmt.Println( s0, s1, s2 )
}

My understanding here is lacking

  1. was hoping that T2 would inherit T1's methods, but such is not the case.

  2. was expecting T2 could be coerced into T1, since it was derived from T1

  3. was surprised that T1 could be coerced into T2, but so it is.

  4. it seems that the relationship between T1 and T2 is completely symmetrical - I cannot find anything that breaks the symmetry despite the fact one is actually derived from the other - or is this an illusion?

解决方案

Go does not support object-oriented type inheritance.

Is Go an object-oriented language?

Why is there no type inheritance?

A method is bound to a single specific type.

A method declaration binds an identifier to a method. The method is said to be bound to the base type and is visible only within selectors for that type.

You can convert between types T1 and T2.

A value x can be converted to type T [when] x's type and T have identical underlying types.

For example,

package main

import (
    "fmt"
)

type T1 struct{ i int }

func (t T1) String() string { return "T1" }

type T2 T1

func (t T2) String() string { return "T2" }

func main() {
    t1 := T1{1}
    t2 := T2{2}
    fmt.Println(t1, t2)
    c1 := T1(t2)
    c2 := T2(t1)
    fmt.Println(c1, c2)
    t1 = T1(c2)
    t2 = T2(c1)
    fmt.Println(t1, t2)
}

Output:
T1 T2
T1 T2
T1 T2

这篇关于如果类型 T2 基于类型 T1,是否存在任何类型的“继承"?从T1到T2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆