您的位置:首页 > 编程语言 > Java开发

java笔记--day09--接口(三)类和接口的关系

2016-08-27 21:08 309 查看
类与类

只支持单继承,但可以多层继承

类与接口

实现关系,可以单实现,也可以多实现。还可以在继承一个类的同时实现多个接口

接口与接口

继承关系,可以单继承,也可以多继承

代码Demo:

interface Father{
public abstract  void show1();
}

interface Mother{
public abstract void show2();
}

interface Brother extends Father,Mother {
}

class son2 implements Father,Mother{
public  void show1(){
System.out.println("Son2 love father.");
}
public void show2(){
System.out.println("Son2 love mother.");
}
}

class son3 implements Brother{
public  void show1(){
System.out.println("Son3 love father.");
}
public  void show2(){
System.out.println("Son3 love mother.");
}
}

class InterfaceDemo2{
public static void main(String[] args) {
Father f = new son2();
f.show1();
//f.show2();    //wrong.

Mother m = new son2();
//m.show1();    //wrong.
m.show2();
System.out.println();

Brother b = new son3();
b.show1();
b.show2();
}
}

/*
running result:
Son2 love father.
Son2 love mother.

Son3 love father.
Son3 love mother.
*/


代码总结

在上述代码中,需要注意接口多态也是多态,多态的弊端也会有(代码的第35行和第38行),所以这种情况下可以新建一个brother接口来满足需要。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: