Last active
August 29, 2015 14:24
-
-
Save stream-iori/63eb400743b0dbabd33a to your computer and use it in GitHub Desktop.
Java8 Default方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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