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之旅(七)——final关键字 , 抽象类abstract,模板方法模式,接口interface,implements,特点,扩展
- Java面向对象高级--接口的基本概念--------interface、implements
- JAVA之旅(七)——final关键字 , 抽象类abstract,模板方法模式,接口interface,implements,特点,扩展
- java入门学习(11)—接口定义(interface)及使用(implements)
- Java中接口interface和实现implements问题
- [转] Java接口_interface_implements
- java接口的基本概述,interface与implements
- Java 学习笔记 (3) -抽象类 abstract 接口 interface 内部类 inner class 继承 extends 实现 implements 包 package
- java基础13 接口(及关键字:interface、implements)
- JAVA 接口 Interface 学习笔记 implements
- 【Java面试题】13 Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
- JAVA 构造器, extends[继承], implements[实现], Interface[接口], reflect[反射], clone[克隆], final, static, abstrac
- [原]Java思想之容器implements与接口interface
- JAVA 接口 Interface 学习笔记 implements
- JAVA8:abstract抽象类与抽象函数以及interface接口的解释
- 【Java基础】——之抽象类(Abstract)与接口(Interface)
- java例程练习(接口interface)
- Java 接口(interface)的用途和好处
- 源程序名Interfacetest.java,是接口的例子
- 关于jndi(Java Naming and Directory Interface)java的命名与目录实例的接口