go入门篇(3)
一、面向对象
1.1 抽象
将一类对象的共有属性和行为抽取出来形成一个物理模型
1.2 封装
将抽查的对象的字段和行为隐藏在对象的每部,只给外部提供特定的访问接口
1.3 封装的实现
-
对属性的封装
属性的开头字母小写,只有本包才可以访问的到 -
对结构体的封装
将结构体的首字母小写 -
结构体所在的包提供一个工程模式的函数,首字母大写,类似构造函数
-
首字母大写的Set方法
-
首字母大写的Get方法
1.3.1 封装的例子
- src/model/student.go
type Student struct{
name string
age int
address string
}
func CreateStudent() Student{
var stu Student = Student{}
return stu
}
func (stu *Student)GetName()string{
fmt.Println(stu.name)
return stu.name
}
func (stu *Student)SetName(name string){
stu.name = name
fmt.Println(stu.name)
}
- src/view/test.go
二、 继承
2.1 嵌套匿名结构体
type Student struct {
Name string
Age int
}
type Teacher struct{
Student
}
2.2 匿名结构体的使用
type Student struct {
Name string
Age int
}
type Teacher struct{
//通过匿名结构体的方式来得到继承的效果
Student
Address string
}
func (stu *Student) GetName() string{
fmt.Println(stu.Name)
return stu.Name
}
type Stu Student
func main(){
//老师对象的使用
var tea *Teacher = &Teacher{}
tea.Student.Name = "json"
tea.Address = "西安"
tea.Student.GetName()
//学生对象的使用
var stu *Student = &Student{}
stu.Name = "rose"
stu.GetName()
}
结果:
json
rose
2.3 结构体与匿名结构体有相同的字段,且匿名结构体本身的方法调用了自身的属性
func (stu *Student) GetName() string{
if stu.Name == ""{
fmt.Println("stu的name是空的")
}
return stu.Name
}
如果没有进行匿名结构体字段的赋值,输出就为空的
type Student struct {
Name string
Age int
}
type Teacher struct{
//通过匿名结构体的方式来得到继承的效果
Student
Name string
Address string
}
func (stu *Student) GetName() string{
if stu.Name == ""{
fmt.Println("stu的name是空的")
}
return stu.Name
}
type Stu Student
func main(){
var tea *Teacher = &Teacher{}
tea.Name = "json"
tea.GetName()
}
2.4 嵌套有名结构体
嵌套有名结构体称为组合
2.5 匿名结构体嵌入基本类型
type Teacher struct{
int
//通过匿名结构体的方式来得到继承的效果
}
func main(){
var tea *Teacher = &Teacher{}
tea.int = 10
fmt.Println(tea.int)
}
2.6 有名结构体的特性
有名结构体和继承没有关系,不会往上找
2.7 有名结构体的举例
type Student struct {
Name string
}
type Teacher struct{
//通过匿名结构体的方式来得到继承的效果
stu Student
}
func main(){
var tea *Teacher = &Teacher{}
tea.stu.Name = "json"
fmt.Println(tea.stu.Name)
}
2.8 多重继承
type A struct{
Name string
}
type B struct{
A
}
type C struct{
B
}
func main(){
var b B = B{}
b.Name = "jack"
fmt.
三、多态
3.1 接口案例
type Usb interface {
insert()
}
type Computer struct{
}
type Phone struct{
}
type Mac struct {
}
func (com Computer) insert(){
fmt.Println("computer插入了接口")
}
func (pho Phone) insert(){
fmt.Println("phone插入了接口")
}
func (mac Mac) working(usb Usb){
usb.insert()
}
func main(){
var mac Mac = Mac{}
var com Computer = Computer{}
var pho Phone = Phone{}
mac.working(com)
mac.working(pho)
}
3.2 接口特性
- 实现接口必须实现接口中的所有方法。
3.3 接口案例
- 实现sort.Sort(int Interface)中的Interface接口,实现对结构体的排序
import(
"fmt"
"math/rand"
"sort"
)
type Student struct{
Name string
Age int
}
type StuList []Student
func (stuList StuList) Len() int{
return len(stuList)
}
func (stulist StuList) Less(i, j int) bool{
return stulist[i].Age > stulist[j].Age
}
func (stulist StuList) Swap(i, j int){
stulist[i], stulist[j] = stulist[j], stulist[i]
}
func main(){
var stulist StuList
for i:=0; i<10; i++{
var student = Student{}
student.Age = rand.Intn(20)
student.Name = "jack" + string(i)
stulist = append(stulist, student)
}
sort.Sort(stulist)
fmt.Println(len(stulist))
for _, v := range stulist{
fmt.Println(v)
}
}
3.4 接口和继承之间的关系
- 接口可以看作是继承的补充
比如相关给🐖类添加飞翔的行为,如果直接通过继承来实现的话,就会破坏继承原有的结构,因为🐖类会飞不科学,这样的话就可以通过接口来实现🐖会飞的功能。
四、多态
4.1 接口实现多态的两种方式
- 多态参数
- 多态数组
五、类型断言
5.1 类型断言举例
type Stu struct {
a int
}
func main(){
var test interface{}
var stu Stu = Stu{
a : 32,
}
test = stu
var stu2 Stu
//此处使用了类型断言
stu2 = test.(Stu)
5.2 用类型断言实现对数据类型的判断
- interface{}可以接受任意类型的数据
func judgeType(args ...interface{}){
for _,x := range args{
switch x.(type) {
case int:
fmt.Println("int")
case float64:
fmt.Println("float")
case string:
fmt.Println("string")
case bool:
fmt.Println("bool")
default:
fmt.Println("default")
}
}
}
func main(){
//编写函数,判断传入参数的类型
judgeType(23,"wewe",23.23,1.2)
}