Skip to content

Instantly share code, notes, and snippets.

@stream-iori
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save stream-iori/63eb400743b0dbabd33a to your computer and use it in GitHub Desktop.

Select an option

Save stream-iori/63eb400743b0dbabd33a to your computer and use it in GitHub Desktop.
Java8 Default方法
interface Product {
//默认方法
default String notRequired() {
return "Default implementation";
}
String required();
//静态方法
static Product create() {
return new DefaultProduct();
}
}
//实现接口
class DefaultProduct implements Product {
@Override
public String required() {
return "requiredMethod";
}
}
//调用实例
//静态工厂
Product product = Product.create();
//直接调用接口里的默认方法
product.notRequired()
//调用必须实现的方法
product.required()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment