package main
import "fmt"
type Book struct{
BookId string
Name string
Price float64
Author string
Borrowable bool
}
type Reader struct {
ReaderID string
Balance float64
}
func (r *Reader)BorrowBook(b *Book) {
if b.Borrowable{
b.Borrowable = false
fmt.Printf("读者%s借阅了%s\n",r.ReaderID,b.Name)
}else {
fmt.Printf("%s借阅失败,该书已借出", b.Name)
}
}
func (r *Reader)ReturnBook(b *Book) {
fmt.Printf("读者%s归还了%s\n",r.ReaderID,b.Name)
}
func (r *Reader)PayPenalty(amount float64) {
r.Balance-=amount
fmt.Printf("%s交纳了罚金%.2f元,余额是%.2f元\n",r.ReaderID,amount,r.Balance)
}
type Student struct {
Reader
Name string
Major string
}
func (s *Student) Study () {
fmt.Printf("%s正在学习\n",s.Name)
}
type Teacher struct {
Reader
Name string
Course string
}
func (t *Teacher) Teach () {
fmt.Printf("%s正在教授%s\n",t.Name,t.Course)
}
func (t *Teacher)PayPenalty(amount float64) {
fmt.Printf("%s交纳了罚金%.2f元,余额是%.2f元\n",t.ReaderID,amount,t.Balance)
}
func main() {
b1 := Book{}
b1.Name="银瓶梅"
b1.Author="欧阳搏达"
b1.Price=999.9
b1.Borrowable=true
b2 := Book{"10001","奥力给",998.0,"yangge",true}
b3 := Book{"10002", "黄金时代", 100.0, "王小波",true}
b4Ptr := new(Book)
b4Ptr.Name="蓝楼梦"
b4Ptr.Author="欧阳搏达"
b4Ptr.Price=9.9
b4Ptr.Borrowable=true
fmt.Println(b1,b2,b3,b4Ptr)
s1 := Student{
Reader: Reader{"001", 100},
Name: "狗蛋",
Major: "Python",
}
fmt.Printf("%v\n",s1)
fmt.Printf("%+v\n",s1)
fmt.Printf("%#v\n",s1)
t1 := Teacher{
Reader: Reader{"002", 0},
Name: "老王",
Course: "撩妹秘籍",
}
fmt.Printf("%v\n",t1)
fmt.Printf("%+v\n",t1)
fmt.Printf("%#v\n",t1)
t1.Teach()
s1.Study()
t1.BorrowBook(&b1)
t1.BorrowBook(b4Ptr)
s1.BorrowBook(&b3)
s1.BorrowBook(&b2)
t1.ReturnBook(&b1)
s1.ReturnBook(&b2)
t1.ReturnBook(b4Ptr)
s1.PayPenalty(5)
}