/**
* @author 42267
* @version
* @项目名称:oop
* @类名称:TestInterface
* @类描述: 接口和实现类
* @创建时间:2020年6月6日 下午11:03:15
*/
public class TestInterface {
public static void main(String[] args) {
Volant v = new Angel();
v.fly();
Honest ha = new Angel();
ha.helpOther();
//Volant v = new Angel();是将angel实现的多个接口中的方法强行指定为只有Volant 中的方法,也算是强转类型的一种.Honest同样是这样。
在下面的代码中就是拥有它自己的所实现的所有方法,fly()和helpOther()。而不只是单方面的东西。
/
Angel a=new Angel();
a.fly();
a.helpOther();
Honest h = new GoodMan();
h.helpOther();
Birdman bm=new Birdman();
bm.fly();
}
}
/**
* @author 42267
* @version
* @项目名称:oop
* @类名称:Volant
* @类描述: 飞行接口
* @创建时间:2020年6月6日 下午11:06:12
*/
interface Volant {
int FLY_HEIGHT = 1000;
void fly();
}
//善良接口
interface Honest {
void helpOther();
}
//
class Angel implements Volant,Honest { //实现类可以实现多个父接口!
@Override
public void helpOther() {
System.out.println("Angel.helpOther()");
}
@Override
public void fly() {
System.out.println("Angel.fly()");
}
}
///
class GoodMan implements Honest {
@Override
public void helpOther() {
System.out.println("GoodMan.helpOther()");
}
}
class Birdman implements Volant {
@Override
public void fly() {
System.out.println("Birdman.fly()");
}
}
结果
Angel.fly()
Angel.helpOther()
Angel.fly()
Angel.helpOther()
GoodMan.helpOther()
Birdman.fly()
虽然java的类只有单继承,但是
java的接口是可以多继承的。
/**
* @author 42267
* @version
* @项目名称:oop
* @类名称:TestInterface2
* @类描述:
* @创建时间:2020年6月6日 下午11:18:47
*/
public class TestInterface2 {
}
interface A {
void testa();
}
interface B {
void testb();
}
/**接口可以多继承:接口C继承接口A和B*/
interface C extends A, B {
void testc();
}
class Mysubclass implements C {
@Override
public void testa() {
}
@Override
public void testb() {
}
@Override
public void testc() {
}
}