2017-07-03 42 views
-4

我必須解決以下TreeHouse Go挑戰,但我被卡在Print方法中。您如何解決以下Golang挑戰?

要求:

clockcalendar包,我們定義ClockCalendar類型,這兩者有一個Display方法,你可以調用打印出來。 在schedule包中,定義了Displayable接口,該接口在ClockCalendar類型上均由Display方法滿足。 (請不要對clock或包進行任何更改。)然後,仍在schedule包中,定義一個Print函數,其值爲Displayable,並調用Display

clock.go:

package clock 

import "fmt" 

type Clock struct { 
    Hours int 
    Minutes int 
} 

func (c Clock) Display() { 
    fmt.Printf("%02d:%02d", c.Hours, c.Minutes) 
} 

calendar.go:

package calendar 

import "fmt" 

type Calendar struct { 
    Year int 
    Month int 
    Day int 
} 

func (c Calendar) Display() { 
    fmt.Printf("%04d-%02d-%02d", c.Year, c.Month, c.Day) 
} 

schedule.go:

package schedule 

// DECLARE A Displayable INTERFACE HERE 
type Displayable interface { 
    Display() 
} 

// DECLARE A Print FUNCTION HERE (I'm stuck here) 

謝謝!

回答

2
func Print(d Displayable) { 
    d.Display() 
} 
+0

我曾嘗試過,但它沒有工作,但現在當我複製並粘貼它,它的工作。可能是有一個錯字。 謝謝! –