在java中类无法实现多继承,接口的方法可以很好弥补这一点。
接口的特点:
1.接口可以看着是特殊的抽象类( 接口中自由抽象方法 );
2.接口和类可以多实现,示例代码:
//接口1
interface Inter1{
void method1();//自动拼接 abstract public ,为抽象方法
}
//接口2
interface Inter2{
void method2();//自动拼接 abstract public ,为抽象方法
}
//类实现接口1,接口2的方法
class Son implements Inter1,Inter2{
public void method1() {
System.out.println("method1...");
}
public void method2() {
System.out.println("method2...");
}
}
3.接口之间可以实现多继承,示例代码:
interface Inter1{
void crawl();
}
interface Inter2{
void swim();
}
interface Eyu extends Inter1,Inter2{
public void crawl() ;//鳄鱼获得爬行方法
public void swim() ;//鳄鱼获得游泳方法
}
#–end–#