显式的实现(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();
}
}
原文:https://www.cnblogs.com/lrzb/p/11799901.html