Go ���Բ���һ�� ����ͳ�� ��������������ԣ�������û����ͼ̳еĸ��
���� Go �������зdz����� �ӿ� ���ͨ��������ʵ�ֺܶ������������ԡ��ӿ��ṩ��һ�ַ�ʽ�� ˵�� �������Ϊ�����˭�ܸ㶨����£����Ϳ������������
�ӿڶ�����һ�鷽��������������������Щ������������ʵ�֣����룺����û�б�ʵ�֣������dz���ģ����ӿ���Ҳ���ܰ���������
ͨ�����¸�ʽ����ӿڣ�
type Namer interface {
Method1(param_list) return_type
Method2(param_list) return_type
...
}
����� Namer ��һ�� �ӿ�������
������Լ����ֻ����һ�������ģ��ӿڵ������ɷ������� [e]r ����ɣ����� Printer��Reader��Writer��Logger��Converter�ȵȡ�����һЩ�����õķ�ʽ������er������ʱ��������Recoverable����ʱ�ӿ�����able��β�������� I ��ͷ���� .NET �� Java ����������
Go �����еĽӿڶ��ܼ�̣�ͨ�����ǻ����0�������3��������
��������������������ԣ��� Go �����нӿڿ�����ֵ��һ���ӿ����͵ı�����һ�� �ӿ�ֵ ��var ai Namer��ai��һ�����֣�multiword�����ݽṹ������ֵ�� nil������������һ��ָ�룬��Ȼ����ȫ��һ���¡�ָ��ӿ�ֵ��ָ���ǷǷ��ģ����Dz���һ����Ҳû�У����ᵼ�´������
�˴��ķ���ָ�����ͨ������ʱ�������������ġ�
���ͣ�����ṹ�壩ʵ�ֽӿڷ������еķ�����ÿһ��������ʵ��˵���˴˷�������������ڸ����͵ģ���ʵ�ֽӿ���ͬʱ������Ҳ�����˸����͵Ľӿڡ�ʵ���� Namer �ӿ����͵ı������Ը�ֵ�� ai ��������ֵ������ʱ�������е�ָ���ָ��ʵ�ֵĽӿڷ�������Ȼ�����һ�����ͣ�Ҳʵ���˸ýӿڣ��ı�������ֵ��ai������ߣ�����ע��ָ��ͷ���ʵ�֣�Ҳ����֮�ı䡣
���Ͳ���Ҫ��ʽ������ʵ����ij���ӿڣ��ӿڱ���ʽ��ʵ�֡�������Ϳ���ʵ��ͬһ���ӿ���
ʵ��ij���ӿڵ����ͣ�����ʵ�ֽӿڷ����⣩�����������ķ�����
һ�����Ϳ���ʵ�ֶ���ӿ���
�ӿ����Ϳ�����һ��ʵ�������ã� ��ʵ��������ʵ���˴˽ӿڣ��ӿ��Ƕ�̬���ͣ���
��ʹ�ӿ�������֮��Ŷ��壬���ߴ��ڲ�ͬ�İ��У����������룺ֻҪ����ʵ���˽ӿ��еķ���������ʵ���˴˽ӿڡ�
������Щ����ʹ�ýӿھ��кܴ������ԡ�
��һ�����ӣ�
ʾ�� 11.1 interfaces.go��
package main
import "fmt"
type Shaper interface {
Area() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
func main() {
sq1 := new(Square)
sq1.side = 5
// var areaIntf Shaper
// areaIntf = sq1
// shorter,without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
areaIntf := sq1
fmt.Printf("The square has area: %f\n", areaIntf.Area())
}
�����
The square has area: 25.000000
����ij�������һ���ṹ�� Square ��һ���ӿ� Shaper���ӿ���һ������ Area()��
�� main() �����д�����һ�� Square ��ʵ��������������߶�����һ�������������� Square������ Area()���������������ε�������ṹ�� Square ʵ���˽ӿ� Shaper ��
���Կ��Խ�һ�� Square ���͵ı�����ֵ��һ���ӿ����͵ı�����areaIntf = sq1 ��
���ڽӿڱ�������һ��ָ�� Square ���������ã�ͨ�������Ե��� Square �ϵķ��� Area()����ȻҲ����ֱ���� Square ��ʵ���ϵ��ô˷����������ڽӿ�ʵ���ϵ��ô˷����������˷ܣ���ʹ�˷���������һ���ԡ��ӿڱ���������˽�����ʵ����ֵ��ָ���Ӧ��������ָ�롣
���� ��̬ �� Go �汾����̬�������������һ����Ϊ��֪�ĸ�����ݵ�ǰ������ѡ����ȷ�ķ���������˵��ͬһ�������ڲ�ͬ��ʵ�����ƺ����ֳ���ͬ����Ϊ��
��� Square û��ʵ�� Area() ������������������������Ĵ�����Ϣ��
cannot use sq1 (type *Square) as type Shaper in assignment:
*Square does not implement Shaper (missing Area method)
��� Shaper ������һ������ Perimeter()������Square û��ʵ��������ʹû������ Square ʵ���ϵ������������������Ҳ���������ͬ���Ĵ���
��չһ����������ӣ����� Rectangle Ҳʵ���� Shaper �ӿڡ����Ŵ���һ�� Shaper ���͵����飬��������ÿһ��Ԫ�ز���������� Area() �������Դ���չʾ��̬��Ϊ��
ʾ�� 11.2 interfaces_poly.go��
package main
import "fmt"
type Shaper interface {
Area() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
type Rectangle struct {
length, width float32
}
func (r Rectangle) Area() float32 {
return r.length * r.width
}
func main() {
r := Rectangle{5, 3} // Area() of Rectangle needs a value
q := &Square{5} // Area() of Square needs a pointer
// shapes := []Shaper{Shaper(r), Shaper(q)}
// or shorter
shapes := []Shaper{r, q}
fmt.Println("Looping through shapes for area ...")
for n, _ := range shapes {
fmt.Println("Shape details: ", shapes[n])
fmt.Println("Area of this shape is: ", shapes[n].Area())
}
}
�����
Looping through shapes for area ...
Shape details: {5 3}
Area of this shape is: 15
Shape details: &{5}
Area of this shape is: 25
�ڵ��� shapes[n].Area()) ���ʱ��ֻ֪�� shapes[n] ��һ�� Shaper ���������ҡ��һ���Ϊ��һ�� Square �� Rectangle �����ұ��ֳ������Ӧ����Ϊ��
Ҳ�������ڿ�ʼ�㽫����ͨ���ӿ���β��� ���ɾ������� �� ��������չ�� �Ĵ��롣�� 11.12.3 �н������ڿ�����Ϊ���������µĽӿ��Ƕ�ô�����ס�
������һ������������ӣ����������� stockPosition �� car�����Ƕ���һ�� getValue() ���������ǿ��Զ���һ�����д˷����Ľӿ� valuable�����Ŷ���һ��ʹ�� valuable������Ϊ�����ĺ��� showValue()������ʵ���� valuable �ӿڵ����Ͷ����������������
ʾ�� 11.3 valuable.go��
package main
import "fmt"
type stockPosition struct {
ticker string
sharePrice float32
count float32
}
/* method to determine the value of a stock position */
func (s stockPosition) getValue() float32 {
return s.sharePrice * s.count
}
type car struct {
make string
model string
price float32
}
/* method to determine the value of a car */
func (c car) getValue() float32 {
return c.price
}
/* contract that defines different things that have value */
type valuable interface {
getValue() float32
}
func showValue(asset valuable) {
fmt.Printf("Value of the asset is %f\n", asset.getValue())
}
func main() {
var o valuable = stockPosition{"GOOG", 577.20, 4}
showValue(o)
o = car{"BMW", "M3", 66500}
showValue(o)
}
�����
Value of the asset is 2308.800049
Value of the asset is 66500.000000
һ�����������
io������һ���ӿ����� Reader:
type Reader interface {
Read(p []byte) (n int, err error)
}
������� r��var r io.Reader
��ô�Ϳ���д���µĴ��룺
var r io.Reader
r = os.Stdin // see 12.1
r = bufio.NewReader(r)
r = new(bytes.Buffer)
f,_ := os.Open("test.txt")
r = bufio.NewReader(f)
���� r �ұߵ����Ͷ�ʵ���� Read() ��������������ͬ�ķ���ǩ����r �ľ�̬������ io.Reader��
��ע
�е�ʱ��Ҳ����һ������ͬ�ķ�ʽ��ʹ�ýӿ�����ʣ���ij�����͵ĽǶ����������Ľӿ�ָ���ǣ��������е���������ֻ����û����ʽ��Ϊ��Щ�����������ⶨһ���ӿڶ��ѡ�
��ϰ 11.1��simple_interface.go
����һ���ӿ� Simpler������һ�� Get() ������һ�� Set()��Get()����һ������ֵ��Set() ��һ�����Ͳ���������һ���ṹ������ Simple ʵ������ӿڡ�
���Ŷ�һ������������һ�� Simpler ���͵IJ��������ò����� Get() �� Set() �������� main �������������������������Ƿ������ȷ���С�
��ϰ 11.2��interfaces_poly2.go
a) ��չ interfaces_poly.go �е����ӣ�����һ�� Circle ����
b) ʹ��һ���������� Shape��û���ֶΣ� ʵ��ͬ���Ĺ��ܣ���ʵ�ֽӿ� Shaper��Ȼ����������������Ƕ�����͡���չ 10.6.5 �е�������˵����д��