package main
//实现继承,封装,这里没有演示多态用法
import "fmt"
type Parent struct{
Name string
Age int
Address string
Sex int
}
type Child struct{
Parent //通过包含实现继承
Work string
}
type IParent interface{
Say()
Walk()
}
type IChild interface{
DoWork()
Happy()
}
/**
*定义父类中的方法
*/
func (p *Parent) Say(){ fmt.Println("调用父类的Say()方法")}
func (p *Parent) Walk(){ fmt.Println("调用父类的Walk()方法")}
/**
*定义子类中的方法
*/
func (c *Child) DoWork(){fmt.Println("调用子类的DoWork()方法")}
func (c *Child) Happy(){fmt.Println("调用子类的DoWork()方法")}
/**
*实现set,get方法
*定义接收者为指针类型,由于代码少就写成一行了
*/
func (ch *Child) setName(name string){ch.Name = name}
func (ch *Child) getName() string{ return ch.Name}
func (ch *Child) setAge(age int){ch.Age = age}
func (ch *Child) getAge()int{ return ch.Age }
func (ch *Child) setAddress(address string){ch.Address = address}
func (ch *Child) getAddress()string{ return ch.Address }
func (ch *Child) setSex(sex int){ ch.Sex = sex }
func (ch *Child) getSex() int{ return ch.Sex}
func (ch *Child) setWork(work string){ ch.Work = work}
func (ch *Child) getWork() string {return ch.Work}
func main(){
fmt.Println("测试对象方法")
child := Child{Parent:Parent{Name:"Jackm",Age:23,Address:"gdsz",Sex:1},Work:"IT"}
fmt.Println(child)
ch := Child{}
ch.setName("mfz");ch.setAge(24)
ch.setAddress("gdsz");ch.setSex(1)
ch.setWork("id-dev")
ch.Say(); ch.Walk()
ch.DoWork(); ch.Happy()
fmt.Println("Name:",ch.getName(),"Age:",ch.getAge(),"Address:",ch.getAddress(),"Sex:",ch.getSex(),"Work:",ch.getWork())
}