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

java--接口(interface\implements)

2017-07-18 16:31 344 查看
/**
* 接口:
* 特殊的抽象类,只包含抽象方法
* 由interface定义
* 只能包含常量和抽象方法
*接口不能被实例化
*接口需要被实现/继承的,实现类/子类;必须重写接口里面的所有抽象方法、
*一个类可以实现多个接口,但是必须重写所有抽象方法
*若又继承又实现,必须先继承后实现
*接口可以继承接口,抽象类可以实现接口
*
*接口可以作为类型声明变量,一个接口类型的变量可以引用实现了该接口的实现类的对象,变量可以调用该接口中的方法(实现类提供方法的具体实现)
*extends---同类型的                              类和类  接口和接口
*implements---不同类型的                    类和接口
*
*/
public class interfanceDemo {
public static void main(String[] args) {
Inter2 ob=new Boo();//向上造型
}
}

interface Inter1{
public static final int NULL=34;//常量
public abstract void test();//抽象方法
double d=1.2;//默认添加public static final
//  double dd;//错误,常量声明时同时初始化
void tess();//默认添加public abstract
}
interface Inter2 {
void tess();
}
class Foo {

}
class Ao extends Foo implements Inter1,Inter2{
public void test() {//必须用public 修饰

}
public void tess(){//必须用public 修饰

}
}
class Boo implements Inter2 {

@Override
public void tess() {
// TODO Auto-generated method stub

}

}


子接口与父接口:

public class test1 {
public static void main(String[] args) {
Inter5 aa=new AAo();//向上造型 子接口
aa.tee();//访问父接口、子接口里面抽象方法
aa.test();
aa.tee(10);
aa.show();
Inter3 aa1=new AAo();//向上造型 父接口
aa1.test();//只能访问父接口里面的抽象方法
aa1.show();
}
}
interface Inter3 {//父接口
public static final int NULL = 12;
public void show();
int test();
}
interface Inter5 extends Inter3 {//子接口
int test();
void tee(int a);
void tee();
}
class AAo implements Inter5 {//实现子接口
public void show() {}
public int test() {return 0;}
public void tee(int a) {}
public void tee() {}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 接口
相关文章推荐