个人观点,仅供参考: 接口是比抽象类更抽象的类,最常用的就是将功能与实现相分离。比如jdbc,sun只定义了一些接口,而数据库厂家来实现它。我们在调用的时候,不管是哪个厂家实现的,只需要对接口进行操作就可以了。在下面的例子中test类的方法printName对testInterface进行操作,所以只要实现了testInterface的类都可以带进去。 public interface testInterface { String getName(); } public class testClass implements testInterface { public String getName() { return "testClass"; } }public class test {
public void printName(testInterface t) { System.out.println(t.getName()); } public static void main(String[] args) {
testClass _testClass = new testClass(); test _test = new test(); _test.printName(_testClass); } }
接口是比抽象类更抽象的类,最常用的就是将功能与实现相分离。比如jdbc,sun只定义了一些接口,而数据库厂家来实现它。我们在调用的时候,不管是哪个厂家实现的,只需要对接口进行操作就可以了。在下面的例子中test类的方法printName对testInterface进行操作,所以只要实现了testInterface的类都可以带进去。
public interface testInterface {
String getName();
}
public class testClass implements testInterface {
public String getName() {
return "testClass";
}
}public class test
{
public void printName(testInterface t)
{
System.out.println(t.getName());
} public static void main(String[] args) {
testClass _testClass = new testClass();
test _test = new test();
_test.printName(_testClass);
}
}
21cnfox