Java里的接口的interface 简单介绍.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java里的接口的interface 简单介绍.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6153字,纯文字阅读大概需要9分钟。
内容图文
这是写给我自己和我这种初学者看的.
Java作为1个强面向对象语言, 基本上所有东西(成员和方法)都是写在class(类)里面的.
但是也存在一种与class平行的东西, 它就是interface , 中文译成接口. 本屌能力有限在这里这能写写Interface的一些语法规则与存在意义.
一, 什么是接口
Java里的接口, 就是抽象方法和常量值的集合.
这里的定义相当表面, 就如java里的类是成员和方法的集合一样.
作为1个初学者, 可以简单理解为接口就是一种特殊的抽象类. 区别下面会详细提到.
二, Java里接口的定义和语法
interface 的定义跟java有点类似. 大概如下:[public] interace interface_name [extends <Superinterface_list>]{
//public static final members
.....................
//public abstract methods
.......
}
下面是1个简单的例子:
interface It_1{ int i = 10; //public static final //int j; //error all the members in a interface must be a const member //& need assignment //public abstract It_1(); //error, must without a constructor void print(); //public abstract }
上面就简单定义了一个接口It_1. 接下来讲下接口定义的一些关键点:
2.1 关于接口前面的修饰符(这里只讨论外部接口)
外部interface前面的修饰符有两种:
2.1.1 public修饰接口
2.1.2 没有任何修饰词修饰接口
如果1个接口不是在同名字的java文件内定义的. 那么这个接口前面不能带有任何权限修饰符, 就如上面的例子. 关键字前面没有权限修饰符.如果架上public 则通不过编译.
这种情况下, 这个接口只能被同1个包内的类访问. 其他类不能访问.
2.1.3 不能用protected ,private 来修饰外部接口
2.1 接口里所有成员都是公共静态常变量成员.
例如上面例子中的
int i = 10; 就相当于 public static final int i =10;
但是你写成
static final int i =10;
final int i = 10;
都是可以通过编译的, 但是i这个成员实际上还是 public static final的, 性质没变.
但是不能用protected 或 private 来修饰成员, 否则编译失败.
2.2 接口里所有方法都是公共抽象方法
2.3 接口里不能定义构造方法
三, 接口的实现和继承
3.1 类可以实现接口
class It_class1 implements It_1{ //implement an interface void f(){ //i = 10; error cannot do assignment for a const member int j = this.i; } //void print(){ the signal of authority cannot smaller than super class‘s(interface) public void print(){ // overwrite an the abstract method. } }
这个类就实现了上面的接口It_1. 接下来有几点要注意的.
3.1.1 1个类要实现1个接口, 关键字是implements 而不是extends
3.1.2 类里面不能为接口的成员赋值
类继承1个接口的话, 可以使用接口的成员, 但是不能为它们赋值, 因为接口的成员都是常量.3.1.3 如果1个非抽象类继承1个接口, 则必须重写接口里的所有方法.
3.2 接口的实现(implement)可以多态
public class Interface_1{ public static void f(){ It_1 a = new It_class1(); // Poly a.print(); } }
3.3 接口可以继承接口
package Interface_kng.Extend; interface Itf_1{ int i = 1; void f(); } interface Itf_3 extends Itf_1{ int j = 3; } class Itf_class1 implements Itf_3{ public void f(){ //overwrite System.out.printf("i is %d, j is %d\n", this.i, this.j); } } public class Interface_3{ public static void f(){ Itf_class1 a = new Itf_class1(); a.f(); } }
3.4 1个接口可以同时继承多个接口
interface Itf_1{ int i = 1; void f(); } interface Itf_2{ int k = 2; void g(); } interface Itf_3 extends Itf_1, Itf_2{ int j = 3; } class Itf_class2 implements Itf_3{ public void f(){ //overwrite System.out.printf("i is %d, k is %d, j is %d\n", this.i, this.k, this.j); } public void g(){ } } public class Interface_3{ public static void f(){ Itf_class2 a = new Itf_class2(); a.f(); } }
上面的接口Itf_3 同时继承了接口Itf_1 和 Itf_2. 所以Itf_3 同时具有了两者的成员和方法
3.2.1 假如1个接口A 继承另外两个接口B和C, 但是接口B,C 有相同名字的成员i
interface Itf_1{ int i = 1; void f(); } interface Itf_2{ String i = "abc"; int k = 2; void g(); } interface Itf_3 extends Itf_1, Itf_2{ int j = 3; } class Itf_class2 implements Itf_3{ public void f(){ //overwrite System.out.printf("i1 is %d, i2 is %s, k is %d, j is %d\n", Itf_1.i, Itf_2.i, this.k, this.j); } public void g(){ } }
例如上面的成员i, 如果在类中直接调用this.i, 编译失败..
3.2.1 假如1个接口A 继承另外两个接口B和C, 但是接口B,C 有相同名字的方法f().
package Interface_kng.Extend; interface Itf_1{ int i = 1; void f(); } interface Itf_2{ String i = "abc"; int k = 2; //same name with Itf_1 void f(); //same name with Itf_1 void g(); } interface Itf_3 extends Itf_1, Itf_2{ int j = 3; } class Itf_class2 implements Itf_3{ public void f(){ //overwrite System.out.printf("i1 is %d, i2 is %s, k is %d, j is %d\n", Itf_1.i, Itf_2.i, this.k, this.j); } public void g(){ } }
interface Itf_1{ int i = 1; void f(); } interface Itf_2{ String i = "abc"; int k = 2; //same name with Itf_1 int f(); //same name with Itf_1, but diff return type void g(); }
原因也很简单, 因为1个非抽象类无法同时重写这两个方法.
3.5 1个类可以同时实现多个接口.
interface Itf_1{ int i = 1; void f(); } interface Itf_2{ String i = "abc"; int k = 2; //same name with Itf_1 void f(); //same name with Itf_1 void g(); } class Itf_class1 implements Itf_1, Itf_2{ public void f(){ //overwrite System.out.printf("i1 is %d, i2 is %s, k is %d", Itf_1.i, Itf_2.i, this.k); } public void g(){ } }
3.6 1个类可以继承另一个类的同时实现1个或多个接口.
四, Java接口的功能和意义
原文:http://blog.csdn.net/nvd11/article/details/18888415
内容总结
以上是互联网集市为您收集整理的Java里的接口的interface 简单介绍.全部内容,希望文章能够帮你解决Java里的接口的interface 简单介绍.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。