介面的繼承實現的和定義

2021-10-01 17:41:51 字數 1556 閱讀 3156

1

package main

23 import "

fmt"

45 type humaner01 inte***ce

89 type personer inte***ce

1314 type student10 struct

1819 func (temp *student10) sayhi()

2223 func (temp *student10)sing(lrc string

) 26

27func main()

31 i =s

32i.sayhi()

33 i.sing("

人民解放歌")

3435

varh humaner01

36 h =s

37h.sayhi()

3839 h = i //

子介面可以賦值給父介面

40h.sayhi()

4142

//i = h

//父介面定義變數不能給子介面

4344 }

1

package main

23 import "

fmt"45

//定義乙個介面型別

6 type humaner inte***ce

1011 type student9 struct

1516 func (temp *student9) sayhi()

1920 type teacher struct

2425 func (temp *teacher) sayhi()

2829 type worker4 struct

3233 func (temp *worker4) sayhi()

3637

//定義乙個普通函式,函式的引數為介面型別

38//

只有乙個函式,可以表現有不同的形式,多型

39 func whosayhi(i humaner)

4243

44func main()

5051

//i =s

52//

i.sayhi()

5354 t := &teacher

55//

i = t

5657

5859 w := &worker4

60//

i= w

61//

i.sayhi()

6263

whosayhi(s)

64whosayhi(t)

65whosayhi(w)

6667

//建立乙個切片,切片放入是介面

68 x := make(humaner,3

)69 x[0] =s

70 x[1]=t

71 x[2] =w

7273

for _,i :=range x

7677

7879 }

類的繼承和介面的實現

首先是乙個 在控制台列印順序的問題,第一步要看的是從子類中找到new 例項化 首先在例項化子類的物件的時候已經例項化了父類的物件所以是從要首先呼叫 呼叫父類的物件,無論子類的new 裡面有沒有引數都是從父類開始呼叫 父類的開始的呼叫無參的方法 執行完畢後呼叫子類的方法 子類的方法要看子類中對應的引數...

關於介面的實現和繼承的問題

inte ce inte ce1 用乙個類去實現這個介面 public class class1 inte ce1 現在,class1實現了介面inte ce1,當類class1的乙個子類class2繼承class1時,子類class2自然就可以訪問helloworld 方法了。問題就出在這裡,其實...

介面的定義與實現

作用 1.約束 2.定義一些方法,讓不同的人實現 3.介面不能被例項化,介面中沒有構造方法。4.implements可以實現多個介面 5.public abstract 6.public static final 7.必須要重寫介面內的方法 8.介面就是規範 定義 public inte ce us...