显式的实现 (implements)
- interface InterfaceName
- {
- //abstract methods declaration
- }
- class ClassName implements InterfaceName
- {
- //abstract methods overwrite
- }
示例代码:
- package com.lx;
- interface Runner
- {
- public void run();
- }
- class Person implements Runner
- {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("我是人类, 我会跑.");
- }
- }
- public class DemoInterface
- {
- public static void main(String[] args) {
- Runner r=new Person();
- r.run();
- }
- }
2, 隐式的实现
- interface InterfaceName
- {
- //abstract methods declaration
- }
- InterfaceName i=new InterfaceName() {
- //abstract methods overwrite
- }
示例代码:
- package com.lx;
- interface Runner
- {
- public void run();
- }
- public class DemoInterface
- {
- public static void main(String[] args) {
- Runner r=new Runner() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("我是匿名的, 但是我会跑...");
- }
- };
- r.run();
- }
- }
来源: http://www.bubuko.com/infodetail-3274041.html