简述接口的定义和语法
与类的定义不同,定义一个接口不能使用class关键字,而是interface关键字。定义接口的基本语法如下:
[访问修饰符] interface 接口名 [extends 父接口1....父接口2....]
利用接口实现代码
package boke;
public interface tan1 {
void fiy();
}
package boke;
public class tan2 implements tan,tan1{
@Override
public void fiy() {
// TODO Auto-generated method stub
System.out.println("我是鸽子,在飞行");
}
@Override
public void getEgg() {
// TODO Auto-generated method stub
System.out.println("我是鸽子,一窝一百个蛋");
}
}
package boke;
public class tan3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
tan2 t=new tan2();
t.fiy();
t.getEgg();
}
}
package boke;
public interface tan4 {
int leg=6;
void kun();
}
package boke;
public class tan5 implements tan4,tan1{
@Override
public void fiy() {
// TODO Auto-generated method stub
System.out.println("我是蚂蚁,我能飞");
}
@Override
public void kun() {
// TODO Auto-generated method stub
System.out.println("我是蚂蚁,我能产一坨卵");
}
}
package boke;
public abstract class tan6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
tan5 t=new tan5();
t.fiy();
t.kun();
}
}