具体代码
package main import "fmt" func main() { d := new(DealImplement) d.Handle(&User{Name: "hello"}) d.Handle(&School{SchoolName: "work"}) } type Deal interface { Handle(params Param) } type DealImplement struct { } func (d *DealImplement) Handle(params Param) { fmt.Println(params.String()) } type Param interface { String() string } type User struct { Name string } func (u *User) String() string { return "hello" } type School struct { SchoolName string } func (u *School) String() string { return "hello" }
表面上调用的Deal函数
实际上执行了子类的方法
与java函数中的范型一样,可以设定参数类型 T 必须是 Comparable的实现
public static <T extends Comparable<T>> T maximum(T x, T y, T z){ T max = x; // 假设x是初始最大值 if ( y.compareTo( max ) > 0 ){ max = y; //y 更大 } if ( z.compareTo( max ) > 0 ){ max = z; // 现在 z 更大 } return max; // 返回最大对象 }
标签:Handle,string,type,params,func,max,写法,any From: https://www.cnblogs.com/xuweiqiang/p/16990432.html