//面对对象中:方法也是函数就是read和 write 主函数中就是搞对象要方法
//接口是一种规范与标准,接口中只有方法的声明,没有方法的实现,接口不是类,类是数据类型,里面存放的是属性. 接口命名习惯1是在单词后面加”er”,2以”i”开头.
//接口就是开始时把方法声明一下(声明那个后面主函数就用那个,可以使用父类继承 使主函数可全部用), 主函数时声明一个接口变量,把声明好了的对象地址赋值个接口,再用接口配合方法来调用函数
传值不要 " * " 传址要用 " * " 和 " & " *和&是配合使用的
例1. 传值 只能调用
type A interface { //A接口
read()
}
type B interface { //B接口
write()
}
type Student struct { //定义类
}
func(Student) read(){
fmt.Println("实现了A接口的read()方法")
}
func(Student) write(){
fmt.Println("实现了B接口的write()方法")
}
func main() {
stu:=Student{} //实例化对象
var a A
var b B
a=stu
b=stu
a.read() //实现了A接口的read()方法
b.write() //实现了B接口的write()方法
例2:传址 可以改变方法里面的值 使用 * 和 & 两件套
package main
import "fmt"
type A interface {
read()
}
type B interface {
write()
}
type Student struct {
}
func (*Student)read(){
fmt.Println("实现了A接口的read()方法")
}
func (*Student)write(){
fmt.Println("实现了B接口的write()方法")
}
func main() {
stu:=Student{}
var a A
var b B
a=&stu
b=&stu
a.read() //实现了A接口的read()方法
b.write() //实现了B接口的write()方法
}