interface 到底是个什么东西?
这个东西和 Java 的接口似乎不同,既然结构中也能放置函数,为什么要设计 interface 这个东西? 通用数据类型 回复 1# 104359176可以理解为 C 语言中的 void
可以理解为 struct 的 type
1 => int
2 => int
struct1 => interfaceA
struct2 => interfaceA 回复 4# dorodaloo
struct 本身就是一种可定义的数据类型,为什么需要 interface.
哪些地方必须使用 interface, 不能使用 struct
既然 struct 也能定义方法,为什么还要需要 interface? 回复 5# 104359176
interface ==> class's typeclass cat
...
class dog
...
c: cat// c is cat
d: dog// d's type is dog
c, d have a different type?? cat vs dog?
If we have noah's arc
In static lang's container must be have same type! how to do that?
Give it a interface?
You'll find? cat is animal ==> interfase: Animal
You'll find? dog is animal ==> interfase: Animal
What's cat or dog's type?
OK it is Animal!
We have A train to the Zoo: (limitations: same types?)
OK: dog1, dog2,.... (dog1 is a classA, dog2 is a classA....)
NG: dog1, cat1, dog2,... different type!!
So? You'll find? tiger? Cat? Dog....they all different type?
a class is a type: cat , dog, tiger.....
but an interface A: may be ? cat, dog,...
So? interface is a class's type!!
train = cat, dog, tiger, lion, cat....
That's All Zoo = Ani1, Ani2,.... {OK} same type
but
Zoo = Cat1, Dog1, Tiger1....{Sorry!! different Type!}
YOU'LL know? WHAT iS an Interface!!:mrgreen: 不太明白。难道 interface 是 struct 更高层次的抽象? 回复 8# 104359176
yes, it is. interface ===> class's type
hello_kitty: Cat (type: Cat)
snoopy: Dog (type: Dog)
Boxes = [ hello_kityy, Snoopy ]
Sorry: differnnt type ==> hello_kity, snoopy?
but Boxes = # OK same type all Animal
interface is:
class Cat: Animal
...
class Dog: Animal
...
hello_kitty: Animal = "hello kitty"
snoopy: Animal = "snoopy"
BOxes = #ok, SAME TYPE
var has a type => some class
some class => has a type ==> some interface!!
回复 9# dorodaloo
Go does not have class.
页:
[1]
2