Java语言是面向对象的:Java语言提供类、接口和继承等原语,为了简单起见,只支持类之间的单继承,但支持接口之间的多继承,并支持类与接口之间的实现机制(关键字为implements)
对于类之间的单继承我们很好理解,是为了避免子类被引用的时候同一个方法无法判断应该使用哪个父类的方法,如类One同时继承了类Two和类Three,类Two和类Three中都有printStr方法,当调用pringStr方法的时候,就无法选择使用哪个类的,编译器直接报错!可以看以下例子:
public class Two implements InterfaceTwo,InterfaceThree{
public void printStr(){
System.out.println("Print by Class Two");
}
@Override
public void printStrByInterface() {
// TODO 自动生成的方法存根
System.out.println("Print by IntercaseTwo Two");
}
}
public class Three {
public void printStr(){
System.out.println("Print by Class Three");
}
}
//编译器报错error:标记“,”上有语法错误,应为 .
public class One extends Two,Three {
}
所以对于类的继承extends来说只能是单继承的。
但是接口之间确实多继承的,难道接口没有上面提到的类继承的那些问题吗?这里需要重温一下接口的特点,如知识点二。接口都是抽象的,他下面的方法也必须全部是抽象方法,没有任何的具体实现,即使继承的两个父接口包含同样的方法也没有任何影响,可以看下面的例子:InterfaceOne继承了InterfaceTwo和InterfaceThree,同时InterfaceTwo和InterfaceThree中都定义了抽象方法printStrByInterface:
package interfaceMutilInherit;
public interface InterfaceOne extends InterfaceTwo,InterfaceThree{
}
package interfaceMutilInherit;
public interface InterfaceTwo {
public void printStrByInterface();
}
package interfaceMutilInherit;
public interface InterfaceThree {
public void printStrByInterface();
}
以上代码完全没有问题,正常编译通过。
接下来看看测试例子,测试接口以及把Three类实现InterfaceThree。完整测试
//类One集成类Two
public class One extends Two{
}
//类Two实现接口InterfaceTwo和InterfaceThree
public class Two implements InterfaceTwo,InterfaceThree{
public void printStr(){
System.out.println("Print by Class Two");
}
/* (非 Javadoc)
* @see interfaceMutilInherit.InterfaceTwo#printStrByInterface()
*/
@Override
public void printStrByInterface() {
// TODO 自动生成的方法存根
System.out.println("Print by IntercaseTwo Two");
}
}
//类Three仅实现接口InterfaceThree
public class Three implements InterfaceThree{
public void printStr(){
System.out.println("Print by Class Three");
}
/* (非 Javadoc)
* @see interfaceMutilInherit.InterfaceThree#printStrByInterface()
*/
@Override
public void printStrByInterface() {
// TODO 自动生成的方法存根
System.out.println("Print by Interface Three");
}
}
//接口InterfaceTwo
public interface InterfaceTwo {
public void printStrByInterface();
}
//接口InterfaceThree
public interface InterfaceThree {
public void printStrByInterface();
}
//测试类TestOneTwoThree
public class TestOneTwoThree {
public static void main(String[] args){
One one = new One();
one.printStr();
InterfaceTwo inter = new Two();
inter.printStrByInterface();
InterfaceThree inter3 = new Three();
inter3.printStrByInterface();
}
}
得到的结果是:
Print by Class Two
Print by IntercaseTwo Two
Print by Interface Three
知识点一:
接口的特点
1、Java接口中的成员变量默认都是public,static,final类型的(都可省略),必须被显示初始化,即接口中的成员变量为常量(大写,单词之间用"_"分隔)
2、Java接口中的方法默认都是public,abstract类型的(都可省略),没有方法体,不能被实例化
3、Java接口中只能包含public,static,final类型的成员变量和public,abstract类型的成员方法
4、接口中没有构造方法,不能被实例化
5、一个接口不能实现(implements)另一个接口,但它可以继承多个其它的接口
6、Java接口必须通过类来实现它的抽象方法
7、当类实现了某个Java接口时,它必须实现接口中的所有抽象方法,否则这个类必须声明为抽象类
8、不允许创建接口的实例(实例化),但允许定义接口类型的引用变量,该引用变量引用实现了这个接口的类的实例
9、一个类只能继承一个直接的父类,但可以实现多个接口,间接的实现了多继承
知识点二:
关于接口实现类的初始化,一般接口xxinterface,他的实现类xximpl,要用实现类不是xximpl xx=new xximpl(),而是xxinterface xx=new xximpl(),好比例子中
public class TestOneTwoThree {
public static void main(String[] args){
One one = new One();
one.printStr();
InterfaceTwo inter = new Two();
inter.printStrByInterface();
}
}