这是写给我自己和我这种初学者看的.
Java作为1个强面向对象语言, 基本上所有东西(成员和方法)都是写在class(类)里面的.
但是也存在一种与class平行的东西, 它就是interface , 中文译成接口. 本屌能力有限在这里这能写写Interface的一些语法规则与存在意义.
Java里的接口, 就是抽象方法和常量值的集合.
这里的定义相当表面, 就如java里的类是成员和方法的集合一样.
作为1个初学者, 可以简单理解为接口就是一种特殊的抽象类. 区别下面会详细提到.
[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. 接下来讲下接口定义的一些关键点:
外部interface前面的修饰符有两种:
如果架上public 则通不过编译.
这种情况下, 这个接口只能被同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 来修饰成员, 否则编译失败.
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. } }
public class Interface_1{ public static void f(){ It_1 a = new It_class1(); // Poly a.print(); } }
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(); } }
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(); } }
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(){ } }
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(); }
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(){ } }
原文:http://blog.csdn.net/nvd11/article/details/18888415